Skip to content

Commit 3cf1b24

Browse files
authored
dyn: Use nil as default location instead of empty slice (#2742)
## Why - This starts to matter once we replace dynassert with regular assert #2737 - We agreed before to use nils consistently instead of empty slices.
1 parent 5bb02c9 commit 3cf1b24

8 files changed

Lines changed: 15 additions & 19 deletions

File tree

bundle/config/generate/job.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func ConvertJobToValue(job *jobs.Job) (dyn.Value, error) {
3434
"name": dyn.NewValue(parameter.Name, []dyn.Location{{Line: 0}}), // We use Line: 0 to ensure that the name goes first.
3535
"default": dyn.NewValue(parameter.Default, []dyn.Location{{Line: 1}}),
3636
}
37-
params = append(params, dyn.NewValue(p, []dyn.Location{}))
37+
params = append(params, dyn.V(p))
3838
}
3939

4040
value["parameters"] = dyn.NewValue(params, []dyn.Location{{Line: jobOrder.Get("parameters")}})

bundle/config/mutator/python/apply_python_output_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ func TestCreateOverrideVisitor_omitempty(t *testing.T) {
254254
}
255255

256256
func mapOf(key string, value dyn.Value) dyn.Value {
257-
return dyn.NewValue(map[string]dyn.Value{
257+
return dyn.V(map[string]dyn.Value{
258258
key: value,
259-
}, []dyn.Location{})
259+
})
260260
}
261261

262262
func mapOf2(key1 string, value1 dyn.Value, key2 string, value2 dyn.Value) dyn.Value {
@@ -267,5 +267,5 @@ func mapOf2(key1 string, value1 dyn.Value, key2 string, value2 dyn.Value) dyn.Va
267267
}
268268

269269
func emptyMap() dyn.Value {
270-
return dyn.NewValue(map[string]dyn.Value{}, []dyn.Location{})
270+
return dyn.V(map[string]dyn.Value{})
271271
}

bundle/config/mutator/python/python_locations_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ func TestMergeLocations(t *testing.T) {
2222

2323
input := dyn.NewValue(
2424
map[string]dyn.Value{
25-
"foo": dyn.NewValue(
25+
"foo": dyn.V(
2626
map[string]dyn.Value{
2727
"baz": dyn.NewValue("baz", []dyn.Location{yamlLocation}),
2828
"qux": dyn.NewValue("baz", []dyn.Location{generatedLocation, yamlLocation}),
2929
},
30-
[]dyn.Location{},
3130
),
3231
"bar": dyn.NewValue("baz", []dyn.Location{generatedLocation}),
3332
},

bundle/config/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ func (r Root) GetLocation(path string) dyn.Location {
555555
func (r Root) GetLocations(path string) []dyn.Location {
556556
v, err := dyn.Get(r.value, path)
557557
if err != nil {
558-
return []dyn.Location{}
558+
return nil
559559
}
560560
return v.Locations()
561561
}

bundle/config/validate/unique_resource_keys.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func (m *uniqueResourceKeys) Apply(ctx context.Context, b *bundle.Bundle) diag.D
5959
m, ok := resourceMetadata[k]
6060
if !ok {
6161
m = &metadata{
62-
paths: []dyn.Path{},
63-
locations: []dyn.Location{},
62+
paths: nil,
63+
locations: nil,
6464
}
6565
}
6666

libs/dyn/mapping_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ func TestMappingSetLoc(t *testing.T) {
125125
func TestMappingKeysValues(t *testing.T) {
126126
// Configure mapping
127127
var m dyn.Mapping
128-
m.SetLoc("key1", []dyn.Location{}, dyn.V("foo"))
129-
m.SetLoc("key2", []dyn.Location{}, dyn.V("bar"))
128+
m.SetLoc("key1", nil, dyn.V("foo"))
129+
m.SetLoc("key2", nil, dyn.V("bar"))
130130

131131
// Confirm keys
132132
keys := m.Keys()

libs/dyn/value.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var NilValue = Value{
3232

3333
// V constructs a new Value with the given value.
3434
func V(v any) Value {
35-
return NewValue(v, []Location{})
35+
return NewValue(v, nil)
3636
}
3737

3838
// NewValue constructs a new Value with the given value and location.

libs/dyn/yamlsaver/utils_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,22 @@ func TestConvertToMap(t *testing.T) {
3232
result, err := ConvertToMapValue(v, nil, []string{"format"}, map[string]dyn.Value{})
3333
assert.NoError(t, err)
3434
assert.Equal(t, dyn.V(map[string]dyn.Value{
35-
"list": dyn.NewValue(
35+
"list": dyn.V(
3636
[]dyn.Value{
3737
dyn.V("a"),
3838
dyn.V("b"),
3939
dyn.V("c"),
4040
},
41-
[]dyn.Location{},
4241
),
43-
"long_name_field": dyn.NewValue("long name goes here", []dyn.Location{}),
44-
"map": dyn.NewValue(
42+
"long_name_field": dyn.V("long name goes here"),
43+
"map": dyn.V(
4544
map[string]dyn.Value{
4645
"key1": dyn.V("value1"),
4746
"key2": dyn.V("value2"),
4847
},
49-
[]dyn.Location{},
5048
),
51-
"name": dyn.NewValue(
49+
"name": dyn.V(
5250
"test",
53-
[]dyn.Location{},
5451
),
5552
}), result)
5653
}

0 commit comments

Comments
 (0)