Skip to content

Commit e3c6ed6

Browse files
author
jennybuckley
committed
Remove TypedValue interface
1 parent b13920e commit e3c6ed6

File tree

8 files changed

+60
-84
lines changed

8 files changed

+60
-84
lines changed

internal/cli/operation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type operationBase struct {
3333
typeName string
3434
}
3535

36-
func (b operationBase) parseFile(path string) (tv typed.TypedValue, err error) {
36+
func (b operationBase) parseFile(path string) (tv *typed.TypedValue, err error) {
3737
bytes, err := ioutil.ReadFile(path)
3838
if err != nil {
3939
return tv, fmt.Errorf("unable to read file %q: %v", path, err)

internal/fixture/state.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// State of the current test in terms of live object. One can check at
3030
// any time that Live and Managers match the expectations.
3131
type State struct {
32-
Live typed.TypedValue
32+
Live *typed.TypedValue
3333
Parser typed.ParseableType
3434
Managers fieldpath.ManagedFields
3535
Updater *merge.Updater
@@ -146,7 +146,7 @@ type dummyConverter struct{}
146146
var _ merge.Converter = dummyConverter{}
147147

148148
// Convert returns the object given in input, not doing any conversion.
149-
func (dummyConverter) Convert(v typed.TypedValue, version fieldpath.APIVersion) (typed.TypedValue, error) {
149+
func (dummyConverter) Convert(v *typed.TypedValue, version fieldpath.APIVersion) (*typed.TypedValue, error) {
150150
if len(version) == 0 {
151151
return nil, fmt.Errorf("cannot convert to invalid version: %q", version)
152152
}

merge/multiple_appliers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ var _ merge.Converter = repeatingConverter{}
834834
var missingVersionError error = fmt.Errorf("cannot convert to invalid version")
835835

836836
// Convert implements merge.Converter
837-
func (r repeatingConverter) Convert(v typed.TypedValue, version fieldpath.APIVersion) (typed.TypedValue, error) {
837+
func (r repeatingConverter) Convert(v *typed.TypedValue, version fieldpath.APIVersion) (*typed.TypedValue, error) {
838838
if len(version) < 2 || string(version)[0] != 'v' {
839839
return nil, missingVersionError
840840
}

merge/obsolete_versions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type specificVersionConverter struct {
3232
AcceptedVersions []fieldpath.APIVersion
3333
}
3434

35-
func (d *specificVersionConverter) Convert(object typed.TypedValue, version fieldpath.APIVersion) (typed.TypedValue, error) {
35+
func (d *specificVersionConverter) Convert(object *typed.TypedValue, version fieldpath.APIVersion) (*typed.TypedValue, error) {
3636
for _, v := range d.AcceptedVersions {
3737
if v == version {
3838
return object, nil

merge/update.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
// Converter is an interface to the conversion logic. The converter
2424
// needs to be able to convert objects from one version to another.
2525
type Converter interface {
26-
Convert(object typed.TypedValue, version fieldpath.APIVersion) (typed.TypedValue, error)
26+
Convert(object *typed.TypedValue, version fieldpath.APIVersion) (*typed.TypedValue, error)
2727
IsMissingVersionError(error) bool
2828
}
2929

@@ -33,12 +33,12 @@ type Updater struct {
3333
Converter Converter
3434
}
3535

36-
func (s *Updater) update(oldObject, newObject typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, workflow string, force bool) (fieldpath.ManagedFields, error) {
36+
func (s *Updater) update(oldObject, newObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, workflow string, force bool) (fieldpath.ManagedFields, error) {
3737
conflicts := fieldpath.ManagedFields{}
3838
removed := fieldpath.ManagedFields{}
3939
type Versioned struct {
40-
oldObject typed.TypedValue
41-
newObject typed.TypedValue
40+
oldObject *typed.TypedValue
41+
newObject *typed.TypedValue
4242
}
4343
versions := map[fieldpath.APIVersion]Versioned{
4444
version: Versioned{
@@ -119,7 +119,7 @@ func (s *Updater) update(oldObject, newObject typed.TypedValue, version fieldpat
119119
// that you intend to persist (after applying the patch if this is for a
120120
// PATCH call), and liveObject must be the original object (empty if
121121
// this is a CREATE call).
122-
func (s *Updater) Update(liveObject, newObject typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string) (fieldpath.ManagedFields, error) {
122+
func (s *Updater) Update(liveObject, newObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string) (fieldpath.ManagedFields, error) {
123123
var err error
124124
managers = shallowCopyManagers(managers)
125125
managers, err = s.update(liveObject, newObject, version, managers, manager, true)
@@ -146,7 +146,7 @@ func (s *Updater) Update(liveObject, newObject typed.TypedValue, version fieldpa
146146
// Apply should be called when Apply is run, given the current object as
147147
// well as the configuration that is applied. This will merge the object
148148
// and return it.
149-
func (s *Updater) Apply(liveObject, configObject typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string, force bool) (typed.TypedValue, fieldpath.ManagedFields, error) {
149+
func (s *Updater) Apply(liveObject, configObject *typed.TypedValue, version fieldpath.APIVersion, managers fieldpath.ManagedFields, manager string, force bool) (*typed.TypedValue, fieldpath.ManagedFields, error) {
150150
managers = shallowCopyManagers(managers)
151151
newObject, err := liveObject.Merge(configObject)
152152
if err != nil {
@@ -185,7 +185,7 @@ func shallowCopyManagers(managers fieldpath.ManagedFields) fieldpath.ManagedFiel
185185
// * applyingManager applied it last time
186186
// * applyingManager didn't apply it this time
187187
// * no other applier claims to manage it
188-
func (s *Updater) prune(merged typed.TypedValue, managers fieldpath.ManagedFields, applyingManager string, lastSet *fieldpath.VersionedSet) (typed.TypedValue, error) {
188+
func (s *Updater) prune(merged *typed.TypedValue, managers fieldpath.ManagedFields, applyingManager string, lastSet *fieldpath.VersionedSet) (*typed.TypedValue, error) {
189189
if lastSet == nil || lastSet.Set.Empty() {
190190
return merged, nil
191191
}
@@ -210,7 +210,7 @@ func (s *Updater) prune(merged typed.TypedValue, managers fieldpath.ManagedField
210210

211211
// addBackOwnedItems adds back any list and map items that were removed by prune,
212212
// but other appliers (or the current applier's new config) claim to own.
213-
func (s *Updater) addBackOwnedItems(merged, pruned typed.TypedValue, managedFields fieldpath.ManagedFields, applyingManager string) (typed.TypedValue, error) {
213+
func (s *Updater) addBackOwnedItems(merged, pruned *typed.TypedValue, managedFields fieldpath.ManagedFields, applyingManager string) (*typed.TypedValue, error) {
214214
var err error
215215
managedAtVersion := map[fieldpath.APIVersion]*fieldpath.Set{}
216216
for _, managerSet := range managedFields {
@@ -253,7 +253,7 @@ func (s *Updater) addBackOwnedItems(merged, pruned typed.TypedValue, managedFiel
253253
// addBackDanglingItems makes sure that the only items removed by prune are items that were
254254
// previously owned by the currently applying manager. This will add back unowned items and items
255255
// which are owned by Updaters that shouldn't be removed.
256-
func (s *Updater) addBackDanglingItems(merged, pruned typed.TypedValue, lastSet *fieldpath.VersionedSet) (typed.TypedValue, error) {
256+
func (s *Updater) addBackDanglingItems(merged, pruned *typed.TypedValue, lastSet *fieldpath.VersionedSet) (*typed.TypedValue, error) {
257257
convertedPruned, err := s.Converter.Convert(pruned, lastSet.APIVersion)
258258
if err != nil {
259259
if s.Converter.IsMissingVersionError(err) {

typed/parser.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func (p *Parser) Type(name string) ParseableType {
7777

7878
// ParseableType allows for easy production of typed objects.
7979
type ParseableType struct {
80-
schema.TypeRef
81-
*schema.Schema
80+
TypeRef schema.TypeRef
81+
Schema *schema.Schema
8282
}
8383

8484
// IsValid return true if p's schema and typename are valid.
@@ -89,7 +89,7 @@ func (p ParseableType) IsValid() bool {
8989

9090
// FromYAML parses a yaml string into an object with the current schema
9191
// and the type "typename" or an error if validation fails.
92-
func (p ParseableType) FromYAML(object YAMLObject) (TypedValue, error) {
92+
func (p ParseableType) FromYAML(object YAMLObject) (*TypedValue, error) {
9393
v, err := value.FromYAML([]byte(object))
9494
if err != nil {
9595
return nil, err
@@ -99,7 +99,7 @@ func (p ParseableType) FromYAML(object YAMLObject) (TypedValue, error) {
9999

100100
// FromUnstructured converts a go interface to a TypedValue. It will return an
101101
// error if the resulting object fails schema validation.
102-
func (p ParseableType) FromUnstructured(in interface{}) (TypedValue, error) {
102+
func (p ParseableType) FromUnstructured(in interface{}) (*TypedValue, error) {
103103
v, err := value.FromUnstructured(in)
104104
if err != nil {
105105
return nil, err

typed/typed.go

Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,11 @@ import (
2525
"sigs.k8s.io/structured-merge-diff/value"
2626
)
2727

28-
// TypedValue is a value with an associated type.
29-
type TypedValue interface {
30-
// AsValue removes the type from the TypedValue and only keeps the value.
31-
AsValue() *value.Value
32-
// Validate returns an error with a list of every spec violation.
33-
Validate() error
34-
// ToFieldSet creates a set containing every leaf field and item mentioned, or
35-
// validation errors, if any were encountered.
36-
ToFieldSet() (*fieldpath.Set, error)
37-
// Merge returns the result of merging tv and pso ("partially specified
38-
// object") together. Of note:
39-
// * No fields can be removed by this operation.
40-
// * If both tv and pso specify a given leaf field, the result will keep pso's
41-
// value.
42-
// * Container typed elements will have their items ordered:
43-
// * like tv, if pso doesn't change anything in the container
44-
// * like pso, if pso does change something in the container.
45-
// tv and pso must both be of the same type (their Schema and TypeRef must
46-
// match), or an error will be returned. Validation errors will be returned if
47-
// the objects don't conform to the schema.
48-
Merge(pso TypedValue) (TypedValue, error)
49-
// Compare compares the two objects. See the comments on the `Comparison`
50-
// struct for details on the return value.
51-
//
52-
// tv and rhs must both be of the same type (their Schema and TypeRef must
53-
// match), or an error will be returned. Validation errors will be returned if
54-
// the objects don't conform to the schema.
55-
Compare(rhs TypedValue) (c *Comparison, err error)
56-
// RemoveItems removes each provided list or map item from the value.
57-
RemoveItems(items *fieldpath.Set) TypedValue
58-
}
59-
6028
// AsTyped accepts a value and a type and returns a TypedValue. 'v' must have
6129
// type 'typeName' in the schema. An error is returned if the v doesn't conform
6230
// to the schema.
63-
func AsTyped(v value.Value, s *schema.Schema, typeRef schema.TypeRef) (TypedValue, error) {
64-
tv := typedValue{
31+
func AsTyped(v value.Value, s *schema.Schema, typeRef schema.TypeRef) (*TypedValue, error) {
32+
tv := &TypedValue{
6533
value: v,
6634
typeRef: typeRef,
6735
schema: s,
@@ -76,36 +44,38 @@ func AsTyped(v value.Value, s *schema.Schema, typeRef schema.TypeRef) (TypedValu
7644
// conforms to the schema, for cases where that has already been checked or
7745
// where you're going to call a method that validates as a side-effect (like
7846
// ToFieldSet).
79-
func AsTypedUnvalidated(v value.Value, s *schema.Schema, typeRef schema.TypeRef) TypedValue {
80-
tv := typedValue{
47+
func AsTypedUnvalidated(v value.Value, s *schema.Schema, typeRef schema.TypeRef) *TypedValue {
48+
tv := &TypedValue{
8149
value: v,
8250
typeRef: typeRef,
8351
schema: s,
8452
}
8553
return tv
8654
}
8755

88-
// typedValue is a value of some specific type.
89-
type typedValue struct {
56+
// TypedValue is a value of some specific type.
57+
type TypedValue struct {
9058
value value.Value
9159
typeRef schema.TypeRef
9260
schema *schema.Schema
9361
}
9462

95-
var _ TypedValue = typedValue{}
96-
97-
func (tv typedValue) AsValue() *value.Value {
63+
// AsValue removes the type from the TypedValue and only keeps the value.
64+
func (tv TypedValue) AsValue() *value.Value {
9865
return &tv.value
9966
}
10067

101-
func (tv typedValue) Validate() error {
68+
// Validate returns an error with a list of every spec violation.
69+
func (tv TypedValue) Validate() error {
10270
if errs := tv.walker().validate(); len(errs) != 0 {
10371
return errs
10472
}
10573
return nil
10674
}
10775

108-
func (tv typedValue) ToFieldSet() (*fieldpath.Set, error) {
76+
// ToFieldSet creates a set containing every leaf field and item mentioned, or
77+
// validation errors, if any were encountered.
78+
func (tv TypedValue) ToFieldSet() (*fieldpath.Set, error) {
10979
s := fieldpath.NewSet()
11080
w := tv.walker()
11181
w.leafFieldCallback = func(p fieldpath.Path) { s.Insert(p) }
@@ -116,27 +86,34 @@ func (tv typedValue) ToFieldSet() (*fieldpath.Set, error) {
11686
return s, nil
11787
}
11888

119-
func (tv typedValue) Merge(pso TypedValue) (TypedValue, error) {
120-
tpso, ok := pso.(typedValue)
121-
if !ok {
122-
return nil, errorFormatter{}.
123-
errorf("can't merge typedValue with %T", pso)
124-
}
125-
return merge(tv, tpso, ruleKeepRHS, nil)
89+
// Merge returns the result of merging tv and pso ("partially specified
90+
// object") together. Of note:
91+
// * No fields can be removed by this operation.
92+
// * If both tv and pso specify a given leaf field, the result will keep pso's
93+
// value.
94+
// * Container typed elements will have their items ordered:
95+
// * like tv, if pso doesn't change anything in the container
96+
// * like pso, if pso does change something in the container.
97+
// tv and pso must both be of the same type (their Schema and TypeRef must
98+
// match), or an error will be returned. Validation errors will be returned if
99+
// the objects don't conform to the schema.
100+
func (tv TypedValue) Merge(pso *TypedValue) (*TypedValue, error) {
101+
return merge(&tv, pso, ruleKeepRHS, nil)
126102
}
127103

128-
func (tv typedValue) Compare(rhs TypedValue) (c *Comparison, err error) {
129-
trhs, ok := rhs.(typedValue)
130-
if !ok {
131-
return nil, errorFormatter{}.
132-
errorf("can't compare typedValue with %T", rhs)
133-
}
104+
// Compare compares the two objects. See the comments on the `Comparison`
105+
// struct for details on the return value.
106+
//
107+
// tv and rhs must both be of the same type (their Schema and TypeRef must
108+
// match), or an error will be returned. Validation errors will be returned if
109+
// the objects don't conform to the schema.
110+
func (tv TypedValue) Compare(rhs *TypedValue) (c *Comparison, err error) {
134111
c = &Comparison{
135112
Removed: fieldpath.NewSet(),
136113
Modified: fieldpath.NewSet(),
137114
Added: fieldpath.NewSet(),
138115
}
139-
c.Merged, err = merge(tv, trhs, func(w *mergingWalker) {
116+
c.Merged, err = merge(&tv, rhs, func(w *mergingWalker) {
140117
if w.lhs == nil {
141118
c.Added.Insert(w.path)
142119
} else if w.rhs == nil {
@@ -163,14 +140,13 @@ func (tv typedValue) Compare(rhs TypedValue) (c *Comparison, err error) {
163140
}
164141

165142
// RemoveItems removes each provided list or map item from the value.
166-
func (tv typedValue) RemoveItems(items *fieldpath.Set) TypedValue {
167-
copied := tv
168-
copied.value, _ = value.FromUnstructured(tv.value.ToUnstructured(true))
169-
removeItemsWithSchema(&copied.value, items, copied.schema, copied.typeRef)
170-
return copied
143+
func (tv TypedValue) RemoveItems(items *fieldpath.Set) *TypedValue {
144+
tv.value, _ = value.FromUnstructured(tv.value.ToUnstructured(true))
145+
removeItemsWithSchema(&tv.value, items, tv.schema, tv.typeRef)
146+
return &tv
171147
}
172148

173-
func merge(lhs, rhs typedValue, rule, postRule mergeRule) (TypedValue, error) {
149+
func merge(lhs, rhs *TypedValue, rule, postRule mergeRule) (*TypedValue, error) {
174150
if lhs.schema != rhs.schema {
175151
return nil, errorFormatter{}.
176152
errorf("expected objects with types from the same schema")
@@ -193,7 +169,7 @@ func merge(lhs, rhs typedValue, rule, postRule mergeRule) (TypedValue, error) {
193169
return nil, errs
194170
}
195171

196-
out := typedValue{
172+
out := &TypedValue{
197173
schema: lhs.schema,
198174
typeRef: lhs.typeRef,
199175
}
@@ -212,7 +188,7 @@ func merge(lhs, rhs typedValue, rule, postRule mergeRule) (TypedValue, error) {
212188
type Comparison struct {
213189
// Merged is the result of merging the two objects, as explained in the
214190
// comments on TypedValue.Merge().
215-
Merged TypedValue
191+
Merged *TypedValue
216192

217193
// Removed contains any fields removed by rhs (the right-hand-side
218194
// object in the comparison).

typed/validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"sigs.k8s.io/structured-merge-diff/value"
2323
)
2424

25-
func (tv typedValue) walker() *validatingObjectWalker {
25+
func (tv TypedValue) walker() *validatingObjectWalker {
2626
return &validatingObjectWalker{
2727
value: tv.value,
2828
schema: tv.schema,

0 commit comments

Comments
 (0)