Skip to content

Commit b13920e

Browse files
author
jennybuckley
committed
Make deduced types use the same parser
1 parent e85c7b2 commit b13920e

File tree

10 files changed

+113
-273
lines changed

10 files changed

+113
-273
lines changed

merge/deduced_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func TestDeduced(t *testing.T) {
168168
},
169169
},
170170
},
171-
"leaf_apply_twice_dangling": {
171+
"leaf_apply_twice_remove": {
172172
Ops: []Operation{
173173
Apply{
174174
Manager: "default",
@@ -188,9 +188,7 @@ func TestDeduced(t *testing.T) {
188188
},
189189
},
190190
Object: `
191-
numeric: 1
192191
string: "new string"
193-
bool: false
194192
`,
195193
Managed: fieldpath.ManagedFields{
196194
"default": &fieldpath.VersionedSet{
@@ -328,9 +326,7 @@ func TestDeduced(t *testing.T) {
328326
Object: ``,
329327
},
330328
},
331-
Object: `
332-
string: "string"
333-
`,
329+
Object: ``,
334330
Managed: fieldpath.ManagedFields{},
335331
},
336332
"apply_update_apply_nested": {
@@ -523,7 +519,7 @@ func TestDeduced(t *testing.T) {
523519

524520
for name, test := range tests {
525521
t.Run(name, func(t *testing.T) {
526-
if err := test.Test(typed.DeducedParseableType{}); err != nil {
522+
if err := test.Test(typed.DeducedParseableType); err != nil {
527523
t.Fatal(err)
528524
}
529525
})

merge/obsolete_versions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestObsoleteVersions(t *testing.T) {
5353
}
5454
state := fixture.State{
5555
Updater: &merge.Updater{Converter: converter},
56-
Parser: typed.DeducedParseableType{},
56+
Parser: typed.DeducedParseableType,
5757
}
5858

5959
if err := state.Update(typed.YAMLObject(`{"v1": 0}`), fieldpath.APIVersion("v1"), "v1"); err != nil {

schema/elements.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616

1717
package schema
1818

19+
import "sigs.k8s.io/structured-merge-diff/value"
20+
1921
// Schema is a list of named types.
2022
type Schema struct {
2123
Types []TypeDef `yaml:"types,omitempty"`
@@ -77,6 +79,8 @@ const (
7779
// Separable means the items of the container type have no particular
7880
// relationship (default behavior for maps and structs).
7981
Separable = ElementRelationship("separable")
82+
// Deduced only applies to untyped (see the documentation there).
83+
Deduced = ElementRelationship("deduced")
8084
)
8185

8286
// Struct represents a type which is composed of a number of different fields.
@@ -180,17 +184,45 @@ type Map struct {
180184
type Untyped struct {
181185
// ElementRelationship states the relationship between the items, if
182186
// 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.
183190
// * `atomic` implies that all elements depend on each other, and this
184191
// is effectively a scalar / leaf field; it doesn't make sense for
185192
// separate actors to set the elements.
186193
// 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)
189194
// The default behavior for untyped data is `atomic`; it's permitted to
190195
// leave this unset to get the default behavior.
191196
ElementRelationship ElementRelationship `yaml:"elementRelationship,omitempty"`
192197
}
193198

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+
194226
// FindNamedType is a convenience function that returns the referenced TypeDef,
195227
// if it exists, or (nil, false) if it doesn't.
196228
func (s Schema) FindNamedType(name string) (TypeDef, bool) {

typed/deduced.go

Lines changed: 0 additions & 178 deletions
This file was deleted.

0 commit comments

Comments
 (0)