@@ -16,6 +16,8 @@ limitations under the License.
16
16
17
17
package schema
18
18
19
+ import "sigs.k8s.io/structured-merge-diff/value"
20
+
19
21
// Schema is a list of named types.
20
22
type Schema struct {
21
23
Types []TypeDef `yaml:"types,omitempty"`
@@ -77,6 +79,8 @@ const (
77
79
// Separable means the items of the container type have no particular
78
80
// relationship (default behavior for maps and structs).
79
81
Separable = ElementRelationship ("separable" )
82
+ // Deduced only applies to untyped (see the documentation there).
83
+ Deduced = ElementRelationship ("deduced" )
80
84
)
81
85
82
86
// Struct represents a type which is composed of a number of different fields.
@@ -180,17 +184,45 @@ type Map struct {
180
184
type Untyped struct {
181
185
// ElementRelationship states the relationship between the items, if
182
186
// container-typed data happens to be present here.
187
+ // * `deduced` implies that the behavior is based on the type of data.
188
+ // Structs and maps are both treated as a `separable` Map with `deduced` Untyped elements.
189
+ // Lists and Scalars are both treated as an `atomic` Untyped.
183
190
// * `atomic` implies that all elements depend on each other, and this
184
191
// is effectively a scalar / leaf field; it doesn't make sense for
185
192
// separate actors to set the elements.
186
193
// TODO: support "guess" (guesses at associative list keys)
187
- // TODO: support "lookup" (calls a lookup function to figure out the
188
- // schema based on the data)
189
194
// The default behavior for untyped data is `atomic`; it's permitted to
190
195
// leave this unset to get the default behavior.
191
196
ElementRelationship ElementRelationship `yaml:"elementRelationship,omitempty"`
192
197
}
193
198
199
+ // DeduceType determines the behavior based on a value.
200
+ func DeduceType (v * value.Value ) TypeRef {
201
+ if v != nil && v .MapValue != nil {
202
+ return TypeRef {
203
+ Inlined : Atom {
204
+ Map : & Map {
205
+ ElementType : TypeRef {
206
+ Inlined : Atom {
207
+ Untyped : & Untyped {
208
+ ElementRelationship : Deduced ,
209
+ },
210
+ },
211
+ },
212
+ ElementRelationship : Separable ,
213
+ },
214
+ },
215
+ }
216
+ }
217
+ return TypeRef {
218
+ Inlined : Atom {
219
+ Untyped : & Untyped {
220
+ ElementRelationship : Atomic ,
221
+ },
222
+ },
223
+ }
224
+ }
225
+
194
226
// FindNamedType is a convenience function that returns the referenced TypeDef,
195
227
// if it exists, or (nil, false) if it doesn't.
196
228
func (s Schema ) FindNamedType (name string ) (TypeDef , bool ) {
0 commit comments