Skip to content

Commit 31eb3d5

Browse files
denikIsaac
andcommitted
structpath: build computed paths and patterns from parts; lint rule
Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 799731f commit 31eb3d5

16 files changed

Lines changed: 129 additions & 55 deletions

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ return fieldPaths
117117

118118
**RULE: Be careful with `encoding/csv` `Writer.UseCRLF = true`.** It rewrites both record terminators AND embedded newlines inside quoted fields to `\r\n`, so tests for quoted multiline fields must expect `\r\n`, not just the line endings between rows.
119119

120+
### Structpath
121+
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.
123+
120124
### Environment variables
121125

122126
**RULE: In library and product code, use `github.com/databricks/cli/libs/env` for reading environment variables, not `os.Getenv`.** `env.Get(ctx, name)` and `env.Lookup(ctx, name)` can be overridden per-context in tests, so you don't have to mutate process-wide state to exercise a code path. `os.Getenv` is still fine in `main`, tests, and acceptance/integration harnesses where no `ctx` is available and overrides aren't needed.

‎bundle/config/mutator/paths/artifact_paths_visitor.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ type artifactRewritePattern struct {
1212

1313
func artifactRewritePatterns() []artifactRewritePattern {
1414
// Base pattern to match all artifacts.
15-
base := "artifacts.*"
15+
base := structpath.NewPattern(nil, "artifacts", structpath.AnyKey)
1616

1717
// Compile list of configuration paths to rewrite.
1818
return []artifactRewritePattern{
1919
{
20-
pattern: structpath.MustParsePattern(base + ".path"),
20+
pattern: structpath.NewPattern(base, "path"),
2121
mode: TranslateModeLocalAbsoluteDirectory,
2222
},
2323
}

‎bundle/config/mutator/paths/job_libraries_paths_visitor.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import (
66
"github.com/databricks/cli/libs/structs/structvar"
77
)
88

9-
func jobTaskLibrariesRewritePatterns(base string) []jobRewritePattern {
9+
func jobTaskLibrariesRewritePatterns(base *structpath.PatternNode) []jobRewritePattern {
1010
return []jobRewritePattern{
1111
{
12-
structpath.MustParsePattern(base + ".libraries[*].whl"),
12+
structpath.NewPattern(base, "libraries", structpath.AnyIndex, "whl"),
1313
TranslateModeLocalRelative,
1414
noSkipRewrite,
1515
},
1616
{
17-
structpath.MustParsePattern(base + ".libraries[*].jar"),
17+
structpath.NewPattern(base, "libraries", structpath.AnyIndex, "jar"),
1818
TranslateModeLocalRelative,
1919
noSkipRewrite,
2020
},
@@ -23,7 +23,7 @@ func jobTaskLibrariesRewritePatterns(base string) []jobRewritePattern {
2323

2424
func jobLibrariesRewritePatterns() []jobRewritePattern {
2525
// Base pattern to match all tasks in all jobs.
26-
base := "resources.jobs.*.tasks[*]"
26+
base := jobTasksPattern
2727

2828
// Compile list of patterns and their respective rewrite functions.
2929
jobEnvironmentsPatterns := []jobRewritePattern{
@@ -48,7 +48,7 @@ func jobLibrariesRewritePatterns() []jobRewritePattern {
4848
}
4949

5050
taskPatterns := jobTaskLibrariesRewritePatterns(base)
51-
forEachPatterns := jobTaskLibrariesRewritePatterns(base + ".for_each_task.task")
51+
forEachPatterns := jobTaskLibrariesRewritePatterns(structpath.NewPattern(base, "for_each_task", "task"))
5252
allPatterns := append(taskPatterns, jobEnvironmentsPatterns...)
5353
allPatterns = append(allPatterns, jobEnvironmentsWithRequirementsPatterns...)
5454
allPatterns = append(allPatterns, forEachPatterns...)

‎bundle/config/mutator/paths/job_paths_visitor.go‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,47 +11,50 @@ type jobRewritePattern struct {
1111
skipRewrite func(string) bool
1212
}
1313

14+
// jobTasksPattern matches all tasks in all jobs.
15+
var jobTasksPattern = structpath.NewPattern(nil, "resources", "jobs", structpath.AnyKey, "tasks", structpath.AnyIndex)
16+
1417
func noSkipRewrite(string) bool {
1518
return false
1619
}
1720

18-
func jobTaskRewritePatterns(base string) []jobRewritePattern {
21+
func jobTaskRewritePatterns(base *structpath.PatternNode) []jobRewritePattern {
1922
return []jobRewritePattern{
2023
{
21-
structpath.MustParsePattern(base + ".notebook_task.notebook_path"),
24+
structpath.NewPattern(base, "notebook_task", "notebook_path"),
2225
TranslateModeNotebook,
2326
noSkipRewrite,
2427
},
2528
{
26-
structpath.MustParsePattern(base + ".spark_python_task.python_file"),
29+
structpath.NewPattern(base, "spark_python_task", "python_file"),
2730
TranslateModeFile,
2831
noSkipRewrite,
2932
},
3033
{
31-
structpath.MustParsePattern(base + ".dbt_task.project_directory"),
34+
structpath.NewPattern(base, "dbt_task", "project_directory"),
3235
TranslateModeDirectory,
3336
noSkipRewrite,
3437
},
3538
{
36-
structpath.MustParsePattern(base + ".sql_task.file.path"),
39+
structpath.NewPattern(base, "sql_task", "file", "path"),
3740
TranslateModeFile,
3841
noSkipRewrite,
3942
},
4043
{
41-
structpath.MustParsePattern(base + ".alert_task.workspace_path"),
44+
structpath.NewPattern(base, "alert_task", "workspace_path"),
4245
TranslateModeFile,
4346
noSkipRewrite,
4447
},
4548
{
46-
structpath.MustParsePattern(base + ".libraries[*].requirements"),
49+
structpath.NewPattern(base, "libraries", structpath.AnyIndex, "requirements"),
4750
TranslateModeFile,
4851
noSkipRewrite,
4952
},
5053
{
5154
// The AI Runtime task runs this bash script on each node; the backend
5255
// reads it as a workspace file, so translate the local path to its
5356
// remote (or immutable-snapshot) location like any other file.
54-
structpath.MustParsePattern(base + ".ai_runtime_task.deployments[*].command_path"),
57+
structpath.NewPattern(base, "ai_runtime_task", "deployments", structpath.AnyIndex, "command_path"),
5558
TranslateModeFile,
5659
noSkipRewrite,
5760
},
@@ -60,10 +63,10 @@ func jobTaskRewritePatterns(base string) []jobRewritePattern {
6063

6164
func jobRewritePatterns() []jobRewritePattern {
6265
// Base pattern to match all tasks in all jobs.
63-
base := "resources.jobs.*.tasks[*]"
66+
base := jobTasksPattern
6467

6568
taskPatterns := jobTaskRewritePatterns(base)
66-
forEachPatterns := jobTaskRewritePatterns(base + ".for_each_task.task")
69+
forEachPatterns := jobTaskRewritePatterns(structpath.NewPattern(base, "for_each_task", "task"))
6770
return append(taskPatterns, forEachPatterns...)
6871
}
6972

‎bundle/config/mutator/paths/pipeline_paths_visitor.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ type pipelineRewritePattern struct {
1616
}
1717

1818
// Base pattern to match all libraries in all pipelines.
19-
var base = "resources.pipelines.*"
19+
var base = structpath.NewPattern(nil, "resources", "pipelines", structpath.AnyKey)
2020

2121
func pipelineRewritePatterns() []pipelineRewritePattern {
2222
// Compile list of configuration paths to rewrite.
2323
allPatterns := []pipelineRewritePattern{
2424
{
25-
pattern: structpath.MustParsePattern(base + ".libraries[*].notebook.path"),
25+
pattern: structpath.NewPattern(base, "libraries", structpath.AnyIndex, "notebook", "path"),
2626
mode: TranslateModeNotebook,
2727
skipRewrite: noSkipRewrite,
2828
},
2929
{
30-
pattern: structpath.MustParsePattern(base + ".libraries[*].file.path"),
30+
pattern: structpath.NewPattern(base, "libraries", structpath.AnyIndex, "file", "path"),
3131
mode: TranslateModeFile,
3232
skipRewrite: noSkipRewrite,
3333
},
3434
{
35-
pattern: structpath.MustParsePattern(base + ".libraries[*].glob.include"),
35+
pattern: structpath.NewPattern(base, "libraries", structpath.AnyIndex, "glob", "include"),
3636
mode: TranslateModeGlob,
3737
skipRewrite: noSkipRewrite,
3838
},
3939
{
40-
pattern: structpath.MustParsePattern(base + ".root_path"),
40+
pattern: structpath.NewPattern(base, "root_path"),
4141
mode: TranslateModeDirectory,
4242
skipRewrite: noSkipRewrite,
4343
},

‎bundle/config/mutator/resolve_variable_references.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var defaultPrefixes = []string{
4747
var artifactPath = structpath.MustParsePath("artifacts")
4848

4949
type resolveVariableReferences struct {
50+
// prefixes are top-level config keys.
5051
prefixes []string
5152
pattern *structpath.PatternNode
5253
lookupFn func(structvar.View, *structpath.PathNode, *bundle.Bundle) (structvar.View, error)
@@ -169,7 +170,7 @@ func (m *resolveVariableReferences) Validate(ctx context.Context, b *bundle.Bund
169170
func (m *resolveVariableReferences) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
170171
prefixes := make([]*structpath.PathNode, len(m.prefixes))
171172
for i, prefix := range m.prefixes {
172-
prefixes[i] = structpath.MustParsePath(prefix)
173+
prefixes[i] = structpath.NewPath(nil, prefix)
173174
}
174175

175176
// The path ${var.foo} is a shorthand for ${variables.foo.value}.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (m *bundlePermissions) Apply(ctx context.Context, b *bundle.Bundle) diag.Di
115115
slices.Sort(keys)
116116

117117
for _, key := range keys {
118-
pattern := structpath.MustParsePattern("resources." + key + ".*")
118+
pattern := structpath.NewPattern(nil, "resources", key, structpath.AnyKey)
119119

120120
err = structvar.ForEach(b.Config.View(), pattern, func(p *structpath.PathNode, v structvar.View) error {
121121
had := v.Get("permissions").IsValid()

‎bundle/config/mutator/rewrite_sync_paths.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (m *rewriteSyncPaths) makeRelativeTo(root string, v structvar.View) (string
4848

4949
func (m *rewriteSyncPaths) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
5050
rewrite := func(field string, toSlash bool) error {
51-
pattern := structpath.MustParsePattern("sync." + field + "[*]")
51+
pattern := structpath.NewPattern(nil, "sync", field, structpath.AnyIndex)
5252
return structvar.ForEach(b.Config.View(), pattern, func(p *structpath.PathNode, v structvar.View) error {
5353
path, err := m.makeRelativeTo(b.BundleRootPath, v)
5454
if err != nil {

‎bundle/config/validate/files_to_sync.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,13 @@ func (v *filesToSync) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnost
5656
Summary: "There are no files to sync, please check your .gitignore",
5757
})
5858
} else {
59-
path := "sync.exclude"
6059
diags = diags.Append(diag.Diagnostic{
6160
Severity: diag.Warning,
6261
Summary: "There are no files to sync, please check your .gitignore and sync.exclude configuration",
6362
// Show all locations where sync.exclude is defined, since merging
6463
// sync.exclude is additive.
65-
Locations: b.Config.GetLocations(path),
66-
Paths: structpath.MustParsePaths(path),
64+
Locations: b.Config.GetLocations("sync.exclude"),
65+
Paths: structpath.NewPathSlice("sync", "exclude"),
6766
})
6867
}
6968

‎bundle/config/validate/validate_artifact_path.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ func findVolumeInBundle(r config.Root, catalogName, schemaName, volumeName strin
6868
if v.SchemaName != schemaName && !isSchemaDefinedInBundle {
6969
continue
7070
}
71-
pathString := "resources.volumes." + k
72-
return structpath.MustParsePath(pathString), r.GetLocations(pathString), true
71+
volumePath := structpath.NewPath(nil, "resources", "volumes", k)
72+
return volumePath, r.GetLocations(volumePath.String()), true
7373
}
7474
return nil, nil, false
7575
}

0 commit comments

Comments
 (0)