Skip to content

Commit 9da018f

Browse files
denikIsaac
andcommitted
config: GetLocationsOf/GetLocationOf/DefinitionLocationOf for path nodes
Co-authored-by: Isaac <no-reply@databricks.com>
1 parent f74ca63 commit 9da018f

20 files changed

Lines changed: 71 additions & 46 deletions

‎.agents/rules/style-guide-go.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ return fieldPaths
119119

120120
### Structpath
121121

122-
**RULE: Build computed structpath paths and patterns from parts (`structpath.NewPath`, `NewPathSlice`, `NewPattern`); never format or concatenate a string to parse it.** `MustParsePath`/`MustParsePattern`/`MustParsePaths` are for string literals and tests; `ParsePath` with the error handled is for user input. The `NoComputedStructpathParse` ruleguard rule enforces this.
122+
**RULE: Build computed structpath paths and patterns from parts (`structpath.NewPath`, `NewPathSlice`, `NewPattern`); never format or concatenate a string to parse it (for location lookups pass the node to `GetLocationsOf`).** `MustParsePath`/`MustParsePattern`/`MustParsePaths` are for string literals and tests; `ParsePath` with the error handled is for user input. The `NoComputedStructpathParse` ruleguard rule enforces this.
123123

124124
### Environment variables
125125

‎bundle/apps/validate.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (v *validate) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics
2828
Severity: diag.Error,
2929
Summary: "Missing app source code path or git source",
3030
Detail: fmt.Sprintf("app resource '%s' should have either source_code_path or git_source field", key),
31-
Locations: b.Config.GetLocations("resources.apps." + key),
31+
Locations: b.Config.GetLocationsOf(structpath.NewPath(nil, "resources", "apps", key)),
3232
})
3333
continue
3434
}
@@ -38,7 +38,7 @@ func (v *validate) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics
3838
Severity: diag.Error,
3939
Summary: "Both source_code_path and git_source fields are set",
4040
Detail: fmt.Sprintf("app resource '%s' should have either source_code_path or git_source field, not both", key),
41-
Locations: b.Config.GetLocations("resources.apps." + key),
41+
Locations: b.Config.GetLocationsOf(structpath.NewPath(nil, "resources", "apps", key)),
4242
})
4343
continue
4444
}
@@ -48,7 +48,7 @@ func (v *validate) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics
4848
Severity: diag.Error,
4949
Summary: "Duplicate app source code path",
5050
Detail: fmt.Sprintf("app resource '%s' has the same source code path as app resource '%s', this will lead to the app configuration being overridden by each other", key, usedSourceCodePaths[app.SourceCodePath]),
51-
Locations: b.Config.GetLocations(fmt.Sprintf("resources.apps.%s.source_code_path", key)),
51+
Locations: b.Config.GetLocationsOf(structpath.NewPath(nil, "resources", "apps", key, "source_code_path")),
5252
})
5353
}
5454
usedSourceCodePaths[app.SourceCodePath] = key
@@ -149,7 +149,7 @@ func warnForAppResourcePermissions(b *bundle.Bundle, appKey string, app *resourc
149149
continue
150150
}
151151

152-
appPath := "resources.apps." + appKey
152+
appPath := structpath.NewPath(nil, "resources", "apps", appKey)
153153
diags = append(diags, diag.Diagnostic{
154154
Severity: diag.Warning,
155155
Summary: fmt.Sprintf("app %q references %s %q which has permissions set. To prevent permission override after deploying the app, please add the app service principal to the %s permissions", appKey, refType, resourceKey, refType),
@@ -167,8 +167,8 @@ func warnForAppResourcePermissions(b *bundle.Bundle, appKey string, app *resourc
167167
ref.permission,
168168
appKey,
169169
),
170-
Paths: structpath.NewPathSlice("resources", "apps", appKey),
171-
Locations: b.Config.GetLocations(appPath),
170+
Paths: []*structpath.PathNode{appPath},
171+
Locations: b.Config.GetLocationsOf(appPath),
172172
})
173173
}
174174

‎bundle/artifacts/prepare.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/databricks/cli/libs/log"
1818
"github.com/databricks/cli/libs/logdiag"
1919
"github.com/databricks/cli/libs/python"
20+
"github.com/databricks/cli/libs/structs/structpath"
2021
)
2122

