schema_options.gno

1.11 Kb ยท 41 lines
 1package datastore
 2
 3import "strings"
 4
 5// StorageOption configures schemas.
 6type SchemaOption func(*Schema)
 7
 8// WithField assign a new field to the schema definition.
 9func WithField(name string) SchemaOption {
10	return func(s *Schema) {
11		name = strings.TrimSpace(name)
12		if name != "" {
13			s.fields.Append(name)
14		}
15	}
16}
17
18// WithDefaultField assign a new field with a default value to the schema definition.
19// Default value is assigned to newly created records asociated to to schema.
20func WithDefaultField(name string, value interface{}) SchemaOption {
21	return func(s *Schema) {
22		name = strings.TrimSpace(name)
23		if name != "" {
24			s.fields.Append(name)
25
26			key := castIntToKey(s.fields.Len() - 1)
27			s.defaults.Set(key, value)
28		}
29	}
30}
31
32// Strict configures the schema as a strict one.
33// By default schemas should allow the creation of any user defined field,
34// making them strict limits the allowed record fields to the ones pre-defined
35// in the schema. Fields are pre-defined using `WithField`, `WithDefaultField`
36// or by calling `Schema.AddField()`.
37func Strict() SchemaOption {
38	return func(s *Schema) {
39		s.strict = true
40	}
41}