Skip to content

Commit d9b9b90

Browse files
author
jennybuckley
committed
Merge map and struct in schema
1 parent ea680f0 commit d9b9b90

File tree

1 file changed

+25
-43
lines changed

1 file changed

+25
-43
lines changed

schema/elements.go

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ type TypeRef struct {
4848
type Atom struct {
4949
*Scalar `yaml:"scalar,omitempty"`
5050
*List `yaml:"list,omitempty"`
51-
52-
// At most, one of the below must be set, since both look the same when serialized
53-
*Struct `yaml:"struct,omitempty"`
5451
*Map `yaml:"map,omitempty"`
5552
}
5653

@@ -67,23 +64,35 @@ const (
6764
)
6865

6966
// ElementRelationship is an enum of the different possible relationships
70-
// between the elements of container types (maps, lists, structs).
67+
// between the elements of container types (maps, lists).
7168
type ElementRelationship string
7269

7370
const (
7471
// Associative only applies to lists (see the documentation there).
7572
Associative = ElementRelationship("associative")
76-
// Atomic makes container types (lists, maps, structs) behave
73+
// Atomic makes container types (lists, maps) behave
7774
// as scalars / leaf fields
7875
Atomic = ElementRelationship("atomic")
7976
// Separable means the items of the container type have no particular
80-
// relationship (default behavior for maps and structs).
77+
// relationship (default behavior for maps).
8178
Separable = ElementRelationship("separable")
8279
)
8380

84-
// Struct represents a type which is composed of a number of different fields.
81+
// Map is a key-value pair. Its default semantics are the same as an
82+
// associative list, but:
83+
// * It is serialized differently:
84+
// map: {"k": {"value": "v"}}
85+
// list: [{"key": "k", "value": "v"}]
86+
// * Keys must be string typed.
87+
// * Keys can't have multiple components.
88+
//
89+
// Optionally, maps may be atomic (for example, imagine representing an RGB
90+
// color value--it doesn't make sense to have different actors own the R and G
91+
// values).
92+
//
93+
// Maps may also represent a type which is composed of a number of different fields.
8594
// Each field has a name and a type.
86-
type Struct struct {
95+
type Map struct {
8796
// Each struct field appears exactly once in this list. The order in
8897
// this list defines the canonical field ordering.
8998
Fields []StructField `yaml:"fields,omitempty"`
@@ -95,14 +104,17 @@ type Struct struct {
95104
// overlap between unions.
96105
Unions []Union `yaml:"unions,omitempty"`
97106

98-
// ElementRelationship states the relationship between the struct's items.
107+
// ElementType is the type of the structs's unknown fields.
108+
ElementType TypeRef `yaml:"elementType,omitempty"`
109+
110+
// ElementRelationship states the relationship between the map's items.
99111
// * `separable` (or unset) implies that each element is 100% independent.
100112
// * `atomic` implies that all elements depend on each other, and this
101113
// is effectively a scalar / leaf field; it doesn't make sense for
102114
// separate actors to set the elements. Example: an RGB color struct;
103115
// it would never make sense to "own" only one component of the
104116
// color.
105-
// The default behavior for structs is `separable`; it's permitted to
117+
// The default behavior for maps is `separable`; it's permitted to
106118
// leave this unset to get the default behavior.
107119
ElementRelationship ElementRelationship `yaml:"elementRelationship,omitempty"`
108120
}
@@ -169,15 +181,14 @@ type List struct {
169181
// * `atomic`: the list is treated as a single entity, like a scalar.
170182
// * `associative`:
171183
// - If the list element is a scalar, the list is treated as a set.
172-
// - If the list element is a struct, the list is treated as a map.
173-
// - The list element must not be a map or a list itself.
184+
// - If the list element is a map, the list is treated as a map.
174185
// There is no default for this value for lists; all schemas must
175186
// explicitly state the element relationship for all lists.
176187
ElementRelationship ElementRelationship `yaml:"elementRelationship,omitempty"`
177188

178189
// Iff ElementRelationship is `associative`, and the element type is
179-
// struct, then Keys must have non-zero length, and it lists the fields
180-
// of the element's struct type which are to be used as the keys of the
190+
// map, then Keys must have non-zero length, and it lists the fields
191+
// of the element's map type which are to be used as the keys of the
181192
// list.
182193
//
183194
// TODO: change this to "non-atomic struct" above and make the code reflect this.
@@ -186,35 +197,6 @@ type List struct {
186197
Keys []string `yaml:"keys,omitempty"`
187198
}
188199

189-
// Map is a key-value pair. Its default semantics are the same as an
190-
// associative list, but:
191-
// * It is serialized differently:
192-
// map: {"k": {"value": "v"}}
193-
// list: [{"key": "k", "value": "v"}]
194-
// * Keys must be string typed.
195-
// * Keys can't have multiple components.
196-
//
197-
// Although serialized the same, maps are different from structs in that each
198-
// map item must have the same type.
199-
//
200-
// Optionally, maps may be atomic (for example, imagine representing an RGB
201-
// color value--it doesn't make sense to have different actors own the R and G
202-
// values).
203-
type Map struct {
204-
// ElementType is the type of the list's elements.
205-
ElementType TypeRef `yaml:"elementType,omitempty"`
206-
207-
// ElementRelationship states the relationship between the map's items.
208-
// * `separable` implies that each element is 100% independent.
209-
// * `atomic` implies that all elements depend on each other, and this
210-
// is effectively a scalar / leaf field; it doesn't make sense for
211-
// separate actors to set the elements.
212-
// TODO: find a simple example.
213-
// The default behavior for maps is `separable`; it's permitted to
214-
// leave this unset to get the default behavior.
215-
ElementRelationship ElementRelationship `yaml:"elementRelationship,omitempty"`
216-
}
217-
218200
// FindNamedType is a convenience function that returns the referenced TypeDef,
219201
// if it exists, or (nil, false) if it doesn't.
220202
func (s Schema) FindNamedType(name string) (TypeDef, bool) {

0 commit comments

Comments
 (0)