Skip to content

Commit f0a07fe

Browse files
authored
internal: structpath: remove prefix dot from string repr (#3612)
## Why We're going to use paths in the plan output, the dot is different from general format used by DABs. For example, we refer to resources as "jobs.foo" or "resources.jobs.foo", no leading dot. The references don't use leading dot either: `${resources.jobs.foo.id}`.
1 parent fd1451f commit f0a07fe

File tree

11 files changed

+149
-145
lines changed

11 files changed

+149
-145
lines changed

bundle/terranova/tnresources/adapter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type IResource interface {
4141
DoDelete(ctx context.Context, id string) error
4242

4343
// [Optional] FieldTriggers returns actions to trigger when given fields are changed.
44-
// Keys are field paths (e.g., ".name", ".catalog_name"). Values are actions.
44+
// Keys are field paths (e.g., "name", "catalog_name"). Values are actions.
4545
// Unspecified changed fields default to ActionTypeUpdate.
4646
FieldTriggers() map[string]deployplan.ActionType
4747
}

bundle/terranova/tnresources/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (r *ResourceApp) DoDelete(ctx context.Context, id string) error {
6666

6767
func (*ResourceApp) FieldTriggers() map[string]deployplan.ActionType {
6868
return map[string]deployplan.ActionType{
69-
".name": deployplan.ActionTypeRecreate,
69+
"name": deployplan.ActionTypeRecreate,
7070
}
7171
}
7272

bundle/terranova/tnresources/pipeline.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@ func (r *ResourcePipeline) DoDelete(ctx context.Context, id string) error {
118118

119119
func (*ResourcePipeline) FieldTriggers() map[string]deployplan.ActionType {
120120
return map[string]deployplan.ActionType{
121-
".storage": deployplan.ActionTypeRecreate,
122-
".catalog": deployplan.ActionTypeRecreate,
123-
".ingestion_definition.connection_name": deployplan.ActionTypeRecreate,
124-
".ingestion_definition.ingestion_gateway_id": deployplan.ActionTypeRecreate,
121+
"storage": deployplan.ActionTypeRecreate,
122+
"catalog": deployplan.ActionTypeRecreate,
123+
"ingestion_definition.connection_name": deployplan.ActionTypeRecreate,
124+
"ingestion_definition.ingestion_gateway_id": deployplan.ActionTypeRecreate,
125125
}
126126
}
127127

bundle/terranova/tnresources/schema.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func (r *ResourceSchema) DoDelete(ctx context.Context, id string) error {
7979

8080
func (*ResourceSchema) FieldTriggers() map[string]deployplan.ActionType {
8181
return map[string]deployplan.ActionType{
82-
".name": deployplan.ActionTypeRecreate,
83-
".catalog_name": deployplan.ActionTypeRecreate,
84-
".storage_root": deployplan.ActionTypeRecreate,
82+
"name": deployplan.ActionTypeRecreate,
83+
"catalog_name": deployplan.ActionTypeRecreate,
84+
"storage_root": deployplan.ActionTypeRecreate,
8585
}
8686
}

bundle/terranova/tnresources/volume.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ func (r *ResourceVolume) DoDelete(ctx context.Context, id string) error {
114114

115115
func (*ResourceVolume) FieldTriggers() map[string]deployplan.ActionType {
116116
return map[string]deployplan.ActionType{
117-
".catalog_name": deployplan.ActionTypeRecreate,
118-
".schema_name": deployplan.ActionTypeRecreate,
119-
".storage_location": deployplan.ActionTypeRecreate,
120-
".volume_type": deployplan.ActionTypeRecreate,
121-
".name": deployplan.ActionTypeUpdateWithID,
117+
"catalog_name": deployplan.ActionTypeRecreate,
118+
"schema_name": deployplan.ActionTypeRecreate,
119+
"storage_location": deployplan.ActionTypeRecreate,
120+
"volume_type": deployplan.ActionTypeRecreate,
121+
"name": deployplan.ActionTypeUpdateWithID,
122122
}
123123
}
124124

libs/structs/structdiff/diff_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,112 +102,112 @@ func TestGetStructDiff(t *testing.T) {
102102
name: "simple field change - omitempty",
103103
a: A{X: 5},
104104
b: A{},
105-
want: []ResolvedChange{{Field: ".x", Old: 5, New: nil}},
105+
want: []ResolvedChange{{Field: "x", Old: 5, New: nil}},
106106
},
107107
{
108108
name: "simple field change - required",
109109
a: A{XX: 5},
110110
b: A{},
111-
want: []ResolvedChange{{Field: ".xx", Old: 5, New: 0}},
111+
want: []ResolvedChange{{Field: "xx", Old: 5, New: 0}},
112112
},
113113
{
114114
name: "nested struct field",
115115
a: A{B: B{S: "one"}},
116116
b: A{B: B{S: "two"}},
117-
want: []ResolvedChange{{Field: ".b.S", Old: "one", New: "two"}},
117+
want: []ResolvedChange{{Field: "b.S", Old: "one", New: "two"}},
118118
},
119119
{
120120
name: "pointer nil vs value",
121121
a: A{P: b1},
122122
b: A{},
123-
want: []ResolvedChange{{Field: ".p", Old: b1, New: nil}},
123+
want: []ResolvedChange{{Field: "p", Old: b1, New: nil}},
124124
},
125125
{
126126
name: "pointer nested value diff",
127127
a: A{P: b1},
128128
b: A{P: b2},
129-
want: []ResolvedChange{{Field: ".p.S", Old: "one", New: "two"}},
129+
want: []ResolvedChange{{Field: "p.S", Old: "one", New: "two"}},
130130
},
131131
{
132132
name: "map diff",
133133
a: A{M: map[string]int{"a": 1}},
134134
b: A{M: map[string]int{"a": 2}},
135-
want: []ResolvedChange{{Field: ".m[\"a\"]", Old: 1, New: 2}},
135+
want: []ResolvedChange{{Field: "m[\"a\"]", Old: 1, New: 2}},
136136
},
137137
{
138138
name: "slice diff",
139139
a: A{L: []string{"a"}},
140140
b: A{L: []string{"a", "b"}},
141-
want: []ResolvedChange{{Field: ".l", Old: []string{"a"}, New: []string{"a", "b"}}},
141+
want: []ResolvedChange{{Field: "l", Old: []string{"a"}, New: []string{"a", "b"}}},
142142
},
143143

144144
// ForceSendFields with non-empty fields (omitempty)
145145
{
146146
name: "forcesend nonempty 1",
147147
a: C{Name: "Hello", ForceSendFields: []string{"Name"}},
148148
b: C{Name: "World"},
149-
want: []ResolvedChange{{Field: ".name", Old: "Hello", New: "World"}},
149+
want: []ResolvedChange{{Field: "name", Old: "Hello", New: "World"}},
150150
},
151151
{
152152
name: "forcesend noneempty 2",
153153
a: C{Name: "Hello", ForceSendFields: []string{"Name"}},
154154
b: C{Name: "World", ForceSendFields: []string{"Name"}},
155-
want: []ResolvedChange{{Field: ".name", Old: "Hello", New: "World"}},
155+
want: []ResolvedChange{{Field: "name", Old: "Hello", New: "World"}},
156156
},
157157
{
158158
name: "forcesend noneempty 3",
159159
a: C{Name: "Hello"},
160160
b: C{Name: "World", ForceSendFields: []string{"Name"}},
161-
want: []ResolvedChange{{Field: ".name", Old: "Hello", New: "World"}},
161+
want: []ResolvedChange{{Field: "name", Old: "Hello", New: "World"}},
162162
},
163163

164164
// ForceSendFields with non-empty fields (required)
165165
{
166166
name: "forcesend nonempty required 1",
167167
a: C{Title: "Hello", ForceSendFields: []string{"Title"}},
168168
b: C{Title: "World"},
169-
want: []ResolvedChange{{Field: ".title", Old: "Hello", New: "World"}},
169+
want: []ResolvedChange{{Field: "title", Old: "Hello", New: "World"}},
170170
},
171171
{
172172
name: "forcesend noneempty required 2",
173173
a: C{Title: "Hello", ForceSendFields: []string{"Title"}},
174174
b: C{Title: "World", ForceSendFields: []string{"Title"}},
175-
want: []ResolvedChange{{Field: ".title", Old: "Hello", New: "World"}},
175+
want: []ResolvedChange{{Field: "title", Old: "Hello", New: "World"}},
176176
},
177177
{
178178
name: "forcesend noneempty required 3",
179179
a: C{Title: "Hello"},
180180
b: C{Title: "World", ForceSendFields: []string{"Title"}},
181-
want: []ResolvedChange{{Field: ".title", Old: "Hello", New: "World"}},
181+
want: []ResolvedChange{{Field: "title", Old: "Hello", New: "World"}},
182182
},
183183

184184
// ForceSendFields with empty fields
185185
{
186186
name: "forcesend empty string diff",
187187
a: C{ForceSendFields: []string{"Name"}}, // Name == "" zero, but forced
188188
b: C{},
189-
want: []ResolvedChange{{Field: ".name", Old: "", New: nil}},
189+
want: []ResolvedChange{{Field: "name", Old: "", New: nil}},
190190
},
191191
{
192192
name: "forcesend empty int diff",
193193
a: C{ForceSendFields: []string{"Age"}},
194194
b: C{},
195-
want: []ResolvedChange{{Field: ".age", Old: 0, New: nil}},
195+
want: []ResolvedChange{{Field: "age", Old: 0, New: nil}},
196196
},
197197
{
198198
name: "forcesend empty bool diff",
199199
a: C{ForceSendFields: []string{"IsEnabled"}},
200200
b: C{},
201-
want: []ResolvedChange{{Field: ".is_enabled", Old: false, New: nil}},
201+
want: []ResolvedChange{{Field: "is_enabled", Old: false, New: nil}},
202202
},
203203
{
204204
name: "forcesend empty all",
205205
a: C{ForceSendFields: []string{"Name", "IsEnabled"}},
206206
b: C{ForceSendFields: []string{"Age"}},
207207
want: []ResolvedChange{
208-
{Field: ".name", Old: "", New: nil},
209-
{Field: ".age", Old: nil, New: 0},
210-
{Field: ".is_enabled", Old: false, New: nil},
208+
{Field: "name", Old: "", New: nil},
209+
{Field: "age", Old: nil, New: 0},
210+
{Field: "is_enabled", Old: false, New: nil},
211211
},
212212
},
213213
{

libs/structs/structdiff/jobsettings_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -474,54 +474,54 @@ func TestJobDiff(t *testing.T) {
474474
changes, err := GetStructDiff(job, zero)
475475
require.NoError(t, err)
476476
require.GreaterOrEqual(t, len(changes), 75)
477-
assert.Equal(t, ".budget_policy_id", changes[0].Path.String())
477+
assert.Equal(t, "budget_policy_id", changes[0].Path.String())
478478
assert.Equal(t, "550e8400-e29b-41d4-a716-446655440000", changes[0].Old)
479479
assert.Equal(t, "", changes[0].New)
480480
// Note: pause_status shows up as nil here because Continous does not have ForceSendFields field
481-
assert.Equal(t, ".continuous.pause_status", changes[1].Path.String())
481+
assert.Equal(t, "continuous.pause_status", changes[1].Path.String())
482482
assert.Equal(t, jobs.PauseStatus("UNPAUSED"), changes[1].Old)
483483
assert.Nil(t, changes[1].New)
484-
assert.Equal(t, ".deployment.kind", changes[2].Path.String())
484+
assert.Equal(t, "deployment.kind", changes[2].Path.String())
485485
assert.Equal(t, jobs.JobDeploymentKind("BUNDLE"), changes[2].Old)
486486
assert.Equal(t, jobs.JobDeploymentKind(""), changes[2].New)
487-
assert.Equal(t, ".deployment.metadata_file_path", changes[3].Path.String())
487+
assert.Equal(t, "deployment.metadata_file_path", changes[3].Path.String())
488488
assert.Equal(t, "string", changes[3].Old)
489489
assert.Equal(t, "", changes[3].New)
490490

491491
changes, err = GetStructDiff(job, nils)
492492
require.NoError(t, err)
493493
require.GreaterOrEqual(t, len(changes), 77)
494-
assert.Equal(t, ".budget_policy_id", changes[0].Path.String())
494+
assert.Equal(t, "budget_policy_id", changes[0].Path.String())
495495
assert.Equal(t, "550e8400-e29b-41d4-a716-446655440000", changes[0].Old)
496496
assert.Nil(t, changes[0].New)
497497

498498
// continous is completely deleted from jobExampleResponseNils
499-
assert.Equal(t, ".continuous", changes[1].Path.String())
499+
assert.Equal(t, "continuous", changes[1].Path.String())
500500
assert.Equal(t, &jobs.Continuous{PauseStatus: "UNPAUSED"}, changes[1].Old)
501501
assert.Nil(t, changes[1].New)
502502

503503
// deployment.kind is not omitempty field, so it does not show up as nil here
504-
assert.Equal(t, ".deployment.kind", changes[2].Path.String())
504+
assert.Equal(t, "deployment.kind", changes[2].Path.String())
505505
assert.Equal(t, jobs.JobDeploymentKind("BUNDLE"), changes[2].Old)
506506
assert.Equal(t, jobs.JobDeploymentKind(""), changes[2].New)
507507

508-
assert.Equal(t, ".deployment.metadata_file_path", changes[3].Path.String())
508+
assert.Equal(t, "deployment.metadata_file_path", changes[3].Path.String())
509509
assert.Equal(t, "string", changes[3].Old)
510510
assert.Nil(t, changes[3].New)
511511

512512
changes, err = GetStructDiff(zero, nils)
513513
require.NoError(t, err)
514514
assert.GreaterOrEqual(t, len(changes), 58)
515-
assert.Equal(t, ".budget_policy_id", changes[0].Path.String())
515+
assert.Equal(t, "budget_policy_id", changes[0].Path.String())
516516
assert.Equal(t, "", changes[0].Old)
517517
assert.Nil(t, changes[0].New)
518-
assert.Equal(t, ".continuous", changes[1].Path.String())
518+
assert.Equal(t, "continuous", changes[1].Path.String())
519519
assert.Equal(t, &jobs.Continuous{}, changes[1].Old)
520520
assert.Nil(t, changes[1].New)
521521

522522
// deployment.kind is "" in both
523523

524-
assert.Equal(t, ".deployment.metadata_file_path", changes[2].Path.String())
524+
assert.Equal(t, "deployment.metadata_file_path", changes[2].Path.String())
525525
assert.Equal(t, "", changes[2].Old)
526526
assert.Nil(t, changes[2].New)
527527
}

libs/structs/structpath/path.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ func (p *PathNode) String() string {
159159
}
160160

161161
if p.index == tagStruct {
162-
return p.prev.String() + "." + p.key
162+
prev := p.prev.String()
163+
if prev == "" {
164+
return p.key
165+
}
166+
return prev + "." + p.key
163167
}
164168

165169
return fmt.Sprintf("%s[%q]", p.prev.String(), p.key)

libs/structs/structpath/path_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,28 @@ func TestPathNode(t *testing.T) {
4343
{
4444
name: "struct field with JSON tag",
4545
node: NewStructField(nil, reflect.StructTag(`json:"json_name"`), "GoFieldName"),
46-
String: ".json_name",
46+
String: "json_name",
4747
DynPath: "json_name",
4848
Field: "json_name",
4949
},
5050
{
5151
name: "struct field without JSON tag (fallback to Go name)",
5252
node: NewStructField(nil, reflect.StructTag(""), "GoFieldName"),
53-
String: ".GoFieldName",
53+
String: "GoFieldName",
5454
DynPath: "GoFieldName",
5555
Field: "GoFieldName",
5656
},
5757
{
5858
name: "struct field with dash JSON tag",
5959
node: NewStructField(nil, reflect.StructTag(`json:"-"`), "GoFieldName"),
60-
String: ".-",
60+
String: "-",
6161
DynPath: "-",
6262
Field: "-",
6363
},
6464
{
6565
name: "struct field with JSON tag options",
6666
node: NewStructField(nil, reflect.StructTag(`json:"lazy_field,omitempty"`), "LazyField"),
67-
String: ".lazy_field",
67+
String: "lazy_field",
6868
DynPath: "lazy_field",
6969
Field: "lazy_field",
7070
},
@@ -86,21 +86,21 @@ func TestPathNode(t *testing.T) {
8686
{
8787
name: "struct field -> array index",
8888
node: NewIndex(NewStructField(nil, reflect.StructTag(`json:"items"`), "Items"), 3),
89-
String: ".items[3]",
89+
String: "items[3]",
9090
DynPath: "items[3]",
9191
Index: 3,
9292
},
9393
{
9494
name: "struct field -> map key",
9595
node: NewMapKey(NewStructField(nil, reflect.StructTag(`json:"config"`), "Config"), "database"),
96-
String: `.config["database"]`,
96+
String: `config["database"]`,
9797
DynPath: "config.database",
9898
MapKey: "database",
9999
},
100100
{
101101
name: "struct field -> struct field",
102102
node: NewStructField(NewStructField(nil, reflect.StructTag(`json:"user"`), "User"), reflect.StructTag(`json:"name"`), "Name"),
103-
String: ".user.name",
103+
String: "user.name",
104104
DynPath: "user.name",
105105
Field: "name",
106106
},
@@ -134,21 +134,21 @@ func TestPathNode(t *testing.T) {
134134
{
135135
name: "struct field without JSON tag -> struct field with JSON tag",
136136
node: NewStructField(NewStructField(nil, reflect.StructTag(""), "Parent"), reflect.StructTag(`json:"child_name"`), "ChildName"),
137-
String: ".Parent.child_name",
137+
String: "Parent.child_name",
138138
DynPath: "Parent.child_name",
139139
Field: "child_name",
140140
},
141141
{
142142
name: "any key",
143143
node: NewAnyKey(NewStructField(nil, reflect.StructTag(""), "Parent")),
144-
String: ".Parent[*]",
144+
String: "Parent[*]",
145145
DynPath: "Parent.*",
146146
AnyKey: true,
147147
},
148148
{
149149
name: "any index",
150150
node: NewAnyIndex(NewStructField(nil, reflect.StructTag(""), "Parent")),
151-
String: ".Parent[*]",
151+
String: "Parent[*]",
152152
DynPath: "Parent[*]",
153153
AnyIndex: true,
154154
},

0 commit comments

Comments
 (0)