From 5f3ed22772c717c8dff7e65e179f0e5139891d14 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 19 Jun 2024 14:19:50 +0200 Subject: [PATCH 1/5] Return dyn.InvalidValue to indicate absence --- libs/dyn/convert/from_typed.go | 8 +++++++- libs/dyn/value.go | 8 ++++---- libs/dyn/walk.go | 6 +++--- libs/dyn/walk_test.go | 6 +++--- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/libs/dyn/convert/from_typed.go b/libs/dyn/convert/from_typed.go index ae491d8ab7..b57d52be80 100644 --- a/libs/dyn/convert/from_typed.go +++ b/libs/dyn/convert/from_typed.go @@ -172,9 +172,15 @@ func fromTypedSlice(src reflect.Value, ref dyn.Value) (dyn.Value, error) { out := make([]dyn.Value, src.Len()) for i := 0; i < src.Len(); i++ { v := src.Index(i) + refv := ref.Index(i) + + // Use nil reference if there is no reference for this index. + if refv == dyn.InvalidValue { + refv = dyn.NilValue + } // Convert entry taking into account the reference value (may be equal to dyn.NilValue). - nv, err := fromTyped(v.Interface(), ref.Index(i), includeZeroValuedScalars) + nv, err := fromTyped(v.Interface(), refv, includeZeroValuedScalars) if err != nil { return dyn.InvalidValue, err } diff --git a/libs/dyn/value.go b/libs/dyn/value.go index 2e8f1b9aff..3d62ea1f51 100644 --- a/libs/dyn/value.go +++ b/libs/dyn/value.go @@ -110,12 +110,12 @@ func (v Value) AsAny() any { func (v Value) Get(key string) Value { m, ok := v.AsMap() if !ok { - return NilValue + return InvalidValue } vv, ok := m.GetByString(key) if !ok { - return NilValue + return InvalidValue } return vv @@ -124,11 +124,11 @@ func (v Value) Get(key string) Value { func (v Value) Index(i int) Value { s, ok := v.v.([]Value) if !ok { - return NilValue + return InvalidValue } if i < 0 || i >= len(s) { - return NilValue + return InvalidValue } return s[i] diff --git a/libs/dyn/walk.go b/libs/dyn/walk.go index 97b99b061e..c51a11e22c 100644 --- a/libs/dyn/walk.go +++ b/libs/dyn/walk.go @@ -28,7 +28,7 @@ func walk(v Value, p Path, fn func(p Path, v Value) (Value, error)) (Value, erro if err == ErrSkip { return v, nil } - return NilValue, err + return InvalidValue, err } switch v.Kind() { @@ -43,7 +43,7 @@ func walk(v Value, p Path, fn func(p Path, v Value) (Value, error)) (Value, erro continue } if err != nil { - return NilValue, err + return InvalidValue, err } out.Set(pk, nv) } @@ -57,7 +57,7 @@ func walk(v Value, p Path, fn func(p Path, v Value) (Value, error)) (Value, erro continue } if err != nil { - return NilValue, err + return InvalidValue, err } out = append(out, nv) } diff --git a/libs/dyn/walk_test.go b/libs/dyn/walk_test.go index d62b9a4db8..f7222b0a5b 100644 --- a/libs/dyn/walk_test.go +++ b/libs/dyn/walk_test.go @@ -39,7 +39,7 @@ func (w *walkCallTracker) returnSkip(path string) { } func (w *walkCallTracker) returnDrop(path string) { - w.on(path, func(v Value) Value { return NilValue }, ErrDrop) + w.on(path, func(v Value) Value { return InvalidValue }, ErrDrop) } func (w *walkCallTracker) track(p Path, v Value) (Value, error) { @@ -148,7 +148,7 @@ func TestWalkMapError(t *testing.T) { }) out, err := Walk(value, tracker.track) assert.Equal(t, cerr, err) - assert.Equal(t, NilValue, out) + assert.Equal(t, InvalidValue, out) // The callback should have been called twice. assert.Len(t, tracker.calls, 2) @@ -239,7 +239,7 @@ func TestWalkSequenceError(t *testing.T) { }) out, err := Walk(value, tracker.track) assert.Equal(t, cerr, err) - assert.Equal(t, NilValue, out) + assert.Equal(t, InvalidValue, out) // The callback should have been called three times. assert.Len(t, tracker.calls, 3) From 7b6629abef14ca8c5afb29b36e0df288fc5368cd Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 19 Jun 2024 15:40:25 +0200 Subject: [PATCH 2/5] Update all call sites to work with InvalidValue --- bundle/config/mutator/environments_compat.go | 12 +++--- bundle/config/mutator/merge_job_clusters.go | 2 +- bundle/config/mutator/merge_job_tasks.go | 2 +- .../config/mutator/merge_pipeline_clusters.go | 2 +- bundle/config/root.go | 30 ++++++++------ .../deploy/terraform/tfdyn/convert_grants.go | 6 +-- .../terraform/tfdyn/convert_permissions.go | 10 ++--- bundle/permissions/filter.go | 4 +- libs/dyn/convert/from_typed.go | 2 +- libs/dyn/convert/from_typed_test.go | 12 +++--- libs/dyn/convert/normalize_test.go | 4 +- libs/dyn/merge/elements_by_key.go | 2 +- libs/dyn/merge/elements_by_key_test.go | 4 +- libs/dyn/value.go | 4 +- libs/dyn/visit_map_test.go | 4 +- libs/dyn/yamlloader/yaml_anchor_test.go | 40 +++++++++---------- libs/dyn/yamlloader/yaml_mix_test.go | 8 ++-- 17 files changed, 76 insertions(+), 72 deletions(-) diff --git a/bundle/config/mutator/environments_compat.go b/bundle/config/mutator/environments_compat.go index cbedcaefd5..955b30c10d 100644 --- a/bundle/config/mutator/environments_compat.go +++ b/bundle/config/mutator/environments_compat.go @@ -28,22 +28,22 @@ func (m *environmentsToTargets) Apply(ctx context.Context, b *bundle.Bundle) dia // The "environments" key is set; validate and rewrite it to "targets". err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) { - environments := v.Get("environments") - targets := v.Get("targets") + environments := v.GetTODO("environments") + targets := v.GetTODO("targets") // Return an error if both "environments" and "targets" are set. - if environments != dyn.NilValue && targets != dyn.NilValue { - return dyn.NilValue, fmt.Errorf( + if environments != dyn.InvalidValue && targets != dyn.InvalidValue { + return dyn.InvalidValue, fmt.Errorf( "both 'environments' and 'targets' are specified; only 'targets' should be used: %s", environments.Location().String(), ) } // Rewrite "environments" to "targets". - if environments != dyn.NilValue && targets == dyn.NilValue { + if environments != dyn.InvalidValue && targets == dyn.InvalidValue { nv, err := dyn.Set(v, "targets", environments) if err != nil { - return dyn.NilValue, err + return dyn.InvalidValue, err } // Drop the "environments" key. return dyn.Walk(nv, func(p dyn.Path, v dyn.Value) (dyn.Value, error) { diff --git a/bundle/config/mutator/merge_job_clusters.go b/bundle/config/mutator/merge_job_clusters.go index 20f4efe851..ec61546085 100644 --- a/bundle/config/mutator/merge_job_clusters.go +++ b/bundle/config/mutator/merge_job_clusters.go @@ -21,7 +21,7 @@ func (m *mergeJobClusters) Name() string { func (m *mergeJobClusters) jobClusterKey(v dyn.Value) string { switch v.Kind() { - case dyn.KindNil: + case dyn.KindInvalid, dyn.KindNil: return "" case dyn.KindString: return v.MustString() diff --git a/bundle/config/mutator/merge_job_tasks.go b/bundle/config/mutator/merge_job_tasks.go index 68c05383c3..f9a9bf7182 100644 --- a/bundle/config/mutator/merge_job_tasks.go +++ b/bundle/config/mutator/merge_job_tasks.go @@ -21,7 +21,7 @@ func (m *mergeJobTasks) Name() string { func (m *mergeJobTasks) taskKeyString(v dyn.Value) string { switch v.Kind() { - case dyn.KindNil: + case dyn.KindInvalid, dyn.KindNil: return "" case dyn.KindString: return v.MustString() diff --git a/bundle/config/mutator/merge_pipeline_clusters.go b/bundle/config/mutator/merge_pipeline_clusters.go index 0b1cf89836..c75f65326d 100644 --- a/bundle/config/mutator/merge_pipeline_clusters.go +++ b/bundle/config/mutator/merge_pipeline_clusters.go @@ -22,7 +22,7 @@ func (m *mergePipelineClusters) Name() string { func (m *mergePipelineClusters) clusterLabel(v dyn.Value) string { switch v.Kind() { - case dyn.KindNil: + case dyn.KindInvalid, dyn.KindNil: // Note: the cluster label is optional and defaults to 'default'. // We therefore ALSO merge all clusters without a label. return "default" diff --git a/bundle/config/root.go b/bundle/config/root.go index 88197c2b87..56c25e0766 100644 --- a/bundle/config/root.go +++ b/bundle/config/root.go @@ -337,7 +337,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Merge `run_as`. This field must be overwritten if set, not merged. - if v := target.Get("run_as"); v != dyn.NilValue { + if v := target.GetTODO("run_as"); v != dyn.InvalidValue { root, err = dyn.Set(root, "run_as", v) if err != nil { return err @@ -345,7 +345,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Below, we're setting fields on the bundle key, so make sure it exists. - if root.Get("bundle") == dyn.NilValue { + if root.GetTODO("bundle") == dyn.InvalidValue { root, err = dyn.Set(root, "bundle", dyn.NewValue(map[string]dyn.Value{}, dyn.Location{})) if err != nil { return err @@ -353,7 +353,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Merge `mode`. This field must be overwritten if set, not merged. - if v := target.Get("mode"); v != dyn.NilValue { + if v := target.GetTODO("mode"); v != dyn.InvalidValue { root, err = dyn.SetByPath(root, dyn.NewPath(dyn.Key("bundle"), dyn.Key("mode")), v) if err != nil { return err @@ -361,7 +361,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Merge `compute_id`. This field must be overwritten if set, not merged. - if v := target.Get("compute_id"); v != dyn.NilValue { + if v := target.GetTODO("compute_id"); v != dyn.InvalidValue { root, err = dyn.SetByPath(root, dyn.NewPath(dyn.Key("bundle"), dyn.Key("compute_id")), v) if err != nil { return err @@ -369,7 +369,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Merge `git`. - if v := target.Get("git"); v != dyn.NilValue { + if v := target.GetTODO("git"); v != dyn.InvalidValue { ref, err := dyn.GetByPath(root, dyn.NewPath(dyn.Key("bundle"), dyn.Key("git"))) if err != nil { ref = dyn.NewValue(map[string]dyn.Value{}, dyn.Location{}) @@ -382,7 +382,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // If the branch was overridden, we need to clear the inferred flag. - if branch := v.Get("branch"); branch != dyn.NilValue { + if branch := v.GetTODO("branch"); branch != dyn.InvalidValue { out, err = dyn.SetByPath(out, dyn.NewPath(dyn.Key("inferred")), dyn.NewValue(false, dyn.Location{})) if err != nil { return err @@ -410,7 +410,7 @@ func rewriteShorthands(v dyn.Value) (dyn.Value, error) { // For each target, rewrite the variables block. return dyn.Map(v, "targets", dyn.Foreach(func(_ dyn.Path, target dyn.Value) (dyn.Value, error) { // Confirm it has a variables block. - if target.Get("variables") == dyn.NilValue { + if target.GetTODO("variables") == dyn.InvalidValue { return target, nil } @@ -440,15 +440,19 @@ func validateVariableOverrides(root, target dyn.Value) (err error) { var tv map[string]variable.Variable // Collect variables from the root. - err = convert.ToTyped(&rv, root.Get("variables")) - if err != nil { - return fmt.Errorf("unable to collect variables from root: %w", err) + if v := root.GetTODO("variables"); v != dyn.InvalidValue { + err = convert.ToTyped(&rv, v) + if err != nil { + return fmt.Errorf("unable to collect variables from root: %w", err) + } } // Collect variables from the target. - err = convert.ToTyped(&tv, target.Get("variables")) - if err != nil { - return fmt.Errorf("unable to collect variables from target: %w", err) + if v := target.GetTODO("variables"); v != dyn.InvalidValue { + err = convert.ToTyped(&tv, v) + if err != nil { + return fmt.Errorf("unable to collect variables from target: %w", err) + } } // Check that all variables in the target exist in the root. diff --git a/bundle/deploy/terraform/tfdyn/convert_grants.go b/bundle/deploy/terraform/tfdyn/convert_grants.go index 1ddd99dd70..8e3f66f68b 100644 --- a/bundle/deploy/terraform/tfdyn/convert_grants.go +++ b/bundle/deploy/terraform/tfdyn/convert_grants.go @@ -8,15 +8,15 @@ import ( ) func convertGrantsResource(ctx context.Context, vin dyn.Value) *schema.ResourceGrants { - grants, ok := vin.Get("grants").AsSequence() + grants, ok := vin.GetTODO("grants").AsSequence() if !ok || len(grants) == 0 { return nil } resource := &schema.ResourceGrants{} for _, permission := range grants { - principal, _ := permission.Get("principal").AsString() - v, _ := permission.Get("privileges").AsSequence() + principal, _ := permission.GetTODO("principal").AsString() + v, _ := permission.GetTODO("privileges").AsSequence() // Turn privileges into a slice of strings. var privileges []string diff --git a/bundle/deploy/terraform/tfdyn/convert_permissions.go b/bundle/deploy/terraform/tfdyn/convert_permissions.go index 99e8d29737..acf7e5e82d 100644 --- a/bundle/deploy/terraform/tfdyn/convert_permissions.go +++ b/bundle/deploy/terraform/tfdyn/convert_permissions.go @@ -8,17 +8,17 @@ import ( ) func convertPermissionsResource(ctx context.Context, vin dyn.Value) *schema.ResourcePermissions { - permissions, ok := vin.Get("permissions").AsSequence() + permissions, ok := vin.GetTODO("permissions").AsSequence() if !ok || len(permissions) == 0 { return nil } resource := &schema.ResourcePermissions{} for _, permission := range permissions { - level, _ := permission.Get("level").AsString() - userName, _ := permission.Get("user_name").AsString() - groupName, _ := permission.Get("group_name").AsString() - servicePrincipalName, _ := permission.Get("service_principal_name").AsString() + level, _ := permission.GetTODO("level").AsString() + userName, _ := permission.GetTODO("user_name").AsString() + groupName, _ := permission.GetTODO("group_name").AsString() + servicePrincipalName, _ := permission.GetTODO("service_principal_name").AsString() resource.AccessControl = append(resource.AccessControl, schema.ResourcePermissionsAccessControl{ PermissionLevel: level, diff --git a/bundle/permissions/filter.go b/bundle/permissions/filter.go index 6d39630c81..e2bffa09f7 100644 --- a/bundle/permissions/filter.go +++ b/bundle/permissions/filter.go @@ -44,13 +44,13 @@ func filter(currentUser string) dyn.WalkValueFunc { } // Filter if the user_name matches the current user - userName, ok := v.Get("user_name").AsString() + userName, ok := v.GetTODO("user_name").AsString() if ok && userName == currentUser { return v, dyn.ErrDrop } // Filter if the service_principal_name matches the current user - servicePrincipalName, ok := v.Get("service_principal_name").AsString() + servicePrincipalName, ok := v.GetTODO("service_principal_name").AsString() if ok && servicePrincipalName == currentUser { return v, dyn.ErrDrop } diff --git a/libs/dyn/convert/from_typed.go b/libs/dyn/convert/from_typed.go index b57d52be80..497b7d443a 100644 --- a/libs/dyn/convert/from_typed.go +++ b/libs/dyn/convert/from_typed.go @@ -172,7 +172,7 @@ func fromTypedSlice(src reflect.Value, ref dyn.Value) (dyn.Value, error) { out := make([]dyn.Value, src.Len()) for i := 0; i < src.Len(); i++ { v := src.Index(i) - refv := ref.Index(i) + refv := ref.IndexTODO(i) // Use nil reference if there is no reference for this index. if refv == dyn.InvalidValue { diff --git a/libs/dyn/convert/from_typed_test.go b/libs/dyn/convert/from_typed_test.go index f75470f420..044e72e71d 100644 --- a/libs/dyn/convert/from_typed_test.go +++ b/libs/dyn/convert/from_typed_test.go @@ -81,10 +81,10 @@ func TestFromTypedStructSetFieldsRetainLocationIfUnchanged(t *testing.T) { require.NoError(t, err) // Assert foo has retained its location. - assert.Equal(t, dyn.NewValue("bar", dyn.Location{File: "foo"}), nv.Get("foo")) + assert.Equal(t, dyn.NewValue("bar", dyn.Location{File: "foo"}), nv.GetTODO("foo")) // Assert bar lost its location (because it was overwritten). - assert.Equal(t, dyn.NewValue("qux", dyn.Location{}), nv.Get("bar")) + assert.Equal(t, dyn.NewValue("qux", dyn.Location{}), nv.GetTODO("bar")) } func TestFromTypedStringMapWithZeroValue(t *testing.T) { @@ -327,10 +327,10 @@ func TestFromTypedMapNonEmptyRetainLocationIfUnchanged(t *testing.T) { require.NoError(t, err) // Assert foo has retained its location. - assert.Equal(t, dyn.NewValue("bar", dyn.Location{File: "foo"}), nv.Get("foo")) + assert.Equal(t, dyn.NewValue("bar", dyn.Location{File: "foo"}), nv.GetTODO("foo")) // Assert bar lost its location (because it was overwritten). - assert.Equal(t, dyn.NewValue("qux", dyn.Location{}), nv.Get("bar")) + assert.Equal(t, dyn.NewValue("qux", dyn.Location{}), nv.GetTODO("bar")) } func TestFromTypedMapFieldWithZeroValue(t *testing.T) { @@ -402,10 +402,10 @@ func TestFromTypedSliceNonEmptyRetainLocationIfUnchanged(t *testing.T) { require.NoError(t, err) // Assert foo has retained its location. - assert.Equal(t, dyn.NewValue("foo", dyn.Location{File: "foo"}), nv.Index(0)) + assert.Equal(t, dyn.NewValue("foo", dyn.Location{File: "foo"}), nv.IndexTODO(0)) // Assert bar lost its location (because it was overwritten). - assert.Equal(t, dyn.NewValue("bar", dyn.Location{}), nv.Index(1)) + assert.Equal(t, dyn.NewValue("bar", dyn.Location{}), nv.IndexTODO(1)) } func TestFromTypedStringEmpty(t *testing.T) { diff --git a/libs/dyn/convert/normalize_test.go b/libs/dyn/convert/normalize_test.go index 843b4ea591..bf0e3e8d8b 100644 --- a/libs/dyn/convert/normalize_test.go +++ b/libs/dyn/convert/normalize_test.go @@ -68,7 +68,7 @@ func TestNormalizeStructUnknownField(t *testing.T) { assert.Equal(t, diag.Diagnostic{ Severity: diag.Warning, Summary: `unknown field: bar`, - Location: vin.Get("foo").Location(), + Location: vin.GetTODO("foo").Location(), Path: dyn.EmptyPath, }, err[0]) @@ -102,7 +102,7 @@ func TestNormalizeStructError(t *testing.T) { assert.Equal(t, diag.Diagnostic{ Severity: diag.Warning, Summary: `expected map, found string`, - Location: vin.Get("foo").Location(), + Location: vin.GetTODO("foo").Location(), Path: dyn.EmptyPath, }, err[0]) } diff --git a/libs/dyn/merge/elements_by_key.go b/libs/dyn/merge/elements_by_key.go index da20ee8497..0234ffaef9 100644 --- a/libs/dyn/merge/elements_by_key.go +++ b/libs/dyn/merge/elements_by_key.go @@ -21,7 +21,7 @@ func (e elementsByKey) Map(_ dyn.Path, v dyn.Value) (dyn.Value, error) { // Iterate in natural order. For a given key, we first see the // base definition and merge instances that come after it. for i := range elements { - kv := elements[i].Get(e.key) + kv := elements[i].GetTODO(e.key) key := e.keyFunc(kv) // Register element with key if not yet seen before. diff --git a/libs/dyn/merge/elements_by_key_test.go b/libs/dyn/merge/elements_by_key_test.go index ef316cc666..1b75e23139 100644 --- a/libs/dyn/merge/elements_by_key_test.go +++ b/libs/dyn/merge/elements_by_key_test.go @@ -36,14 +36,14 @@ func TestElementByKey(t *testing.T) { require.NoError(t, err) assert.Len(t, vout.MustSequence(), 2) assert.Equal(t, - vout.Index(0).AsAny(), + vout.IndexTODO(0).AsAny(), map[string]any{ "key": "foo", "value": 44, }, ) assert.Equal(t, - vout.Index(1).AsAny(), + vout.IndexTODO(1).AsAny(), map[string]any{ "key": "bar", "value": 43, diff --git a/libs/dyn/value.go b/libs/dyn/value.go index 3d62ea1f51..ce73713cc8 100644 --- a/libs/dyn/value.go +++ b/libs/dyn/value.go @@ -107,7 +107,7 @@ func (v Value) AsAny() any { } } -func (v Value) Get(key string) Value { +func (v Value) GetTODO(key string) Value { m, ok := v.AsMap() if !ok { return InvalidValue @@ -121,7 +121,7 @@ func (v Value) Get(key string) Value { return vv } -func (v Value) Index(i int) Value { +func (v Value) IndexTODO(i int) Value { s, ok := v.v.([]Value) if !ok { return InvalidValue diff --git a/libs/dyn/visit_map_test.go b/libs/dyn/visit_map_test.go index df6bad4965..ee22e8e4e8 100644 --- a/libs/dyn/visit_map_test.go +++ b/libs/dyn/visit_map_test.go @@ -94,7 +94,7 @@ func TestMapFuncOnMapWithEmptySequence(t *testing.T) { return variants[j], nil }) assert.NoError(t, err) - assert.Equal(t, variants[j], vout.Get("key")) + assert.Equal(t, variants[j], vout.GetTODO("key")) } } } @@ -160,7 +160,7 @@ func TestMapFuncOnSequenceWithEmptySequence(t *testing.T) { return variants[j], nil }) assert.NoError(t, err) - assert.Equal(t, variants[j], vout.Index(0)) + assert.Equal(t, variants[j], vout.IndexTODO(0)) } } } diff --git a/libs/dyn/yamlloader/yaml_anchor_test.go b/libs/dyn/yamlloader/yaml_anchor_test.go index 29ce69f0ac..186eba3235 100644 --- a/libs/dyn/yamlloader/yaml_anchor_test.go +++ b/libs/dyn/yamlloader/yaml_anchor_test.go @@ -12,11 +12,11 @@ func TestYAMLAnchor01(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - assert.True(t, self.Get("defaults").IsAnchor()) - assert.False(t, self.Get("shirt1").IsAnchor()) - assert.False(t, self.Get("shirt2").IsAnchor()) + assert.True(t, self.GetTODO("defaults").IsAnchor()) + assert.False(t, self.GetTODO("shirt1").IsAnchor()) + assert.False(t, self.GetTODO("shirt2").IsAnchor()) - pattern := self.Get("shirt1").Get("pattern") + pattern := self.GetTODO("shirt1").GetTODO("pattern") assert.Equal(t, "striped", pattern.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 8, Column: 12}, pattern.Location()) } @@ -26,15 +26,15 @@ func TestYAMLAnchor02(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - color := self.Get("shirt").Get("color") + color := self.GetTODO("shirt").GetTODO("color") assert.Equal(t, "red", color.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 10}, color.Location()) - primary := self.Get("shirt").Get("primary") + primary := self.GetTODO("shirt").GetTODO("primary") assert.Equal(t, "cotton", primary.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 8, Column: 12}, primary.Location()) - pattern := self.Get("shirt").Get("pattern") + pattern := self.GetTODO("shirt").GetTODO("pattern") assert.Equal(t, "striped", pattern.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 13, Column: 12}, pattern.Location()) } @@ -45,7 +45,7 @@ func TestYAMLAnchor03(t *testing.T) { assert.NotEqual(t, dyn.NilValue, self) // Assert the override took place. - blue := self.Get("shirt").Get("color") + blue := self.GetTODO("shirt").GetTODO("color") assert.Equal(t, "blue", blue.AsAny()) assert.Equal(t, file, blue.Location().File) assert.Equal(t, 10, blue.Location().Line) @@ -57,11 +57,11 @@ func TestYAMLAnchor04(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - p1 := self.Get("person1").Get("address").Get("city") + p1 := self.GetTODO("person1").GetTODO("address").GetTODO("city") assert.Equal(t, "San Francisco", p1.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 9}, p1.Location()) - p2 := self.Get("person2").Get("address").Get("city") + p2 := self.GetTODO("person2").GetTODO("address").GetTODO("city") assert.Equal(t, "Los Angeles", p2.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 16, Column: 11}, p2.Location()) } @@ -71,11 +71,11 @@ func TestYAMLAnchor05(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - features := self.Get("phone1").Get("features") - assert.Equal(t, "wifi", features.Index(0).AsAny()) - assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 5}, features.Index(0).Location()) - assert.Equal(t, "bluetooth", features.Index(1).AsAny()) - assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 5}, features.Index(1).Location()) + features := self.GetTODO("phone1").GetTODO("features") + assert.Equal(t, "wifi", features.IndexTODO(0).AsAny()) + assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 5}, features.IndexTODO(0).Location()) + assert.Equal(t, "bluetooth", features.IndexTODO(1).AsAny()) + assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 5}, features.IndexTODO(1).Location()) } func TestYAMLAnchor06(t *testing.T) { @@ -83,7 +83,7 @@ func TestYAMLAnchor06(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - greeting := self.Get("greeting1") + greeting := self.GetTODO("greeting1") assert.Equal(t, "Hello, World!", greeting.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 2, Column: 16}, greeting.Location()) } @@ -93,11 +93,11 @@ func TestYAMLAnchor07(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - name := self.Get("person1").Get("name") + name := self.GetTODO("person1").GetTODO("name") assert.Equal(t, "Alice", name.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 9}, name.Location()) - age := self.Get("person1").Get("age") + age := self.GetTODO("person1").GetTODO("age") assert.Equal(t, 25, age.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 2, Column: 13}, age.Location()) } @@ -107,11 +107,11 @@ func TestYAMLAnchor08(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - username := self.Get("user1").Get("username") + username := self.GetTODO("user1").GetTODO("username") assert.Equal(t, "user1", username.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 13}, username.Location()) - active := self.Get("user1").Get("active") + active := self.GetTODO("user1").GetTODO("active") assert.Equal(t, true, active.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 2, Column: 11}, active.Location()) } diff --git a/libs/dyn/yamlloader/yaml_mix_test.go b/libs/dyn/yamlloader/yaml_mix_test.go index 55ded6bafc..21cd5a8a95 100644 --- a/libs/dyn/yamlloader/yaml_mix_test.go +++ b/libs/dyn/yamlloader/yaml_mix_test.go @@ -12,8 +12,8 @@ func TestYAMLMix01(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - assert.True(t, self.Get("base_address").IsAnchor()) - assert.False(t, self.Get("office_address").IsAnchor()) + assert.True(t, self.GetTODO("base_address").IsAnchor()) + assert.False(t, self.GetTODO("office_address").IsAnchor()) } func TestYAMLMix02(t *testing.T) { @@ -21,6 +21,6 @@ func TestYAMLMix02(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - assert.True(t, self.Get("base_colors").IsAnchor()) - assert.False(t, self.Get("theme").IsAnchor()) + assert.True(t, self.GetTODO("base_colors").IsAnchor()) + assert.False(t, self.GetTODO("theme").IsAnchor()) } From 874e5a0f85d9901d5ff6dfebca7c2d46f2a98b52 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 19 Jun 2024 15:40:58 +0200 Subject: [PATCH 3/5] Rename back to original --- bundle/config/mutator/environments_compat.go | 4 +- bundle/config/root.go | 18 ++++----- .../deploy/terraform/tfdyn/convert_grants.go | 6 +-- .../terraform/tfdyn/convert_permissions.go | 10 ++--- bundle/permissions/filter.go | 4 +- libs/dyn/convert/from_typed.go | 2 +- libs/dyn/convert/from_typed_test.go | 12 +++--- libs/dyn/convert/normalize_test.go | 4 +- libs/dyn/merge/elements_by_key.go | 2 +- libs/dyn/merge/elements_by_key_test.go | 4 +- libs/dyn/value.go | 4 +- libs/dyn/visit_map_test.go | 4 +- libs/dyn/yamlloader/yaml_anchor_test.go | 40 +++++++++---------- libs/dyn/yamlloader/yaml_mix_test.go | 8 ++-- 14 files changed, 61 insertions(+), 61 deletions(-) diff --git a/bundle/config/mutator/environments_compat.go b/bundle/config/mutator/environments_compat.go index 955b30c10d..053fd2e361 100644 --- a/bundle/config/mutator/environments_compat.go +++ b/bundle/config/mutator/environments_compat.go @@ -28,8 +28,8 @@ func (m *environmentsToTargets) Apply(ctx context.Context, b *bundle.Bundle) dia // The "environments" key is set; validate and rewrite it to "targets". err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) { - environments := v.GetTODO("environments") - targets := v.GetTODO("targets") + environments := v.Get("environments") + targets := v.Get("targets") // Return an error if both "environments" and "targets" are set. if environments != dyn.InvalidValue && targets != dyn.InvalidValue { diff --git a/bundle/config/root.go b/bundle/config/root.go index 56c25e0766..8dd06630df 100644 --- a/bundle/config/root.go +++ b/bundle/config/root.go @@ -337,7 +337,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Merge `run_as`. This field must be overwritten if set, not merged. - if v := target.GetTODO("run_as"); v != dyn.InvalidValue { + if v := target.Get("run_as"); v != dyn.InvalidValue { root, err = dyn.Set(root, "run_as", v) if err != nil { return err @@ -345,7 +345,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Below, we're setting fields on the bundle key, so make sure it exists. - if root.GetTODO("bundle") == dyn.InvalidValue { + if root.Get("bundle") == dyn.InvalidValue { root, err = dyn.Set(root, "bundle", dyn.NewValue(map[string]dyn.Value{}, dyn.Location{})) if err != nil { return err @@ -353,7 +353,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Merge `mode`. This field must be overwritten if set, not merged. - if v := target.GetTODO("mode"); v != dyn.InvalidValue { + if v := target.Get("mode"); v != dyn.InvalidValue { root, err = dyn.SetByPath(root, dyn.NewPath(dyn.Key("bundle"), dyn.Key("mode")), v) if err != nil { return err @@ -361,7 +361,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Merge `compute_id`. This field must be overwritten if set, not merged. - if v := target.GetTODO("compute_id"); v != dyn.InvalidValue { + if v := target.Get("compute_id"); v != dyn.InvalidValue { root, err = dyn.SetByPath(root, dyn.NewPath(dyn.Key("bundle"), dyn.Key("compute_id")), v) if err != nil { return err @@ -369,7 +369,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // Merge `git`. - if v := target.GetTODO("git"); v != dyn.InvalidValue { + if v := target.Get("git"); v != dyn.InvalidValue { ref, err := dyn.GetByPath(root, dyn.NewPath(dyn.Key("bundle"), dyn.Key("git"))) if err != nil { ref = dyn.NewValue(map[string]dyn.Value{}, dyn.Location{}) @@ -382,7 +382,7 @@ func (r *Root) MergeTargetOverrides(name string) error { } // If the branch was overridden, we need to clear the inferred flag. - if branch := v.GetTODO("branch"); branch != dyn.InvalidValue { + if branch := v.Get("branch"); branch != dyn.InvalidValue { out, err = dyn.SetByPath(out, dyn.NewPath(dyn.Key("inferred")), dyn.NewValue(false, dyn.Location{})) if err != nil { return err @@ -410,7 +410,7 @@ func rewriteShorthands(v dyn.Value) (dyn.Value, error) { // For each target, rewrite the variables block. return dyn.Map(v, "targets", dyn.Foreach(func(_ dyn.Path, target dyn.Value) (dyn.Value, error) { // Confirm it has a variables block. - if target.GetTODO("variables") == dyn.InvalidValue { + if target.Get("variables") == dyn.InvalidValue { return target, nil } @@ -440,7 +440,7 @@ func validateVariableOverrides(root, target dyn.Value) (err error) { var tv map[string]variable.Variable // Collect variables from the root. - if v := root.GetTODO("variables"); v != dyn.InvalidValue { + if v := root.Get("variables"); v != dyn.InvalidValue { err = convert.ToTyped(&rv, v) if err != nil { return fmt.Errorf("unable to collect variables from root: %w", err) @@ -448,7 +448,7 @@ func validateVariableOverrides(root, target dyn.Value) (err error) { } // Collect variables from the target. - if v := target.GetTODO("variables"); v != dyn.InvalidValue { + if v := target.Get("variables"); v != dyn.InvalidValue { err = convert.ToTyped(&tv, v) if err != nil { return fmt.Errorf("unable to collect variables from target: %w", err) diff --git a/bundle/deploy/terraform/tfdyn/convert_grants.go b/bundle/deploy/terraform/tfdyn/convert_grants.go index 8e3f66f68b..1ddd99dd70 100644 --- a/bundle/deploy/terraform/tfdyn/convert_grants.go +++ b/bundle/deploy/terraform/tfdyn/convert_grants.go @@ -8,15 +8,15 @@ import ( ) func convertGrantsResource(ctx context.Context, vin dyn.Value) *schema.ResourceGrants { - grants, ok := vin.GetTODO("grants").AsSequence() + grants, ok := vin.Get("grants").AsSequence() if !ok || len(grants) == 0 { return nil } resource := &schema.ResourceGrants{} for _, permission := range grants { - principal, _ := permission.GetTODO("principal").AsString() - v, _ := permission.GetTODO("privileges").AsSequence() + principal, _ := permission.Get("principal").AsString() + v, _ := permission.Get("privileges").AsSequence() // Turn privileges into a slice of strings. var privileges []string diff --git a/bundle/deploy/terraform/tfdyn/convert_permissions.go b/bundle/deploy/terraform/tfdyn/convert_permissions.go index acf7e5e82d..99e8d29737 100644 --- a/bundle/deploy/terraform/tfdyn/convert_permissions.go +++ b/bundle/deploy/terraform/tfdyn/convert_permissions.go @@ -8,17 +8,17 @@ import ( ) func convertPermissionsResource(ctx context.Context, vin dyn.Value) *schema.ResourcePermissions { - permissions, ok := vin.GetTODO("permissions").AsSequence() + permissions, ok := vin.Get("permissions").AsSequence() if !ok || len(permissions) == 0 { return nil } resource := &schema.ResourcePermissions{} for _, permission := range permissions { - level, _ := permission.GetTODO("level").AsString() - userName, _ := permission.GetTODO("user_name").AsString() - groupName, _ := permission.GetTODO("group_name").AsString() - servicePrincipalName, _ := permission.GetTODO("service_principal_name").AsString() + level, _ := permission.Get("level").AsString() + userName, _ := permission.Get("user_name").AsString() + groupName, _ := permission.Get("group_name").AsString() + servicePrincipalName, _ := permission.Get("service_principal_name").AsString() resource.AccessControl = append(resource.AccessControl, schema.ResourcePermissionsAccessControl{ PermissionLevel: level, diff --git a/bundle/permissions/filter.go b/bundle/permissions/filter.go index e2bffa09f7..6d39630c81 100644 --- a/bundle/permissions/filter.go +++ b/bundle/permissions/filter.go @@ -44,13 +44,13 @@ func filter(currentUser string) dyn.WalkValueFunc { } // Filter if the user_name matches the current user - userName, ok := v.GetTODO("user_name").AsString() + userName, ok := v.Get("user_name").AsString() if ok && userName == currentUser { return v, dyn.ErrDrop } // Filter if the service_principal_name matches the current user - servicePrincipalName, ok := v.GetTODO("service_principal_name").AsString() + servicePrincipalName, ok := v.Get("service_principal_name").AsString() if ok && servicePrincipalName == currentUser { return v, dyn.ErrDrop } diff --git a/libs/dyn/convert/from_typed.go b/libs/dyn/convert/from_typed.go index 497b7d443a..b57d52be80 100644 --- a/libs/dyn/convert/from_typed.go +++ b/libs/dyn/convert/from_typed.go @@ -172,7 +172,7 @@ func fromTypedSlice(src reflect.Value, ref dyn.Value) (dyn.Value, error) { out := make([]dyn.Value, src.Len()) for i := 0; i < src.Len(); i++ { v := src.Index(i) - refv := ref.IndexTODO(i) + refv := ref.Index(i) // Use nil reference if there is no reference for this index. if refv == dyn.InvalidValue { diff --git a/libs/dyn/convert/from_typed_test.go b/libs/dyn/convert/from_typed_test.go index 044e72e71d..f75470f420 100644 --- a/libs/dyn/convert/from_typed_test.go +++ b/libs/dyn/convert/from_typed_test.go @@ -81,10 +81,10 @@ func TestFromTypedStructSetFieldsRetainLocationIfUnchanged(t *testing.T) { require.NoError(t, err) // Assert foo has retained its location. - assert.Equal(t, dyn.NewValue("bar", dyn.Location{File: "foo"}), nv.GetTODO("foo")) + assert.Equal(t, dyn.NewValue("bar", dyn.Location{File: "foo"}), nv.Get("foo")) // Assert bar lost its location (because it was overwritten). - assert.Equal(t, dyn.NewValue("qux", dyn.Location{}), nv.GetTODO("bar")) + assert.Equal(t, dyn.NewValue("qux", dyn.Location{}), nv.Get("bar")) } func TestFromTypedStringMapWithZeroValue(t *testing.T) { @@ -327,10 +327,10 @@ func TestFromTypedMapNonEmptyRetainLocationIfUnchanged(t *testing.T) { require.NoError(t, err) // Assert foo has retained its location. - assert.Equal(t, dyn.NewValue("bar", dyn.Location{File: "foo"}), nv.GetTODO("foo")) + assert.Equal(t, dyn.NewValue("bar", dyn.Location{File: "foo"}), nv.Get("foo")) // Assert bar lost its location (because it was overwritten). - assert.Equal(t, dyn.NewValue("qux", dyn.Location{}), nv.GetTODO("bar")) + assert.Equal(t, dyn.NewValue("qux", dyn.Location{}), nv.Get("bar")) } func TestFromTypedMapFieldWithZeroValue(t *testing.T) { @@ -402,10 +402,10 @@ func TestFromTypedSliceNonEmptyRetainLocationIfUnchanged(t *testing.T) { require.NoError(t, err) // Assert foo has retained its location. - assert.Equal(t, dyn.NewValue("foo", dyn.Location{File: "foo"}), nv.IndexTODO(0)) + assert.Equal(t, dyn.NewValue("foo", dyn.Location{File: "foo"}), nv.Index(0)) // Assert bar lost its location (because it was overwritten). - assert.Equal(t, dyn.NewValue("bar", dyn.Location{}), nv.IndexTODO(1)) + assert.Equal(t, dyn.NewValue("bar", dyn.Location{}), nv.Index(1)) } func TestFromTypedStringEmpty(t *testing.T) { diff --git a/libs/dyn/convert/normalize_test.go b/libs/dyn/convert/normalize_test.go index bf0e3e8d8b..843b4ea591 100644 --- a/libs/dyn/convert/normalize_test.go +++ b/libs/dyn/convert/normalize_test.go @@ -68,7 +68,7 @@ func TestNormalizeStructUnknownField(t *testing.T) { assert.Equal(t, diag.Diagnostic{ Severity: diag.Warning, Summary: `unknown field: bar`, - Location: vin.GetTODO("foo").Location(), + Location: vin.Get("foo").Location(), Path: dyn.EmptyPath, }, err[0]) @@ -102,7 +102,7 @@ func TestNormalizeStructError(t *testing.T) { assert.Equal(t, diag.Diagnostic{ Severity: diag.Warning, Summary: `expected map, found string`, - Location: vin.GetTODO("foo").Location(), + Location: vin.Get("foo").Location(), Path: dyn.EmptyPath, }, err[0]) } diff --git a/libs/dyn/merge/elements_by_key.go b/libs/dyn/merge/elements_by_key.go index 0234ffaef9..da20ee8497 100644 --- a/libs/dyn/merge/elements_by_key.go +++ b/libs/dyn/merge/elements_by_key.go @@ -21,7 +21,7 @@ func (e elementsByKey) Map(_ dyn.Path, v dyn.Value) (dyn.Value, error) { // Iterate in natural order. For a given key, we first see the // base definition and merge instances that come after it. for i := range elements { - kv := elements[i].GetTODO(e.key) + kv := elements[i].Get(e.key) key := e.keyFunc(kv) // Register element with key if not yet seen before. diff --git a/libs/dyn/merge/elements_by_key_test.go b/libs/dyn/merge/elements_by_key_test.go index 1b75e23139..ef316cc666 100644 --- a/libs/dyn/merge/elements_by_key_test.go +++ b/libs/dyn/merge/elements_by_key_test.go @@ -36,14 +36,14 @@ func TestElementByKey(t *testing.T) { require.NoError(t, err) assert.Len(t, vout.MustSequence(), 2) assert.Equal(t, - vout.IndexTODO(0).AsAny(), + vout.Index(0).AsAny(), map[string]any{ "key": "foo", "value": 44, }, ) assert.Equal(t, - vout.IndexTODO(1).AsAny(), + vout.Index(1).AsAny(), map[string]any{ "key": "bar", "value": 43, diff --git a/libs/dyn/value.go b/libs/dyn/value.go index ce73713cc8..3d62ea1f51 100644 --- a/libs/dyn/value.go +++ b/libs/dyn/value.go @@ -107,7 +107,7 @@ func (v Value) AsAny() any { } } -func (v Value) GetTODO(key string) Value { +func (v Value) Get(key string) Value { m, ok := v.AsMap() if !ok { return InvalidValue @@ -121,7 +121,7 @@ func (v Value) GetTODO(key string) Value { return vv } -func (v Value) IndexTODO(i int) Value { +func (v Value) Index(i int) Value { s, ok := v.v.([]Value) if !ok { return InvalidValue diff --git a/libs/dyn/visit_map_test.go b/libs/dyn/visit_map_test.go index ee22e8e4e8..df6bad4965 100644 --- a/libs/dyn/visit_map_test.go +++ b/libs/dyn/visit_map_test.go @@ -94,7 +94,7 @@ func TestMapFuncOnMapWithEmptySequence(t *testing.T) { return variants[j], nil }) assert.NoError(t, err) - assert.Equal(t, variants[j], vout.GetTODO("key")) + assert.Equal(t, variants[j], vout.Get("key")) } } } @@ -160,7 +160,7 @@ func TestMapFuncOnSequenceWithEmptySequence(t *testing.T) { return variants[j], nil }) assert.NoError(t, err) - assert.Equal(t, variants[j], vout.IndexTODO(0)) + assert.Equal(t, variants[j], vout.Index(0)) } } } diff --git a/libs/dyn/yamlloader/yaml_anchor_test.go b/libs/dyn/yamlloader/yaml_anchor_test.go index 186eba3235..29ce69f0ac 100644 --- a/libs/dyn/yamlloader/yaml_anchor_test.go +++ b/libs/dyn/yamlloader/yaml_anchor_test.go @@ -12,11 +12,11 @@ func TestYAMLAnchor01(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - assert.True(t, self.GetTODO("defaults").IsAnchor()) - assert.False(t, self.GetTODO("shirt1").IsAnchor()) - assert.False(t, self.GetTODO("shirt2").IsAnchor()) + assert.True(t, self.Get("defaults").IsAnchor()) + assert.False(t, self.Get("shirt1").IsAnchor()) + assert.False(t, self.Get("shirt2").IsAnchor()) - pattern := self.GetTODO("shirt1").GetTODO("pattern") + pattern := self.Get("shirt1").Get("pattern") assert.Equal(t, "striped", pattern.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 8, Column: 12}, pattern.Location()) } @@ -26,15 +26,15 @@ func TestYAMLAnchor02(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - color := self.GetTODO("shirt").GetTODO("color") + color := self.Get("shirt").Get("color") assert.Equal(t, "red", color.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 10}, color.Location()) - primary := self.GetTODO("shirt").GetTODO("primary") + primary := self.Get("shirt").Get("primary") assert.Equal(t, "cotton", primary.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 8, Column: 12}, primary.Location()) - pattern := self.GetTODO("shirt").GetTODO("pattern") + pattern := self.Get("shirt").Get("pattern") assert.Equal(t, "striped", pattern.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 13, Column: 12}, pattern.Location()) } @@ -45,7 +45,7 @@ func TestYAMLAnchor03(t *testing.T) { assert.NotEqual(t, dyn.NilValue, self) // Assert the override took place. - blue := self.GetTODO("shirt").GetTODO("color") + blue := self.Get("shirt").Get("color") assert.Equal(t, "blue", blue.AsAny()) assert.Equal(t, file, blue.Location().File) assert.Equal(t, 10, blue.Location().Line) @@ -57,11 +57,11 @@ func TestYAMLAnchor04(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - p1 := self.GetTODO("person1").GetTODO("address").GetTODO("city") + p1 := self.Get("person1").Get("address").Get("city") assert.Equal(t, "San Francisco", p1.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 9}, p1.Location()) - p2 := self.GetTODO("person2").GetTODO("address").GetTODO("city") + p2 := self.Get("person2").Get("address").Get("city") assert.Equal(t, "Los Angeles", p2.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 16, Column: 11}, p2.Location()) } @@ -71,11 +71,11 @@ func TestYAMLAnchor05(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - features := self.GetTODO("phone1").GetTODO("features") - assert.Equal(t, "wifi", features.IndexTODO(0).AsAny()) - assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 5}, features.IndexTODO(0).Location()) - assert.Equal(t, "bluetooth", features.IndexTODO(1).AsAny()) - assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 5}, features.IndexTODO(1).Location()) + features := self.Get("phone1").Get("features") + assert.Equal(t, "wifi", features.Index(0).AsAny()) + assert.Equal(t, dyn.Location{File: file, Line: 4, Column: 5}, features.Index(0).Location()) + assert.Equal(t, "bluetooth", features.Index(1).AsAny()) + assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 5}, features.Index(1).Location()) } func TestYAMLAnchor06(t *testing.T) { @@ -83,7 +83,7 @@ func TestYAMLAnchor06(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - greeting := self.GetTODO("greeting1") + greeting := self.Get("greeting1") assert.Equal(t, "Hello, World!", greeting.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 2, Column: 16}, greeting.Location()) } @@ -93,11 +93,11 @@ func TestYAMLAnchor07(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - name := self.GetTODO("person1").GetTODO("name") + name := self.Get("person1").Get("name") assert.Equal(t, "Alice", name.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 9}, name.Location()) - age := self.GetTODO("person1").GetTODO("age") + age := self.Get("person1").Get("age") assert.Equal(t, 25, age.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 2, Column: 13}, age.Location()) } @@ -107,11 +107,11 @@ func TestYAMLAnchor08(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - username := self.GetTODO("user1").GetTODO("username") + username := self.Get("user1").Get("username") assert.Equal(t, "user1", username.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 5, Column: 13}, username.Location()) - active := self.GetTODO("user1").GetTODO("active") + active := self.Get("user1").Get("active") assert.Equal(t, true, active.AsAny()) assert.Equal(t, dyn.Location{File: file, Line: 2, Column: 11}, active.Location()) } diff --git a/libs/dyn/yamlloader/yaml_mix_test.go b/libs/dyn/yamlloader/yaml_mix_test.go index 21cd5a8a95..55ded6bafc 100644 --- a/libs/dyn/yamlloader/yaml_mix_test.go +++ b/libs/dyn/yamlloader/yaml_mix_test.go @@ -12,8 +12,8 @@ func TestYAMLMix01(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - assert.True(t, self.GetTODO("base_address").IsAnchor()) - assert.False(t, self.GetTODO("office_address").IsAnchor()) + assert.True(t, self.Get("base_address").IsAnchor()) + assert.False(t, self.Get("office_address").IsAnchor()) } func TestYAMLMix02(t *testing.T) { @@ -21,6 +21,6 @@ func TestYAMLMix02(t *testing.T) { self := loadYAML(t, file) assert.NotEqual(t, dyn.NilValue, self) - assert.True(t, self.GetTODO("base_colors").IsAnchor()) - assert.False(t, self.GetTODO("theme").IsAnchor()) + assert.True(t, self.Get("base_colors").IsAnchor()) + assert.False(t, self.Get("theme").IsAnchor()) } From aaa23ff521f4e5b8a3cdef1aa36ba5bf97f2c98a Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 19 Jun 2024 15:50:23 +0200 Subject: [PATCH 4/5] Update test to use dyn.InvalidValue --- libs/dyn/value_underlying_test.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/libs/dyn/value_underlying_test.go b/libs/dyn/value_underlying_test.go index 9878cfaf9d..b7850132ab 100644 --- a/libs/dyn/value_underlying_test.go +++ b/libs/dyn/value_underlying_test.go @@ -18,7 +18,7 @@ func TestValueUnderlyingMap(t *testing.T) { vv1, ok := v.AsMap() assert.True(t, ok) - _, ok = dyn.NilValue.AsMap() + _, ok = dyn.InvalidValue.AsMap() assert.False(t, ok) vv2 := v.MustMap() @@ -26,7 +26,7 @@ func TestValueUnderlyingMap(t *testing.T) { // Test panic. assert.PanicsWithValue(t, "expected kind map, got nil", func() { - dyn.NilValue.MustMap() + dyn.InvalidValue.MustMap() }) } @@ -40,7 +40,7 @@ func TestValueUnderlyingSequence(t *testing.T) { vv1, ok := v.AsSequence() assert.True(t, ok) - _, ok = dyn.NilValue.AsSequence() + _, ok = dyn.InvalidValue.AsSequence() assert.False(t, ok) vv2 := v.MustSequence() @@ -48,7 +48,7 @@ func TestValueUnderlyingSequence(t *testing.T) { // Test panic. assert.PanicsWithValue(t, "expected kind sequence, got nil", func() { - dyn.NilValue.MustSequence() + dyn.InvalidValue.MustSequence() }) } @@ -58,7 +58,7 @@ func TestValueUnderlyingString(t *testing.T) { vv1, ok := v.AsString() assert.True(t, ok) - _, ok = dyn.NilValue.AsString() + _, ok = dyn.InvalidValue.AsString() assert.False(t, ok) vv2 := v.MustString() @@ -66,7 +66,7 @@ func TestValueUnderlyingString(t *testing.T) { // Test panic. assert.PanicsWithValue(t, "expected kind string, got nil", func() { - dyn.NilValue.MustString() + dyn.InvalidValue.MustString() }) } @@ -76,7 +76,7 @@ func TestValueUnderlyingBool(t *testing.T) { vv1, ok := v.AsBool() assert.True(t, ok) - _, ok = dyn.NilValue.AsBool() + _, ok = dyn.InvalidValue.AsBool() assert.False(t, ok) vv2 := v.MustBool() @@ -84,7 +84,7 @@ func TestValueUnderlyingBool(t *testing.T) { // Test panic. assert.PanicsWithValue(t, "expected kind bool, got nil", func() { - dyn.NilValue.MustBool() + dyn.InvalidValue.MustBool() }) } @@ -94,7 +94,7 @@ func TestValueUnderlyingInt(t *testing.T) { vv1, ok := v.AsInt() assert.True(t, ok) - _, ok = dyn.NilValue.AsInt() + _, ok = dyn.InvalidValue.AsInt() assert.False(t, ok) vv2 := v.MustInt() @@ -102,7 +102,7 @@ func TestValueUnderlyingInt(t *testing.T) { // Test panic. assert.PanicsWithValue(t, "expected kind int, got nil", func() { - dyn.NilValue.MustInt() + dyn.InvalidValue.MustInt() }) // Test int32 type specifically. @@ -124,7 +124,7 @@ func TestValueUnderlyingFloat(t *testing.T) { vv1, ok := v.AsFloat() assert.True(t, ok) - _, ok = dyn.NilValue.AsFloat() + _, ok = dyn.InvalidValue.AsFloat() assert.False(t, ok) vv2 := v.MustFloat() @@ -132,7 +132,7 @@ func TestValueUnderlyingFloat(t *testing.T) { // Test panic. assert.PanicsWithValue(t, "expected kind float, got nil", func() { - dyn.NilValue.MustFloat() + dyn.InvalidValue.MustFloat() }) // Test float64 type specifically. @@ -148,7 +148,7 @@ func TestValueUnderlyingTime(t *testing.T) { vv1, ok := v.AsTime() assert.True(t, ok) - _, ok = dyn.NilValue.AsTime() + _, ok = dyn.InvalidValue.AsTime() assert.False(t, ok) vv2 := v.MustTime() @@ -156,6 +156,6 @@ func TestValueUnderlyingTime(t *testing.T) { // Test panic. assert.PanicsWithValue(t, "expected kind time, got nil", func() { - dyn.NilValue.MustTime() + dyn.InvalidValue.MustTime() }) } From dbc725469824e9088048a5248358cbd657098ab6 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 19 Jun 2024 16:01:17 +0200 Subject: [PATCH 5/5] Update test --- libs/dyn/value_underlying_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libs/dyn/value_underlying_test.go b/libs/dyn/value_underlying_test.go index b7850132ab..83cffb772d 100644 --- a/libs/dyn/value_underlying_test.go +++ b/libs/dyn/value_underlying_test.go @@ -25,7 +25,7 @@ func TestValueUnderlyingMap(t *testing.T) { assert.Equal(t, vv1, vv2) // Test panic. - assert.PanicsWithValue(t, "expected kind map, got nil", func() { + assert.PanicsWithValue(t, "expected kind map, got invalid", func() { dyn.InvalidValue.MustMap() }) } @@ -47,7 +47,7 @@ func TestValueUnderlyingSequence(t *testing.T) { assert.Equal(t, vv1, vv2) // Test panic. - assert.PanicsWithValue(t, "expected kind sequence, got nil", func() { + assert.PanicsWithValue(t, "expected kind sequence, got invalid", func() { dyn.InvalidValue.MustSequence() }) } @@ -65,7 +65,7 @@ func TestValueUnderlyingString(t *testing.T) { assert.Equal(t, vv1, vv2) // Test panic. - assert.PanicsWithValue(t, "expected kind string, got nil", func() { + assert.PanicsWithValue(t, "expected kind string, got invalid", func() { dyn.InvalidValue.MustString() }) } @@ -83,7 +83,7 @@ func TestValueUnderlyingBool(t *testing.T) { assert.Equal(t, vv1, vv2) // Test panic. - assert.PanicsWithValue(t, "expected kind bool, got nil", func() { + assert.PanicsWithValue(t, "expected kind bool, got invalid", func() { dyn.InvalidValue.MustBool() }) } @@ -101,7 +101,7 @@ func TestValueUnderlyingInt(t *testing.T) { assert.Equal(t, vv1, vv2) // Test panic. - assert.PanicsWithValue(t, "expected kind int, got nil", func() { + assert.PanicsWithValue(t, "expected kind int, got invalid", func() { dyn.InvalidValue.MustInt() }) @@ -131,7 +131,7 @@ func TestValueUnderlyingFloat(t *testing.T) { assert.Equal(t, vv1, vv2) // Test panic. - assert.PanicsWithValue(t, "expected kind float, got nil", func() { + assert.PanicsWithValue(t, "expected kind float, got invalid", func() { dyn.InvalidValue.MustFloat() }) @@ -155,7 +155,7 @@ func TestValueUnderlyingTime(t *testing.T) { assert.Equal(t, vv1, vv2) // Test panic. - assert.PanicsWithValue(t, "expected kind time, got nil", func() { + assert.PanicsWithValue(t, "expected kind time, got invalid", func() { dyn.InvalidValue.MustTime() }) }