2223
func Prepare() bundle.Mutator {
@@ -38,7 +39,7 @@ func (m *prepare) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics
3839
for _, artifactName := range slices.Sorted(maps.Keys(b.Config.Artifacts)) {
3940
artifact := b.Config.Artifacts[artifactName]
4041
if artifact == nil {
41-
l := b.Config.GetLocation("artifacts." + artifactName)
42+
l := b.Config.GetLocationOf(structpath.NewPath(nil, "artifacts", artifactName))
4243
logdiag.LogDiag(ctx, diag.Diagnostic{
4344
Severity: diag.Error,
4445
Summary: "Artifact not properly configured",
@@ -61,7 +62,7 @@ func (m *prepare) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics
6162
logdiag.LogError(ctx, fmt.Errorf("artifact %q: a tgz artifact needs a `files` entry naming the output path", artifactName))
6263
}
6364

64-
l := b.Config.DefinitionLocation("artifacts." + artifactName)
65+
l := b.Config.DefinitionLocationOf(structpath.NewPath(nil, "artifacts", artifactName))
6566
dirPath := filepath.Dir(l.File)
6667

6768
// Check if source paths are absolute, if not, make them absolute

‎bundle/config/loader/process_root_includes.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/databricks/cli/bundle/config"
1212
"github.com/databricks/cli/libs/diag"
1313
"github.com/databricks/cli/libs/logdiag"
14+
"github.com/databricks/cli/libs/structs/structpath"
1415
)
1516

1617
type processRootIncludes struct{}
@@ -112,7 +113,7 @@ func (m *processRootIncludes) Apply(ctx context.Context, b *bundle.Bundle) diag.
112113
Summary: "Files in the 'include' configuration section must be YAML or JSON files.",
113114
Detail: fmt.Sprintf("The file %s in the 'include' configuration section is not a YAML or JSON file, and only such files are supported. To include files to sync, specify them in the 'sync.include' configuration section instead.", rel),
114115
// The match's index within the glob is unrelated to the entry's position in the include list.
115-
Locations: b.Config.GetLocations(fmt.Sprintf("include[%d]", entryIndex)),
116+
Locations: b.Config.GetLocationsOf(structpath.NewPath(nil, "include", entryIndex)),
116117
})
117118
continue
118119
}

‎bundle/config/mutator/apply_source_linked_deployment_preset.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (m *applySourceLinkedDeploymentPreset) Apply(ctx context.Context, b *bundle
8181
Summary: "workspace.file_path setting will be ignored in source-linked deployment mode",
8282
Detail: "In source-linked deployment files are not copied to the destination and resources use source files instead",
8383
Paths: []*structpath.PathNode{path},
84-
Locations: b.Config.GetLocations(path.String()),
84+
Locations: b.Config.GetLocationsOf(path),
8585
},
8686
)
8787
}

‎bundle/config/mutator/resolve_job_run_file_triggers.go‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/databricks/cli/bundle"
1616
"github.com/databricks/cli/bundle/config/resources"
1717
"github.com/databricks/cli/libs/diag"
18+
"github.com/databricks/cli/libs/structs/structpath"
1819
libsync "github.com/databricks/cli/libs/sync"
1920
)
2021

