From e790e8a034f50c69229bb4833d941fe638e84642 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Thu, 18 Jul 2024 18:09:51 +0200 Subject: [PATCH] Introduce new type for a slice of dyn.Location --- bundle/config/generate/job.go | 2 +- .../mutator/python/python_mutator_test.go | 12 +- bundle/internal/bundletest/location.go | 2 +- libs/dyn/convert/from_typed_test.go | 60 ++--- libs/dyn/convert/normalize_test.go | 48 ++-- libs/dyn/location.go | 2 + libs/dyn/merge/merge_test.go | 60 ++--- libs/dyn/merge/override_test.go | 218 +++++++++--------- libs/dyn/value_test.go | 4 +- libs/dyn/value_underlying_test.go | 4 +- libs/dyn/yamlloader/loader.go | 22 +- libs/dyn/yamlsaver/saver_test.go | 34 +-- libs/dyn/yamlsaver/utils.go | 2 +- libs/dyn/yamlsaver/utils_test.go | 8 +- 14 files changed, 240 insertions(+), 238 deletions(-) diff --git a/bundle/config/generate/job.go b/bundle/config/generate/job.go index 28bc864122..149ba52792 100644 --- a/bundle/config/generate/job.go +++ b/bundle/config/generate/job.go @@ -22,7 +22,7 @@ func ConvertJobToValue(job *jobs.Job) (dyn.Value, error) { tasks = append(tasks, v) } // We're using location lines to define the order of keys in exported YAML. - value["tasks"] = dyn.NewValue(tasks, []dyn.Location{{Line: jobOrder.Get("tasks")}}) + value["tasks"] = dyn.NewValue(tasks, dyn.Locations{{Line: jobOrder.Get("tasks")}}) } return yamlsaver.ConvertToMapValue(job.Settings, jobOrder, []string{"format", "new_cluster", "existing_cluster_id"}, value) diff --git a/bundle/config/mutator/python/python_mutator_test.go b/bundle/config/mutator/python/python_mutator_test.go index 588589831b..3bdb48f8d9 100644 --- a/bundle/config/mutator/python/python_mutator_test.go +++ b/bundle/config/mutator/python/python_mutator_test.go @@ -470,21 +470,21 @@ func TestCreateOverrideVisitor_omitempty(t *testing.T) { // this is not happening, but adding for completeness name: "undo delete of empty variables", path: dyn.MustPathFromString("variables"), - left: dyn.NewValue([]dyn.Value{}, []dyn.Location{location}), + left: dyn.NewValue([]dyn.Value{}, dyn.Locations{location}), expectedErr: merge.ErrOverrideUndoDelete, phases: allPhases, }, { name: "undo delete of empty job clusters", path: dyn.MustPathFromString("resources.jobs.job0.job_clusters"), - left: dyn.NewValue([]dyn.Value{}, []dyn.Location{location}), + left: dyn.NewValue([]dyn.Value{}, dyn.Locations{location}), expectedErr: merge.ErrOverrideUndoDelete, phases: allPhases, }, { name: "allow delete of non-empty job clusters", path: dyn.MustPathFromString("resources.jobs.job0.job_clusters"), - left: dyn.NewValue([]dyn.Value{dyn.NewValue("abc", []dyn.Location{location})}, []dyn.Location{location}), + left: dyn.NewValue([]dyn.Value{dyn.NewValue("abc", dyn.Locations{location})}, dyn.Locations{location}), expectedErr: nil, // deletions aren't allowed in 'load' phase phases: []phase{PythonMutatorPhaseInit}, @@ -492,14 +492,14 @@ func TestCreateOverrideVisitor_omitempty(t *testing.T) { { name: "undo delete of empty tags", path: dyn.MustPathFromString("resources.jobs.job0.tags"), - left: dyn.NewValue(map[string]dyn.Value{}, []dyn.Location{location}), + left: dyn.NewValue(map[string]dyn.Value{}, dyn.Locations{location}), expectedErr: merge.ErrOverrideUndoDelete, phases: allPhases, }, { name: "allow delete of non-empty tags", path: dyn.MustPathFromString("resources.jobs.job0.tags"), - left: dyn.NewValue(map[string]dyn.Value{"dev": dyn.NewValue("true", []dyn.Location{location})}, []dyn.Location{location}), + left: dyn.NewValue(map[string]dyn.Value{"dev": dyn.NewValue("true", dyn.Locations{location})}, dyn.Locations{location}), expectedErr: nil, // deletions aren't allowed in 'load' phase @@ -508,7 +508,7 @@ func TestCreateOverrideVisitor_omitempty(t *testing.T) { { name: "undo delete of nil", path: dyn.MustPathFromString("resources.jobs.job0.tags"), - left: dyn.NilValue.WithLocations([]dyn.Location{location}), + left: dyn.NilValue.WithLocations(dyn.Locations{location}), expectedErr: merge.ErrOverrideUndoDelete, phases: allPhases, }, diff --git a/bundle/internal/bundletest/location.go b/bundle/internal/bundletest/location.go index ebec43d30a..f40c5614f4 100644 --- a/bundle/internal/bundletest/location.go +++ b/bundle/internal/bundletest/location.go @@ -14,7 +14,7 @@ func SetLocation(b *bundle.Bundle, prefix string, filePath string) { return dyn.Walk(root, func(p dyn.Path, v dyn.Value) (dyn.Value, error) { // If the path has the given prefix, set the location. if p.HasPrefix(start) { - return v.WithLocations([]dyn.Location{{ + return v.WithLocations(dyn.Locations{{ File: filePath, }}), nil } diff --git a/libs/dyn/convert/from_typed_test.go b/libs/dyn/convert/from_typed_test.go index 0cddff3be0..7550fb1aca 100644 --- a/libs/dyn/convert/from_typed_test.go +++ b/libs/dyn/convert/from_typed_test.go @@ -115,16 +115,16 @@ func TestFromTypedStructSetFieldsRetainLocation(t *testing.T) { } ref := dyn.V(map[string]dyn.Value{ - "foo": dyn.NewValue("bar", []dyn.Location{{File: "foo"}}), - "bar": dyn.NewValue("baz", []dyn.Location{{File: "bar"}}), + "foo": dyn.NewValue("bar", dyn.Locations{{File: "foo"}}), + "bar": dyn.NewValue("baz", dyn.Locations{{File: "bar"}}), }) nv, err := FromTyped(src, ref) require.NoError(t, err) // Assert foo and bar have retained their location. - assert.Equal(t, dyn.NewValue("bar", []dyn.Location{{File: "foo"}}), nv.Get("foo")) - assert.Equal(t, dyn.NewValue("qux", []dyn.Location{{File: "bar"}}), nv.Get("bar")) + assert.Equal(t, dyn.NewValue("bar", dyn.Locations{{File: "foo"}}), nv.Get("foo")) + assert.Equal(t, dyn.NewValue("qux", dyn.Locations{{File: "bar"}}), nv.Get("bar")) } func TestFromTypedStringMapWithZeroValue(t *testing.T) { @@ -359,16 +359,16 @@ func TestFromTypedMapNonEmptyRetainLocation(t *testing.T) { } ref := dyn.V(map[string]dyn.Value{ - "foo": dyn.NewValue("bar", []dyn.Location{{File: "foo"}}), - "bar": dyn.NewValue("baz", []dyn.Location{{File: "bar"}}), + "foo": dyn.NewValue("bar", dyn.Locations{{File: "foo"}}), + "bar": dyn.NewValue("baz", dyn.Locations{{File: "bar"}}), }) nv, err := FromTyped(src, ref) require.NoError(t, err) // Assert foo and bar have retained their locations. - assert.Equal(t, dyn.NewValue("bar", []dyn.Location{{File: "foo"}}), nv.Get("foo")) - assert.Equal(t, dyn.NewValue("qux", []dyn.Location{{File: "bar"}}), nv.Get("bar")) + assert.Equal(t, dyn.NewValue("bar", dyn.Locations{{File: "foo"}}), nv.Get("foo")) + assert.Equal(t, dyn.NewValue("qux", dyn.Locations{{File: "bar"}}), nv.Get("bar")) } func TestFromTypedMapFieldWithZeroValue(t *testing.T) { @@ -432,16 +432,16 @@ func TestFromTypedSliceNonEmptyRetainLocation(t *testing.T) { } ref := dyn.V([]dyn.Value{ - dyn.NewValue("foo", []dyn.Location{{File: "foo"}}), - dyn.NewValue("bar", []dyn.Location{{File: "bar"}}), + dyn.NewValue("foo", dyn.Locations{{File: "foo"}}), + dyn.NewValue("bar", dyn.Locations{{File: "bar"}}), }) nv, err := FromTyped(src, ref) require.NoError(t, err) // Assert foo and bar have retained their locations. - assert.Equal(t, dyn.NewValue("foo", []dyn.Location{{File: "foo"}}), nv.Index(0)) - assert.Equal(t, dyn.NewValue("bar", []dyn.Location{{File: "bar"}}), nv.Index(1)) + assert.Equal(t, dyn.NewValue("foo", dyn.Locations{{File: "foo"}}), nv.Index(0)) + assert.Equal(t, dyn.NewValue("bar", dyn.Locations{{File: "bar"}}), nv.Index(1)) } func TestFromTypedStringEmpty(t *testing.T) { @@ -477,19 +477,19 @@ func TestFromTypedStringNonEmptyOverwrite(t *testing.T) { } func TestFromTypedStringRetainsLocations(t *testing.T) { - var ref = dyn.NewValue("foo", []dyn.Location{{File: "foo"}}) + var ref = dyn.NewValue("foo", dyn.Locations{{File: "foo"}}) // case: value has not been changed var src string = "foo" nv, err := FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue("foo", []dyn.Location{{File: "foo"}}), nv) + assert.Equal(t, dyn.NewValue("foo", dyn.Locations{{File: "foo"}}), nv) // case: value has been changed src = "bar" nv, err = FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue("bar", []dyn.Location{{File: "foo"}}), nv) + assert.Equal(t, dyn.NewValue("bar", dyn.Locations{{File: "foo"}}), nv) } func TestFromTypedStringTypeError(t *testing.T) { @@ -532,19 +532,19 @@ func TestFromTypedBoolNonEmptyOverwrite(t *testing.T) { } func TestFromTypedBoolRetainsLocations(t *testing.T) { - var ref = dyn.NewValue(true, []dyn.Location{{File: "foo"}}) + var ref = dyn.NewValue(true, dyn.Locations{{File: "foo"}}) // case: value has not been changed var src bool = true nv, err := FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue(true, []dyn.Location{{File: "foo"}}), nv) + assert.Equal(t, dyn.NewValue(true, dyn.Locations{{File: "foo"}}), nv) // case: value has been changed src = false nv, err = FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue(false, []dyn.Location{{File: "foo"}}), nv) + assert.Equal(t, dyn.NewValue(false, dyn.Locations{{File: "foo"}}), nv) } func TestFromTypedBoolVariableReference(t *testing.T) { @@ -595,19 +595,19 @@ func TestFromTypedIntNonEmptyOverwrite(t *testing.T) { } func TestFromTypedIntRetainsLocations(t *testing.T) { - var ref = dyn.NewValue(1234, []dyn.Location{{File: "foo"}}) + var ref = dyn.NewValue(1234, dyn.Locations{{File: "foo"}}) // case: value has not been changed var src int = 1234 nv, err := FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue(1234, []dyn.Location{{File: "foo"}}), nv) + assert.Equal(t, dyn.NewValue(1234, dyn.Locations{{File: "foo"}}), nv) // case: value has been changed src = 1235 nv, err = FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue(int64(1235), []dyn.Location{{File: "foo"}}), nv) + assert.Equal(t, dyn.NewValue(int64(1235), dyn.Locations{{File: "foo"}}), nv) } func TestFromTypedIntVariableReference(t *testing.T) { @@ -659,19 +659,19 @@ func TestFromTypedFloatNonEmptyOverwrite(t *testing.T) { func TestFromTypedFloatRetainsLocations(t *testing.T) { var src float64 - var ref = dyn.NewValue(1.23, []dyn.Location{{File: "foo"}}) + var ref = dyn.NewValue(1.23, dyn.Locations{{File: "foo"}}) // case: value has not been changed src = 1.23 nv, err := FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue(1.23, []dyn.Location{{File: "foo"}}), nv) + assert.Equal(t, dyn.NewValue(1.23, dyn.Locations{{File: "foo"}}), nv) // case: value has been changed src = 1.24 nv, err = FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue(1.24, []dyn.Location{{File: "foo"}}), nv) + assert.Equal(t, dyn.NewValue(1.24, dyn.Locations{{File: "foo"}}), nv) } func TestFromTypedFloatVariableReference(t *testing.T) { @@ -740,27 +740,27 @@ func TestFromTypedNilPointerRetainsLocations(t *testing.T) { } var src *Tmp - ref := dyn.NewValue(nil, []dyn.Location{{File: "foobar"}}) + ref := dyn.NewValue(nil, dyn.Locations{{File: "foobar"}}) nv, err := FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue(nil, []dyn.Location{{File: "foobar"}}), nv) + assert.Equal(t, dyn.NewValue(nil, dyn.Locations{{File: "foobar"}}), nv) } func TestFromTypedNilMapRetainsLocation(t *testing.T) { var src map[string]string - ref := dyn.NewValue(nil, []dyn.Location{{File: "foobar"}}) + ref := dyn.NewValue(nil, dyn.Locations{{File: "foobar"}}) nv, err := FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue(nil, []dyn.Location{{File: "foobar"}}), nv) + assert.Equal(t, dyn.NewValue(nil, dyn.Locations{{File: "foobar"}}), nv) } func TestFromTypedNilSliceRetainsLocation(t *testing.T) { var src []string - ref := dyn.NewValue(nil, []dyn.Location{{File: "foobar"}}) + ref := dyn.NewValue(nil, dyn.Locations{{File: "foobar"}}) nv, err := FromTyped(src, ref) require.NoError(t, err) - assert.Equal(t, dyn.NewValue(nil, []dyn.Location{{File: "foobar"}}), nv) + assert.Equal(t, dyn.NewValue(nil, dyn.Locations{{File: "foobar"}}), nv) } diff --git a/libs/dyn/convert/normalize_test.go b/libs/dyn/convert/normalize_test.go index 452ed4eb1d..f3c291b38c 100644 --- a/libs/dyn/convert/normalize_test.go +++ b/libs/dyn/convert/normalize_test.go @@ -229,7 +229,7 @@ func TestNormalizeStructVariableReference(t *testing.T) { } var typ Tmp - vin := dyn.NewValue("${var.foo}", []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue("${var.foo}", dyn.Locations{{File: "file", Line: 1, Column: 1}}) vout, err := Normalize(typ, vin) assert.Empty(t, err) assert.Equal(t, vin, vout) @@ -241,7 +241,7 @@ func TestNormalizeStructRandomStringError(t *testing.T) { } var typ Tmp - vin := dyn.NewValue("var foo", []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue("var foo", dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -258,7 +258,7 @@ func TestNormalizeStructIntError(t *testing.T) { } var typ Tmp - vin := dyn.NewValue(1, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(1, dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -360,7 +360,7 @@ func TestNormalizeMapNestedError(t *testing.T) { func TestNormalizeMapVariableReference(t *testing.T) { var typ map[string]string - vin := dyn.NewValue("${var.foo}", []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue("${var.foo}", dyn.Locations{{File: "file", Line: 1, Column: 1}}) vout, err := Normalize(typ, vin) assert.Empty(t, err) assert.Equal(t, vin, vout) @@ -368,7 +368,7 @@ func TestNormalizeMapVariableReference(t *testing.T) { func TestNormalizeMapRandomStringError(t *testing.T) { var typ map[string]string - vin := dyn.NewValue("var foo", []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue("var foo", dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -381,7 +381,7 @@ func TestNormalizeMapRandomStringError(t *testing.T) { func TestNormalizeMapIntError(t *testing.T) { var typ map[string]string - vin := dyn.NewValue(1, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(1, dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -482,7 +482,7 @@ func TestNormalizeSliceNestedError(t *testing.T) { func TestNormalizeSliceVariableReference(t *testing.T) { var typ []string - vin := dyn.NewValue("${var.foo}", []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue("${var.foo}", dyn.Locations{{File: "file", Line: 1, Column: 1}}) vout, err := Normalize(typ, vin) assert.Empty(t, err) assert.Equal(t, vin, vout) @@ -490,7 +490,7 @@ func TestNormalizeSliceVariableReference(t *testing.T) { func TestNormalizeSliceRandomStringError(t *testing.T) { var typ []string - vin := dyn.NewValue("var foo", []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue("var foo", dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -503,7 +503,7 @@ func TestNormalizeSliceRandomStringError(t *testing.T) { func TestNormalizeSliceIntError(t *testing.T) { var typ []string - vin := dyn.NewValue(1, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(1, dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -524,7 +524,7 @@ func TestNormalizeString(t *testing.T) { func TestNormalizeStringNil(t *testing.T) { var typ string - vin := dyn.NewValue(nil, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(nil, dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(&typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -537,7 +537,7 @@ func TestNormalizeStringNil(t *testing.T) { func TestNormalizeStringFromBool(t *testing.T) { var typ string - vin := dyn.NewValue(true, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(true, dyn.Locations{{File: "file", Line: 1, Column: 1}}) vout, err := Normalize(&typ, vin) assert.Empty(t, err) assert.Equal(t, dyn.NewValue("true", vin.Locations()), vout) @@ -545,7 +545,7 @@ func TestNormalizeStringFromBool(t *testing.T) { func TestNormalizeStringFromInt(t *testing.T) { var typ string - vin := dyn.NewValue(123, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(123, dyn.Locations{{File: "file", Line: 1, Column: 1}}) vout, err := Normalize(&typ, vin) assert.Empty(t, err) assert.Equal(t, dyn.NewValue("123", vin.Locations()), vout) @@ -553,7 +553,7 @@ func TestNormalizeStringFromInt(t *testing.T) { func TestNormalizeStringFromFloat(t *testing.T) { var typ string - vin := dyn.NewValue(1.20, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(1.20, dyn.Locations{{File: "file", Line: 1, Column: 1}}) vout, err := Normalize(&typ, vin) assert.Empty(t, err) assert.Equal(t, dyn.NewValue("1.2", vin.Locations()), vout) @@ -582,7 +582,7 @@ func TestNormalizeBool(t *testing.T) { func TestNormalizeBoolNil(t *testing.T) { var typ bool - vin := dyn.NewValue(nil, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(nil, dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(&typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -658,7 +658,7 @@ func TestNormalizeInt(t *testing.T) { func TestNormalizeIntNil(t *testing.T) { var typ int - vin := dyn.NewValue(nil, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(nil, dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(&typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -742,7 +742,7 @@ func TestNormalizeFloat(t *testing.T) { func TestNormalizeFloatNil(t *testing.T) { var typ float64 - vin := dyn.NewValue(nil, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(nil, dyn.Locations{{File: "file", Line: 1, Column: 1}}) _, err := Normalize(&typ, vin) assert.Len(t, err, 1) assert.Equal(t, diag.Diagnostic{ @@ -842,26 +842,26 @@ func TestNormalizeAnchors(t *testing.T) { func TestNormalizeBoolToAny(t *testing.T) { var typ any - vin := dyn.NewValue(false, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(false, dyn.Locations{{File: "file", Line: 1, Column: 1}}) vout, err := Normalize(&typ, vin) assert.Len(t, err, 0) - assert.Equal(t, dyn.NewValue(false, []dyn.Location{{File: "file", Line: 1, Column: 1}}), vout) + assert.Equal(t, dyn.NewValue(false, dyn.Locations{{File: "file", Line: 1, Column: 1}}), vout) } func TestNormalizeIntToAny(t *testing.T) { var typ any - vin := dyn.NewValue(10, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue(10, dyn.Locations{{File: "file", Line: 1, Column: 1}}) vout, err := Normalize(&typ, vin) assert.Len(t, err, 0) - assert.Equal(t, dyn.NewValue(10, []dyn.Location{{File: "file", Line: 1, Column: 1}}), vout) + assert.Equal(t, dyn.NewValue(10, dyn.Locations{{File: "file", Line: 1, Column: 1}}), vout) } func TestNormalizeSliceToAny(t *testing.T) { var typ any - v1 := dyn.NewValue(1, []dyn.Location{{File: "file", Line: 1, Column: 1}}) - v2 := dyn.NewValue(2, []dyn.Location{{File: "file", Line: 1, Column: 1}}) - vin := dyn.NewValue([]dyn.Value{v1, v2}, []dyn.Location{{File: "file", Line: 1, Column: 1}}) + v1 := dyn.NewValue(1, dyn.Locations{{File: "file", Line: 1, Column: 1}}) + v2 := dyn.NewValue(2, dyn.Locations{{File: "file", Line: 1, Column: 1}}) + vin := dyn.NewValue([]dyn.Value{v1, v2}, dyn.Locations{{File: "file", Line: 1, Column: 1}}) vout, err := Normalize(&typ, vin) assert.Len(t, err, 0) - assert.Equal(t, dyn.NewValue([]dyn.Value{v1, v2}, []dyn.Location{{File: "file", Line: 1, Column: 1}}), vout) + assert.Equal(t, dyn.NewValue([]dyn.Value{v1, v2}, dyn.Locations{{File: "file", Line: 1, Column: 1}}), vout) } diff --git a/libs/dyn/location.go b/libs/dyn/location.go index 961d2f1215..3942a0efde 100644 --- a/libs/dyn/location.go +++ b/libs/dyn/location.go @@ -5,6 +5,8 @@ import ( "path/filepath" ) +type Locations []Location + type Location struct { File string Line int diff --git a/libs/dyn/merge/merge_test.go b/libs/dyn/merge/merge_test.go index 4a4bf9e6c0..d602a05068 100644 --- a/libs/dyn/merge/merge_test.go +++ b/libs/dyn/merge/merge_test.go @@ -10,15 +10,15 @@ import ( func TestMergeMaps(t *testing.T) { l1 := dyn.Location{File: "file1", Line: 1, Column: 2} v1 := dyn.NewValue(map[string]dyn.Value{ - "foo": dyn.NewValue("bar", []dyn.Location{l1}), - "bar": dyn.NewValue("baz", []dyn.Location{l1}), - }, []dyn.Location{l1}) + "foo": dyn.NewValue("bar", dyn.Locations{l1}), + "bar": dyn.NewValue("baz", dyn.Locations{l1}), + }, dyn.Locations{l1}) l2 := dyn.Location{File: "file2", Line: 3, Column: 4} v2 := dyn.NewValue(map[string]dyn.Value{ - "bar": dyn.NewValue("qux", []dyn.Location{l2}), - "qux": dyn.NewValue("foo", []dyn.Location{l2}), - }, []dyn.Location{l2}) + "bar": dyn.NewValue("qux", dyn.Locations{l2}), + "qux": dyn.NewValue("foo", dyn.Locations{l2}), + }, dyn.Locations{l2}) // Merge v2 into v1. { @@ -31,10 +31,10 @@ func TestMergeMaps(t *testing.T) { }, out.AsAny()) // Locations of both values should be preserved. - assert.Equal(t, []dyn.Location{l1, l2}, out.Locations()) - assert.Equal(t, []dyn.Location{l2, l1}, out.Get("bar").Locations()) - assert.Equal(t, []dyn.Location{l1}, out.Get("foo").Locations()) - assert.Equal(t, []dyn.Location{l2}, out.Get("qux").Locations()) + assert.Equal(t, dyn.Locations{l1, l2}, out.Locations()) + assert.Equal(t, dyn.Locations{l2, l1}, out.Get("bar").Locations()) + assert.Equal(t, dyn.Locations{l1}, out.Get("foo").Locations()) + assert.Equal(t, dyn.Locations{l2}, out.Get("qux").Locations()) // Location of the merged value should be the location of v1. assert.Equal(t, l1, out.Location()) @@ -59,10 +59,10 @@ func TestMergeMaps(t *testing.T) { }, out.AsAny()) // Locations of both values should be preserved. - assert.Equal(t, []dyn.Location{l2, l1}, out.Locations()) - assert.Equal(t, []dyn.Location{l1, l2}, out.Get("bar").Locations()) - assert.Equal(t, []dyn.Location{l1}, out.Get("foo").Locations()) - assert.Equal(t, []dyn.Location{l2}, out.Get("qux").Locations()) + assert.Equal(t, dyn.Locations{l2, l1}, out.Locations()) + assert.Equal(t, dyn.Locations{l1, l2}, out.Get("bar").Locations()) + assert.Equal(t, dyn.Locations{l1}, out.Get("foo").Locations()) + assert.Equal(t, dyn.Locations{l2}, out.Get("qux").Locations()) // Location of the merged value should be the location of v2. assert.Equal(t, l2, out.Location()) @@ -82,10 +82,10 @@ func TestMergeMapsNil(t *testing.T) { l := dyn.Location{File: "file", Line: 1, Column: 2} v := dyn.NewValue(map[string]dyn.Value{ "foo": dyn.V("bar"), - }, []dyn.Location{l}) + }, dyn.Locations{l}) nilL := dyn.Location{File: "file", Line: 3, Column: 4} - nilV := dyn.NewValue(nil, []dyn.Location{nilL}) + nilV := dyn.NewValue(nil, dyn.Locations{nilL}) // Merge nil into v. { @@ -96,7 +96,7 @@ func TestMergeMapsNil(t *testing.T) { }, out.AsAny()) // Locations of both values should be preserved. - assert.Equal(t, []dyn.Location{l, nilL}, out.Locations()) + assert.Equal(t, dyn.Locations{l, nilL}, out.Locations()) // Location of the non-nil value should be returned by .Location(). assert.Equal(t, l, out.Location()) @@ -111,7 +111,7 @@ func TestMergeMapsNil(t *testing.T) { }, out.AsAny()) // Locations of both values should be preserved. - assert.Equal(t, []dyn.Location{l, nilL}, out.Locations()) + assert.Equal(t, dyn.Locations{l, nilL}, out.Locations()) // Location of the non-nil value should be returned by .Location(). assert.Equal(t, l, out.Location()) @@ -136,16 +136,16 @@ func TestMergeMapsError(t *testing.T) { func TestMergeSequences(t *testing.T) { l1 := dyn.Location{File: "file1", Line: 1, Column: 2} v1 := dyn.NewValue([]dyn.Value{ - dyn.NewValue("bar", []dyn.Location{l1}), - dyn.NewValue("baz", []dyn.Location{l1}), - }, []dyn.Location{l1}) + dyn.NewValue("bar", dyn.Locations{l1}), + dyn.NewValue("baz", dyn.Locations{l1}), + }, dyn.Locations{l1}) l2 := dyn.Location{File: "file2", Line: 3, Column: 4} l3 := dyn.Location{File: "file3", Line: 5, Column: 6} v2 := dyn.NewValue([]dyn.Value{ - dyn.NewValue("qux", []dyn.Location{l2}), - dyn.NewValue("foo", []dyn.Location{l3}), - }, []dyn.Location{l2, l3}) + dyn.NewValue("qux", dyn.Locations{l2}), + dyn.NewValue("foo", dyn.Locations{l3}), + }, dyn.Locations{l2, l3}) // Merge v2 into v1. { @@ -159,7 +159,7 @@ func TestMergeSequences(t *testing.T) { }, out.AsAny()) // Locations of both values should be preserved. - assert.Equal(t, []dyn.Location{l1, l2, l3}, out.Locations()) + assert.Equal(t, dyn.Locations{l1, l2, l3}, out.Locations()) // Location of the merged value should be the location of v1. assert.Equal(t, l1, out.Location()) @@ -183,7 +183,7 @@ func TestMergeSequences(t *testing.T) { }, out.AsAny()) // Locations of both values should be preserved. - assert.Equal(t, []dyn.Location{l2, l3, l1}, out.Locations()) + assert.Equal(t, dyn.Locations{l2, l3, l1}, out.Locations()) // Location of the merged value should be the location of v2. assert.Equal(t, l2, out.Location()) @@ -238,8 +238,8 @@ func TestMergeSequencesError(t *testing.T) { func TestMergePrimitives(t *testing.T) { l1 := dyn.Location{File: "file1", Line: 1, Column: 2} l2 := dyn.Location{File: "file2", Line: 3, Column: 4} - v1 := dyn.NewValue("bar", []dyn.Location{l1}) - v2 := dyn.NewValue("baz", []dyn.Location{l2}) + v1 := dyn.NewValue("bar", dyn.Locations{l1}) + v2 := dyn.NewValue("baz", dyn.Locations{l2}) // Merge v2 into v1. { @@ -248,7 +248,7 @@ func TestMergePrimitives(t *testing.T) { assert.Equal(t, "baz", out.AsAny()) // Locations of both values should be preserved. - assert.Equal(t, []dyn.Location{l2, l1}, out.Locations()) + assert.Equal(t, dyn.Locations{l2, l1}, out.Locations()) // Location of the merged value should be the location of v2, the second value. assert.Equal(t, l2, out.Location()) @@ -261,7 +261,7 @@ func TestMergePrimitives(t *testing.T) { assert.Equal(t, "bar", out.AsAny()) // Locations of both values should be preserved. - assert.Equal(t, []dyn.Location{l1, l2}, out.Locations()) + assert.Equal(t, dyn.Locations{l1, l2}, out.Locations()) // Location of the merged value should be the location of v1, the second value. assert.Equal(t, l1, out.Location()) diff --git a/libs/dyn/merge/override_test.go b/libs/dyn/merge/override_test.go index 9d41a526ea..24d84ae898 100644 --- a/libs/dyn/merge/override_test.go +++ b/libs/dyn/merge/override_test.go @@ -27,79 +27,79 @@ func TestOverride_Primitive(t *testing.T) { { name: "string (updated)", state: visitorState{updated: []string{"root"}}, - left: dyn.NewValue("a", []dyn.Location{leftLocation}), - right: dyn.NewValue("b", []dyn.Location{rightLocation}), - expected: dyn.NewValue("b", []dyn.Location{rightLocation}), + left: dyn.NewValue("a", dyn.Locations{leftLocation}), + right: dyn.NewValue("b", dyn.Locations{rightLocation}), + expected: dyn.NewValue("b", dyn.Locations{rightLocation}), }, { name: "string (not updated)", state: visitorState{}, - left: dyn.NewValue("a", []dyn.Location{leftLocation}), - right: dyn.NewValue("a", []dyn.Location{rightLocation}), - expected: dyn.NewValue("a", []dyn.Location{leftLocation}), + left: dyn.NewValue("a", dyn.Locations{leftLocation}), + right: dyn.NewValue("a", dyn.Locations{rightLocation}), + expected: dyn.NewValue("a", dyn.Locations{leftLocation}), }, { name: "bool (updated)", state: visitorState{updated: []string{"root"}}, - left: dyn.NewValue(true, []dyn.Location{leftLocation}), - right: dyn.NewValue(false, []dyn.Location{rightLocation}), - expected: dyn.NewValue(false, []dyn.Location{rightLocation}), + left: dyn.NewValue(true, dyn.Locations{leftLocation}), + right: dyn.NewValue(false, dyn.Locations{rightLocation}), + expected: dyn.NewValue(false, dyn.Locations{rightLocation}), }, { name: "bool (not updated)", state: visitorState{}, - left: dyn.NewValue(true, []dyn.Location{leftLocation}), - right: dyn.NewValue(true, []dyn.Location{rightLocation}), - expected: dyn.NewValue(true, []dyn.Location{leftLocation}), + left: dyn.NewValue(true, dyn.Locations{leftLocation}), + right: dyn.NewValue(true, dyn.Locations{rightLocation}), + expected: dyn.NewValue(true, dyn.Locations{leftLocation}), }, { name: "int (updated)", state: visitorState{updated: []string{"root"}}, - left: dyn.NewValue(1, []dyn.Location{leftLocation}), - right: dyn.NewValue(2, []dyn.Location{rightLocation}), - expected: dyn.NewValue(2, []dyn.Location{rightLocation}), + left: dyn.NewValue(1, dyn.Locations{leftLocation}), + right: dyn.NewValue(2, dyn.Locations{rightLocation}), + expected: dyn.NewValue(2, dyn.Locations{rightLocation}), }, { name: "int (not updated)", state: visitorState{}, - left: dyn.NewValue(int32(1), []dyn.Location{leftLocation}), - right: dyn.NewValue(int64(1), []dyn.Location{rightLocation}), - expected: dyn.NewValue(int32(1), []dyn.Location{leftLocation}), + left: dyn.NewValue(int32(1), dyn.Locations{leftLocation}), + right: dyn.NewValue(int64(1), dyn.Locations{rightLocation}), + expected: dyn.NewValue(int32(1), dyn.Locations{leftLocation}), }, { name: "float (updated)", state: visitorState{updated: []string{"root"}}, - left: dyn.NewValue(1.0, []dyn.Location{leftLocation}), - right: dyn.NewValue(2.0, []dyn.Location{rightLocation}), - expected: dyn.NewValue(2.0, []dyn.Location{rightLocation}), + left: dyn.NewValue(1.0, dyn.Locations{leftLocation}), + right: dyn.NewValue(2.0, dyn.Locations{rightLocation}), + expected: dyn.NewValue(2.0, dyn.Locations{rightLocation}), }, { name: "float (not updated)", state: visitorState{}, - left: dyn.NewValue(float32(1.0), []dyn.Location{leftLocation}), - right: dyn.NewValue(float64(1.0), []dyn.Location{rightLocation}), - expected: dyn.NewValue(float32(1.0), []dyn.Location{leftLocation}), + left: dyn.NewValue(float32(1.0), dyn.Locations{leftLocation}), + right: dyn.NewValue(float64(1.0), dyn.Locations{rightLocation}), + expected: dyn.NewValue(float32(1.0), dyn.Locations{leftLocation}), }, { name: "time (updated)", state: visitorState{updated: []string{"root"}}, - left: dyn.NewValue(time.UnixMilli(10000), []dyn.Location{leftLocation}), - right: dyn.NewValue(time.UnixMilli(10001), []dyn.Location{rightLocation}), - expected: dyn.NewValue(time.UnixMilli(10001), []dyn.Location{rightLocation}), + left: dyn.NewValue(time.UnixMilli(10000), dyn.Locations{leftLocation}), + right: dyn.NewValue(time.UnixMilli(10001), dyn.Locations{rightLocation}), + expected: dyn.NewValue(time.UnixMilli(10001), dyn.Locations{rightLocation}), }, { name: "time (not updated)", state: visitorState{}, - left: dyn.NewValue(time.UnixMilli(10000), []dyn.Location{leftLocation}), - right: dyn.NewValue(time.UnixMilli(10000), []dyn.Location{rightLocation}), - expected: dyn.NewValue(time.UnixMilli(10000), []dyn.Location{leftLocation}), + left: dyn.NewValue(time.UnixMilli(10000), dyn.Locations{leftLocation}), + right: dyn.NewValue(time.UnixMilli(10000), dyn.Locations{rightLocation}), + expected: dyn.NewValue(time.UnixMilli(10000), dyn.Locations{leftLocation}), }, { name: "different types (updated)", state: visitorState{updated: []string{"root"}}, - left: dyn.NewValue("a", []dyn.Location{leftLocation}), - right: dyn.NewValue(42, []dyn.Location{rightLocation}), - expected: dyn.NewValue(42, []dyn.Location{rightLocation}), + left: dyn.NewValue("a", dyn.Locations{leftLocation}), + right: dyn.NewValue(42, dyn.Locations{rightLocation}), + expected: dyn.NewValue(42, dyn.Locations{rightLocation}), }, { name: "map - remove 'a', update 'b'", @@ -109,22 +109,22 @@ func TestOverride_Primitive(t *testing.T) { }, left: dyn.NewValue( map[string]dyn.Value{ - "a": dyn.NewValue(42, []dyn.Location{leftLocation}), - "b": dyn.NewValue(10, []dyn.Location{leftLocation}), + "a": dyn.NewValue(42, dyn.Locations{leftLocation}), + "b": dyn.NewValue(10, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}), + dyn.Locations{leftLocation}), right: dyn.NewValue( map[string]dyn.Value{ - "b": dyn.NewValue(20, []dyn.Location{rightLocation}), + "b": dyn.NewValue(20, dyn.Locations{rightLocation}), }, - []dyn.Location{rightLocation}), + dyn.Locations{rightLocation}), expected: dyn.NewValue( map[string]dyn.Value{ - "b": dyn.NewValue(20, []dyn.Location{rightLocation}), + "b": dyn.NewValue(20, dyn.Locations{rightLocation}), }, - []dyn.Location{leftLocation}), + dyn.Locations{leftLocation}), }, { name: "map - add 'a'", @@ -133,26 +133,26 @@ func TestOverride_Primitive(t *testing.T) { }, left: dyn.NewValue( map[string]dyn.Value{ - "b": dyn.NewValue(10, []dyn.Location{leftLocation}), + "b": dyn.NewValue(10, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), right: dyn.NewValue( map[string]dyn.Value{ - "a": dyn.NewValue(42, []dyn.Location{rightLocation}), - "b": dyn.NewValue(10, []dyn.Location{rightLocation}), + "a": dyn.NewValue(42, dyn.Locations{rightLocation}), + "b": dyn.NewValue(10, dyn.Locations{rightLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), expected: dyn.NewValue( map[string]dyn.Value{ - "a": dyn.NewValue(42, []dyn.Location{rightLocation}), + "a": dyn.NewValue(42, dyn.Locations{rightLocation}), // location hasn't changed because value hasn't changed - "b": dyn.NewValue(10, []dyn.Location{leftLocation}), + "b": dyn.NewValue(10, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, { @@ -162,25 +162,25 @@ func TestOverride_Primitive(t *testing.T) { }, left: dyn.NewValue( map[string]dyn.Value{ - "a": dyn.NewValue(42, []dyn.Location{leftLocation}), - "b": dyn.NewValue(10, []dyn.Location{leftLocation}), + "a": dyn.NewValue(42, dyn.Locations{leftLocation}), + "b": dyn.NewValue(10, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), right: dyn.NewValue( map[string]dyn.Value{ - "b": dyn.NewValue(10, []dyn.Location{rightLocation}), + "b": dyn.NewValue(10, dyn.Locations{rightLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), expected: dyn.NewValue( map[string]dyn.Value{ // location hasn't changed because value hasn't changed - "b": dyn.NewValue(10, []dyn.Location{leftLocation}), + "b": dyn.NewValue(10, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, { @@ -192,38 +192,38 @@ func TestOverride_Primitive(t *testing.T) { map[string]dyn.Value{ "jobs": dyn.NewValue( map[string]dyn.Value{ - "job_0": dyn.NewValue(42, []dyn.Location{leftLocation}), + "job_0": dyn.NewValue(42, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), right: dyn.NewValue( map[string]dyn.Value{ "jobs": dyn.NewValue( map[string]dyn.Value{ - "job_0": dyn.NewValue(42, []dyn.Location{rightLocation}), - "job_1": dyn.NewValue(1337, []dyn.Location{rightLocation}), + "job_0": dyn.NewValue(42, dyn.Locations{rightLocation}), + "job_1": dyn.NewValue(1337, dyn.Locations{rightLocation}), }, - []dyn.Location{rightLocation}, + dyn.Locations{rightLocation}, ), }, - []dyn.Location{rightLocation}, + dyn.Locations{rightLocation}, ), expected: dyn.NewValue( map[string]dyn.Value{ "jobs": dyn.NewValue( map[string]dyn.Value{ - "job_0": dyn.NewValue(42, []dyn.Location{leftLocation}), - "job_1": dyn.NewValue(1337, []dyn.Location{rightLocation}), + "job_0": dyn.NewValue(42, dyn.Locations{leftLocation}), + "job_1": dyn.NewValue(1337, dyn.Locations{rightLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, { @@ -233,35 +233,35 @@ func TestOverride_Primitive(t *testing.T) { map[string]dyn.Value{ "jobs": dyn.NewValue( map[string]dyn.Value{ - "job_0": dyn.NewValue(42, []dyn.Location{leftLocation}), - "job_1": dyn.NewValue(1337, []dyn.Location{rightLocation}), + "job_0": dyn.NewValue(42, dyn.Locations{leftLocation}), + "job_1": dyn.NewValue(1337, dyn.Locations{rightLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), right: dyn.NewValue( map[string]dyn.Value{ "jobs": dyn.NewValue( map[string]dyn.Value{ - "job_0": dyn.NewValue(42, []dyn.Location{rightLocation}), + "job_0": dyn.NewValue(42, dyn.Locations{rightLocation}), }, - []dyn.Location{rightLocation}, + dyn.Locations{rightLocation}, ), }, - []dyn.Location{rightLocation}, + dyn.Locations{rightLocation}, ), expected: dyn.NewValue( map[string]dyn.Value{ "jobs": dyn.NewValue( map[string]dyn.Value{ - "job_0": dyn.NewValue(42, []dyn.Location{leftLocation}), + "job_0": dyn.NewValue(42, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, { @@ -269,23 +269,23 @@ func TestOverride_Primitive(t *testing.T) { state: visitorState{added: []string{"root[1]"}}, left: dyn.NewValue( []dyn.Value{ - dyn.NewValue(42, []dyn.Location{leftLocation}), + dyn.NewValue(42, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), right: dyn.NewValue( []dyn.Value{ - dyn.NewValue(42, []dyn.Location{rightLocation}), - dyn.NewValue(10, []dyn.Location{rightLocation}), + dyn.NewValue(42, dyn.Locations{rightLocation}), + dyn.NewValue(10, dyn.Locations{rightLocation}), }, - []dyn.Location{rightLocation}, + dyn.Locations{rightLocation}, ), expected: dyn.NewValue( []dyn.Value{ - dyn.NewValue(42, []dyn.Location{leftLocation}), - dyn.NewValue(10, []dyn.Location{rightLocation}), + dyn.NewValue(42, dyn.Locations{leftLocation}), + dyn.NewValue(10, dyn.Locations{rightLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, { @@ -293,22 +293,22 @@ func TestOverride_Primitive(t *testing.T) { state: visitorState{removed: []string{"root[1]"}}, left: dyn.NewValue( []dyn.Value{ - dyn.NewValue(42, []dyn.Location{leftLocation}), - dyn.NewValue(10, []dyn.Location{leftLocation}), + dyn.NewValue(42, dyn.Locations{leftLocation}), + dyn.NewValue(10, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), right: dyn.NewValue( []dyn.Value{ - dyn.NewValue(42, []dyn.Location{rightLocation}), + dyn.NewValue(42, dyn.Locations{rightLocation}), }, - []dyn.Location{rightLocation}, + dyn.Locations{rightLocation}, ), expected: dyn.NewValue( []dyn.Value{ - dyn.NewValue(42, []dyn.Location{leftLocation}), + dyn.NewValue(42, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), // location hasn't changed because value hasn't changed }, @@ -317,43 +317,43 @@ func TestOverride_Primitive(t *testing.T) { state: visitorState{}, left: dyn.NewValue( []dyn.Value{ - dyn.NewValue(42, []dyn.Location{leftLocation}), + dyn.NewValue(42, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), right: dyn.NewValue( []dyn.Value{ - dyn.NewValue(42, []dyn.Location{rightLocation}), + dyn.NewValue(42, dyn.Locations{rightLocation}), }, - []dyn.Location{rightLocation}, + dyn.Locations{rightLocation}, ), expected: dyn.NewValue( []dyn.Value{ - dyn.NewValue(42, []dyn.Location{leftLocation}), + dyn.NewValue(42, dyn.Locations{leftLocation}), }, - []dyn.Location{leftLocation}, + dyn.Locations{leftLocation}, ), }, { name: "nil (not updated)", state: visitorState{}, - left: dyn.NilValue.WithLocations([]dyn.Location{leftLocation}), - right: dyn.NilValue.WithLocations([]dyn.Location{rightLocation}), - expected: dyn.NilValue.WithLocations([]dyn.Location{leftLocation}), + left: dyn.NilValue.WithLocations(dyn.Locations{leftLocation}), + right: dyn.NilValue.WithLocations(dyn.Locations{rightLocation}), + expected: dyn.NilValue.WithLocations(dyn.Locations{leftLocation}), }, { name: "nil (updated)", state: visitorState{updated: []string{"root"}}, left: dyn.NilValue, - right: dyn.NewValue(42, []dyn.Location{rightLocation}), - expected: dyn.NewValue(42, []dyn.Location{rightLocation}), + right: dyn.NewValue(42, dyn.Locations{rightLocation}), + expected: dyn.NewValue(42, dyn.Locations{rightLocation}), }, { name: "change kind (updated)", state: visitorState{updated: []string{"root"}}, - left: dyn.NewValue(42.0, []dyn.Location{leftLocation}), - right: dyn.NewValue(42, []dyn.Location{rightLocation}), - expected: dyn.NewValue(42, []dyn.Location{rightLocation}), + left: dyn.NewValue(42.0, dyn.Locations{leftLocation}), + right: dyn.NewValue(42, dyn.Locations{rightLocation}), + expected: dyn.NewValue(42, dyn.Locations{rightLocation}), }, } @@ -432,17 +432,17 @@ func TestOverride_PreserveMappingKeys(t *testing.T) { rightValueLocation := dyn.Location{File: "right.yml", Line: 3, Column: 1} left := dyn.NewMapping() - left.Set(dyn.NewValue("a", []dyn.Location{leftKeyLocation}), dyn.NewValue(42, []dyn.Location{leftValueLocation})) + left.Set(dyn.NewValue("a", dyn.Locations{leftKeyLocation}), dyn.NewValue(42, dyn.Locations{leftValueLocation})) right := dyn.NewMapping() - right.Set(dyn.NewValue("a", []dyn.Location{rightKeyLocation}), dyn.NewValue(7, []dyn.Location{rightValueLocation})) + right.Set(dyn.NewValue("a", dyn.Locations{rightKeyLocation}), dyn.NewValue(7, dyn.Locations{rightValueLocation})) state, visitor := createVisitor(visitorOpts{}) out, err := override( dyn.EmptyPath, - dyn.NewValue(left, []dyn.Location{leftLocation}), - dyn.NewValue(right, []dyn.Location{rightLocation}), + dyn.NewValue(left, dyn.Locations{leftLocation}), + dyn.NewValue(right, dyn.Locations{rightLocation}), visitor, ) diff --git a/libs/dyn/value_test.go b/libs/dyn/value_test.go index 6a0a27b8dc..2345264f7f 100644 --- a/libs/dyn/value_test.go +++ b/libs/dyn/value_test.go @@ -33,9 +33,9 @@ func TestValueAsMap(t *testing.T) { map[string]dyn.Value{ "key": dyn.NewValue( "value", - []dyn.Location{{File: "file", Line: 1, Column: 2}}), + dyn.Locations{{File: "file", Line: 1, Column: 2}}), }, - []dyn.Location{{File: "file", Line: 1, Column: 2}}, + dyn.Locations{{File: "file", Line: 1, Column: 2}}, ) m, ok := mapValue.AsMap() diff --git a/libs/dyn/value_underlying_test.go b/libs/dyn/value_underlying_test.go index e35cde5823..09653da135 100644 --- a/libs/dyn/value_underlying_test.go +++ b/libs/dyn/value_underlying_test.go @@ -11,7 +11,7 @@ import ( func TestValueUnderlyingMap(t *testing.T) { v := dyn.V( map[string]dyn.Value{ - "key": dyn.NewValue("value", []dyn.Location{{File: "file", Line: 1, Column: 2}}), + "key": dyn.NewValue("value", dyn.Locations{{File: "file", Line: 1, Column: 2}}), }, ) @@ -33,7 +33,7 @@ func TestValueUnderlyingMap(t *testing.T) { func TestValueUnderlyingSequence(t *testing.T) { v := dyn.V( []dyn.Value{ - dyn.NewValue("value", []dyn.Location{{File: "file", Line: 1, Column: 2}}), + dyn.NewValue("value", dyn.Locations{{File: "file", Line: 1, Column: 2}}), }, ) diff --git a/libs/dyn/yamlloader/loader.go b/libs/dyn/yamlloader/loader.go index fbb52b5045..7214328e96 100644 --- a/libs/dyn/yamlloader/loader.go +++ b/libs/dyn/yamlloader/loader.go @@ -86,7 +86,7 @@ func (d *loader) loadSequence(node *yaml.Node, loc dyn.Location) (dyn.Value, err acc[i] = v } - return dyn.NewValue(acc, []dyn.Location{loc}), nil + return dyn.NewValue(acc, dyn.Locations{loc}), nil } func (d *loader) loadMapping(node *yaml.Node, loc dyn.Location) (dyn.Value, error) { @@ -130,7 +130,7 @@ func (d *loader) loadMapping(node *yaml.Node, loc dyn.Location) (dyn.Value, erro } if merge == nil { - return dyn.NewValue(acc, []dyn.Location{loc}), nil + return dyn.NewValue(acc, dyn.Locations{loc}), nil } // Build location for the merge node. @@ -171,20 +171,20 @@ func (d *loader) loadMapping(node *yaml.Node, loc dyn.Location) (dyn.Value, erro out.Merge(m) } - return dyn.NewValue(out, []dyn.Location{loc}), nil + return dyn.NewValue(out, dyn.Locations{loc}), nil } func (d *loader) loadScalar(node *yaml.Node, loc dyn.Location) (dyn.Value, error) { st := node.ShortTag() switch st { case "!!str": - return dyn.NewValue(node.Value, []dyn.Location{loc}), nil + return dyn.NewValue(node.Value, dyn.Locations{loc}), nil case "!!bool": switch strings.ToLower(node.Value) { case "true": - return dyn.NewValue(true, []dyn.Location{loc}), nil + return dyn.NewValue(true, dyn.Locations{loc}), nil case "false": - return dyn.NewValue(false, []dyn.Location{loc}), nil + return dyn.NewValue(false, dyn.Locations{loc}), nil default: return dyn.InvalidValue, errorf(loc, "invalid bool value: %v", node.Value) } @@ -195,17 +195,17 @@ func (d *loader) loadScalar(node *yaml.Node, loc dyn.Location) (dyn.Value, error } // Use regular int type instead of int64 if possible. if i64 >= math.MinInt32 && i64 <= math.MaxInt32 { - return dyn.NewValue(int(i64), []dyn.Location{loc}), nil + return dyn.NewValue(int(i64), dyn.Locations{loc}), nil } - return dyn.NewValue(i64, []dyn.Location{loc}), nil + return dyn.NewValue(i64, dyn.Locations{loc}), nil case "!!float": f64, err := strconv.ParseFloat(node.Value, 64) if err != nil { return dyn.InvalidValue, errorf(loc, "invalid float value: %v", node.Value) } - return dyn.NewValue(f64, []dyn.Location{loc}), nil + return dyn.NewValue(f64, dyn.Locations{loc}), nil case "!!null": - return dyn.NewValue(nil, []dyn.Location{loc}), nil + return dyn.NewValue(nil, dyn.Locations{loc}), nil case "!!timestamp": // Try a couple of layouts for _, layout := range []string{ @@ -216,7 +216,7 @@ func (d *loader) loadScalar(node *yaml.Node, loc dyn.Location) (dyn.Value, error } { t, terr := time.Parse(layout, node.Value) if terr == nil { - return dyn.NewValue(t, []dyn.Location{loc}), nil + return dyn.NewValue(t, dyn.Locations{loc}), nil } } return dyn.InvalidValue, errorf(loc, "invalid timestamp value: %v", node.Value) diff --git a/libs/dyn/yamlsaver/saver_test.go b/libs/dyn/yamlsaver/saver_test.go index 387090104f..0fa252353b 100644 --- a/libs/dyn/yamlsaver/saver_test.go +++ b/libs/dyn/yamlsaver/saver_test.go @@ -57,10 +57,10 @@ func TestMarshalSequenceValue(t *testing.T) { s := NewSaver() var sequenceValue = dyn.NewValue( []dyn.Value{ - dyn.NewValue("value1", []dyn.Location{{File: "file", Line: 1, Column: 2}}), - dyn.NewValue("value2", []dyn.Location{{File: "file", Line: 2, Column: 2}}), + dyn.NewValue("value1", dyn.Locations{{File: "file", Line: 1, Column: 2}}), + dyn.NewValue("value2", dyn.Locations{{File: "file", Line: 2, Column: 2}}), }, - []dyn.Location{{File: "file", Line: 1, Column: 2}}, + dyn.Locations{{File: "file", Line: 1, Column: 2}}, ) v, err := s.toYamlNode(sequenceValue) assert.NoError(t, err) @@ -82,11 +82,11 @@ func TestMarshalMapValue(t *testing.T) { s := NewSaver() var mapValue = dyn.NewValue( map[string]dyn.Value{ - "key3": dyn.NewValue("value3", []dyn.Location{{File: "file", Line: 3, Column: 2}}), - "key2": dyn.NewValue("value2", []dyn.Location{{File: "file", Line: 2, Column: 2}}), - "key1": dyn.NewValue("value1", []dyn.Location{{File: "file", Line: 1, Column: 2}}), + "key3": dyn.NewValue("value3", dyn.Locations{{File: "file", Line: 3, Column: 2}}), + "key2": dyn.NewValue("value2", dyn.Locations{{File: "file", Line: 2, Column: 2}}), + "key1": dyn.NewValue("value1", dyn.Locations{{File: "file", Line: 1, Column: 2}}), }, - []dyn.Location{{File: "file", Line: 1, Column: 2}}, + dyn.Locations{{File: "file", Line: 1, Column: 2}}, ) v, err := s.toYamlNode(mapValue) @@ -108,12 +108,12 @@ func TestMarshalNestedValues(t *testing.T) { map[string]dyn.Value{ "key1": dyn.NewValue( map[string]dyn.Value{ - "key2": dyn.NewValue("value", []dyn.Location{{File: "file", Line: 1, Column: 2}}), + "key2": dyn.NewValue("value", dyn.Locations{{File: "file", Line: 1, Column: 2}}), }, - []dyn.Location{{File: "file", Line: 1, Column: 2}}, + dyn.Locations{{File: "file", Line: 1, Column: 2}}, ), }, - []dyn.Location{{File: "file", Line: 1, Column: 2}}, + dyn.Locations{{File: "file", Line: 1, Column: 2}}, ) v, err := s.toYamlNode(mapValue) assert.NoError(t, err) @@ -216,18 +216,18 @@ func TestCustomStylingWithNestedMap(t *testing.T) { var styledMap = dyn.NewValue( map[string]dyn.Value{ - "key1": dyn.NewValue("value1", []dyn.Location{{File: "file", Line: 1, Column: 2}}), - "key2": dyn.NewValue("value2", []dyn.Location{{File: "file", Line: 2, Column: 2}}), + "key1": dyn.NewValue("value1", dyn.Locations{{File: "file", Line: 1, Column: 2}}), + "key2": dyn.NewValue("value2", dyn.Locations{{File: "file", Line: 2, Column: 2}}), }, - []dyn.Location{{File: "file", Line: -2, Column: 2}}, + dyn.Locations{{File: "file", Line: -2, Column: 2}}, ) var unstyledMap = dyn.NewValue( map[string]dyn.Value{ - "key3": dyn.NewValue("value3", []dyn.Location{{File: "file", Line: 1, Column: 2}}), - "key4": dyn.NewValue("value4", []dyn.Location{{File: "file", Line: 2, Column: 2}}), + "key3": dyn.NewValue("value3", dyn.Locations{{File: "file", Line: 1, Column: 2}}), + "key4": dyn.NewValue("value4", dyn.Locations{{File: "file", Line: 2, Column: 2}}), }, - []dyn.Location{{File: "file", Line: -1, Column: 2}}, + dyn.Locations{{File: "file", Line: -1, Column: 2}}, ) var val = dyn.NewValue( @@ -235,7 +235,7 @@ func TestCustomStylingWithNestedMap(t *testing.T) { "styled": styledMap, "unstyled": unstyledMap, }, - []dyn.Location{{File: "file", Line: 1, Column: 2}}, + dyn.Locations{{File: "file", Line: 1, Column: 2}}, ) mv, err := s.toYamlNode(val) diff --git a/libs/dyn/yamlsaver/utils.go b/libs/dyn/yamlsaver/utils.go index a162bf31f3..b474e316c6 100644 --- a/libs/dyn/yamlsaver/utils.go +++ b/libs/dyn/yamlsaver/utils.go @@ -44,7 +44,7 @@ func skipAndOrder(mv dyn.Value, order *Order, skipFields []string, dst map[strin continue } - dst[k] = dyn.NewValue(v.Value(), []dyn.Location{{Line: order.Get(k)}}) + dst[k] = dyn.NewValue(v.Value(), dyn.Locations{{Line: order.Get(k)}}) } return dyn.V(dst), nil diff --git a/libs/dyn/yamlsaver/utils_test.go b/libs/dyn/yamlsaver/utils_test.go index 1afab601ab..5c15849333 100644 --- a/libs/dyn/yamlsaver/utils_test.go +++ b/libs/dyn/yamlsaver/utils_test.go @@ -39,19 +39,19 @@ func TestConvertToMapValueWithOrder(t *testing.T) { dyn.V("b"), dyn.V("c"), }, - []dyn.Location{{Line: -3}}, + dyn.Locations{{Line: -3}}, ), "name": dyn.NewValue( "test", - []dyn.Location{{Line: -2}}, + dyn.Locations{{Line: -2}}, ), "map": dyn.NewValue( map[string]dyn.Value{ "key1": dyn.V("value1"), "key2": dyn.V("value2"), }, - []dyn.Location{{Line: -1}}, + dyn.Locations{{Line: -1}}, ), - "long_name_field": dyn.NewValue("long name goes here", []dyn.Location{{Line: 1}}), + "long_name_field": dyn.NewValue("long name goes here", dyn.Locations{{Line: 1}}), }), result) }