@@ -65,7 +66,7 @@ func (*resolveJobRunFileTriggers) Apply(ctx context.Context, b *bundle.Bundle) d
6566
if t.OnFileChange == nil {
6667
continue
6768
}
68-
path := fmt.Sprintf("resources.job_runs.%s.lifecycle.triggers[%d].on_file_change", name, i)
69+
path := structpath.NewPath(nil, "resources", "job_runs", name, "lifecycle", "triggers", i, "on_file_change")
6970
pattern, fingerprint, d := resolveFileTrigger(b, path, *t.OnFileChange, syncable)
7071
diags = diags.Extend(d)
7172
if !d.HasError() {
@@ -105,7 +106,7 @@ func listSyncableRelPaths(ctx context.Context, b *bundle.Bundle) ([]string, erro
105106
return out, nil
106107
}
107108

108-
func resolveFileTrigger(b *bundle.Bundle, loc, pattern string, syncable []string) (string, string, diag.Diagnostics) {
109+
func resolveFileTrigger(b *bundle.Bundle, loc *structpath.PathNode, pattern string, syncable []string) (string, string, diag.Diagnostics) {
109110
relPattern, diags := validateFileTriggerPattern(b, loc, pattern)
110111
if diags.HasError() {
111112
return "", "", diags
@@ -119,7 +120,7 @@ func resolveFileTrigger(b *bundle.Bundle, loc, pattern string, syncable []string
119120
diags = diags.Append(diag.Diagnostic{
120121
Severity: diag.Error,
121122
Summary: fileTriggerPrefix + fmt.Sprintf("invalid pattern %q: %s", pattern, err),
122-
Locations: b.Config.GetLocations(loc),
123+
Locations: b.Config.GetLocationsOf(loc),
123124
})
124125
continue
125126
}
@@ -131,7 +132,7 @@ func resolveFileTrigger(b *bundle.Bundle, loc, pattern string, syncable []string
131132
diags = diags.Append(diag.Diagnostic{
132133
Severity: diag.Error,
133134
Summary: fileTriggerPrefix + fmt.Sprintf("hash %q: %s", rel, err),
134-
Locations: b.Config.GetLocations(loc),
135+
Locations: b.Config.GetLocationsOf(loc),
135136
})
136137
continue
137138
}
@@ -145,20 +146,20 @@ func resolveFileTrigger(b *bundle.Bundle, loc, pattern string, syncable []string
145146
diags = diags.Append(diag.Diagnostic{
146147
Severity: diag.Warning,
147148
Summary: fileTriggerPrefix + fmt.Sprintf("no synced files match %q", pattern),
148-
Locations: b.Config.GetLocations(loc),
149+
Locations: b.Config.GetLocationsOf(loc),
149150
})
150151
}
151152
return relPattern, hex.EncodeToString(h.Sum(nil)), diags
152153
}
153154

154-
func validateFileTriggerPattern(b *bundle.Bundle, loc, pattern string) (string, diag.Diagnostics) {
155+
func validateFileTriggerPattern(b *bundle.Bundle, loc *structpath.PathNode, pattern string) (string, diag.Diagnostics) {
155156
var diags diag.Diagnostics
156157
// A double star looks recursive but path.Match treats it as two ordinary stars.
157158
if strings.Contains(pattern, "**") {
158159
return "", diags.Append(diag.Diagnostic{
159160
Severity: diag.Error,
160161
Summary: fileTriggerPrefix + fmt.Sprintf("** in %q is not supported; use * for a single directory level", pattern),
161-
Locations: b.Config.GetLocations(loc),
162+
Locations: b.Config.GetLocationsOf(loc),
162163
})
163164
}
164165
// Reject a genuinely absolute path; Join would otherwise silently reinterpret it
@@ -170,7 +171,7 @@ func validateFileTriggerPattern(b *bundle.Bundle, loc, pattern string) (string,
170171
return "", diags.Append(diag.Diagnostic{
171172
Severity: diag.Error,
172173
Summary: fileTriggerPrefix + fmt.Sprintf("pattern %q must be relative to the defining YAML file", pattern),
173-
Locations: b.Config.GetLocations(loc),
174+
Locations: b.Config.GetLocationsOf(loc),
174175
})
175176
}
176177
// NormalizePaths has already rewritten YAML-relative globs to be bundle-root
@@ -182,7 +183,7 @@ func validateFileTriggerPattern(b *bundle.Bundle, loc, pattern string) (string,
182183
return "", diags.Append(diag.Diagnostic{
183184
Severity: diag.Error,
184185
Summary: fileTriggerPrefix + fmt.Sprintf("pattern %q is not under the sync root", pattern),
185-
Locations: b.Config.GetLocations(loc),
186+
Locations: b.Config.GetLocationsOf(loc),
186187
})
187188
}
188189
relPattern = filepath.ToSlash(relPattern)
@@ -191,7 +192,7 @@ func validateFileTriggerPattern(b *bundle.Bundle, loc, pattern string) (string,
191192
return "", diags.Append(diag.Diagnostic{
192193
Severity: diag.Error,
193194
Summary: fileTriggerPrefix + fmt.Sprintf("invalid pattern %q: %s", pattern, err),
194-
Locations: b.Config.GetLocations(loc),
195+
Locations: b.Config.GetLocationsOf(loc),
195196
})
196197
}
197198
return relPattern, diags

‎bundle/config/mutator/resourcemutator/model_serving_endpoint_fixups.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/databricks/cli/bundle"
77
"github.com/databricks/cli/libs/diag"
8+
"github.com/databricks/cli/libs/structs/structpath"
89
"github.com/databricks/cli/libs/utils"
910
"github.com/databricks/databricks-sdk-go/service/serving"
1011
)
@@ -56,7 +57,7 @@ func (m *modelServingEndpointFixups) Apply(ctx context.Context, b *bundle.Bundle
5657
Summary: "Cannot use both served_models and served_entities",
5758
Detail: "Model serving endpoint cannot specify both served_models and served_entities at the same time.",
5859
Locations: []diag.Location{
59-
b.Config.GetLocation("resources.model_serving_endpoints." + key),
60+
b.Config.GetLocationOf(structpath.NewPath(nil, "resources", "model_serving_endpoints", key)),
6061
},
6162
})
6263
continue
@@ -71,7 +72,7 @@ func (m *modelServingEndpointFixups) Apply(ctx context.Context, b *bundle.Bundle
7172
Summary: "Using served_models is deprecated",
7273
Detail: "The served_models field is deprecated. Please use served_entities instead.",
7374
Locations: []diag.Location{
74-
b.Config.GetLocation("resources.model_serving_endpoints." + key + ".config.served_models"),
75+
b.Config.GetLocationOf(structpath.NewPath(nil, "resources", "model_serving_endpoints", key, "config", "served_models")),
7576
},
7677
})
7778

‎bundle/config/mutator/resourcemutator/secret_scope_fixups.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (m *secretScopeFixups) Apply(ctx context.Context, b *bundle.Bundle) diag.Di
140140
Summary: "Failed to collapse permissions for secret scope",
141141
Detail: err.Error(),
142142
Paths: []*structpath.PathNode{path},
143-
Locations: []diag.Location{b.Config.GetLocation(path.String())},
143+
Locations: []diag.Location{b.Config.GetLocationOf(path)},
144144
},
145145
}
146146
}

‎bundle/config/mutator/sync_infer_root.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (m *syncInferRoot) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagno
9393
diags = append(diags, diag.Diagnostic{
9494
Severity: diag.Error,
9595
Summary: fmt.Sprintf("invalid sync path %q", path),
96-
Locations: b.Config.GetLocations(fmt.Sprintf("sync.paths[%d]", i)),
96+
Locations: b.Config.GetLocationsOf(structpath.NewPath(nil, "sync", "paths", i)),
9797
Paths: []*structpath.PathNode{structpath.NewIndex(structpath.MustParsePath("sync.paths"), i)},
9898
})
9999
}

‎bundle/config/mutator/validate_job_run_triggers.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package mutator
22

33
import (
44
"context"
5-
"fmt"
65
"strings"
76

87
"github.com/databricks/cli/bundle"
98
"github.com/databricks/cli/libs/diag"
9+
"github.com/databricks/cli/libs/structs/structpath"
1010
)
1111

1212
type validateJobRunTriggers struct{}
@@ -27,41 +27,42 @@ func (*validateJobRunTriggers) Apply(_ context.Context, b *bundle.Bundle) diag.D
2727
continue
2828
}
2929
for i, t := range jr.Lifecycle.Triggers {
30-
path := fmt.Sprintf("resources.job_runs.%s.lifecycle.triggers[%d]", name, i)
30+
path := structpath.NewPath(nil, "resources", "job_runs", name, "lifecycle", "triggers", i)
3131
if t.OnBundleDeploy == nil && t.OnFileChange == nil {
3232
diags = diags.Append(diag.Diagnostic{
3333
Severity: diag.Error,
3434
Summary: "lifecycle.triggers entry must set on_bundle_deploy or on_file_change",
35-
Locations: b.Config.GetLocations(path),
35+
Locations: b.Config.GetLocationsOf(path),
3636
})
3737
continue
3838
}
3939
if t.OnBundleDeploy != nil && t.OnFileChange != nil {
4040
diags = diags.Append(diag.Diagnostic{
4141
Severity: diag.Error,
4242
Summary: "lifecycle.triggers entry must set only one of on_bundle_deploy or on_file_change",
43-
Locations: b.Config.GetLocations(path),
43+
Locations: b.Config.GetLocationsOf(path),
4444
})
4545
continue
4646
}
4747
if t.OnBundleDeploy != nil && !*t.OnBundleDeploy {
4848
diags = diags.Append(diag.Diagnostic{
4949
Severity: diag.Error,
5050
Summary: "lifecycle.triggers.on_bundle_deploy must be true when set",
51-
Locations: b.Config.GetLocations(path + ".on_bundle_deploy"),
51+
Locations: b.Config.GetLocationsOf(structpath.NewPath(path, "on_bundle_deploy")),
5252
})
5353
}
5454
if t.OnFileChange != nil {
55+
onFileChange := structpath.NewPath(path, "on_file_change")
5556
if strings.TrimSpace(*t.OnFileChange) == "" {
5657
diags = diags.Append(diag.Diagnostic{
5758
Severity: diag.Error,
5859
Summary: "lifecycle.triggers.on_file_change must be non-empty when set",
59-
Locations: b.Config.GetLocations(path + ".on_file_change"),
60+
Locations: b.Config.GetLocationsOf(onFileChange),
6061
})
6162
continue
6263
}
6364
// Report bad patterns at validate time; hashing only runs on deploy.
64-
_, patternDiags := validateFileTriggerPattern(b, path+".on_file_change", *t.OnFileChange)
65+
_, patternDiags := validateFileTriggerPattern(b, onFileChange, *t.OnFileChange)
6566
diags = diags.Extend(patternDiags)
6667
}
6768
}

0 commit comments

Comments
 (0)