Skip to content

Commit

Permalink
Do not emit warning on YAML anchor blocks (#1354)
Browse files Browse the repository at this point in the history
## Changes
In 0.217.0 we started to emit warning on unknown fields in YAML
configuration but wrongly considered YAML anchor blocks as unknown
field.

This PR fixes this by skipping normalising of YAML blocks.

## Tests
Added regression tests
  • Loading branch information
andrewnester authored Apr 10, 2024
1 parent d064202 commit d914a1b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
15 changes: 12 additions & 3 deletions bundle/tests/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config/mutator"
"github.com/databricks/cli/bundle/phases"
"github.com/databricks/cli/libs/diag"
"github.com/stretchr/testify/require"
)

Expand All @@ -20,16 +21,24 @@ func load(t *testing.T, path string) *bundle.Bundle {
}

func loadTarget(t *testing.T, path, env string) *bundle.Bundle {
b, diags := loadTargetWithDiags(path, env)
require.NoError(t, diags.Error())
return b
}

func loadTargetWithDiags(path, env string) (*bundle.Bundle, diag.Diagnostics) {
ctx := context.Background()
b, err := bundle.Load(ctx, path)
require.NoError(t, err)
if err != nil {
return nil, diag.FromErr(err)
}

diags := bundle.Apply(ctx, b, bundle.Seq(
phases.LoadNamedTarget(env),
mutator.RewriteSyncPaths(),
mutator.MergeJobClusters(),
mutator.MergeJobTasks(),
mutator.MergePipelineClusters(),
))
require.NoError(t, diags.Error())
return b
return b, diags
}
15 changes: 15 additions & 0 deletions bundle/tests/yaml_anchors_separate_block/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bundle:
name: yaml_anchors_separate_block

tags: &custom_tags
Tag1: "Value1"
Tag2: "Value2"
Tag3: "Value3"

resources:
jobs:
my_job:
tasks:
- task_key: yaml_anchors_separate_block
tags:
<<: *custom_tags
12 changes: 12 additions & 0 deletions bundle/tests/yaml_anchors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ func TestYAMLAnchors(t *testing.T) {
require.NotNil(t, t0)
require.NotNil(t, t1)

require.NotNil(t, t0.NewCluster)
require.NotNil(t, t1.NewCluster)
assert.Equal(t, "10.4.x-scala2.12", t0.NewCluster.SparkVersion)
assert.Equal(t, "10.4.x-scala2.12", t1.NewCluster.SparkVersion)
}

func TestYAMLAnchorsNoWarnings(t *testing.T) {
_, diags := loadTargetWithDiags("./yaml_anchors", "default")
assert.Empty(t, diags)
}

func TestYAMLAnchorsSeparateBlockNoWarnings(t *testing.T) {
_, diags := loadTargetWithDiags("./yaml_anchors_separate_block", "default")
assert.Empty(t, diags)
}
15 changes: 9 additions & 6 deletions libs/dyn/convert/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,17 @@ func (n normalizeOptions) normalizeStruct(typ reflect.Type, src dyn.Value, seen
for _, pair := range src.MustMap().Pairs() {
pk := pair.Key
pv := pair.Value

index, ok := info.Fields[pk.MustString()]
if !ok {
diags = diags.Append(diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("unknown field: %s", pk.MustString()),
Location: pk.Location(),
Path: path,
})
if !pv.IsAnchor() {
diags = diags.Append(diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("unknown field: %s", pk.MustString()),
Location: pk.Location(),
Path: path,
})
}
continue
}

Expand Down
20 changes: 20 additions & 0 deletions libs/dyn/convert/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,23 @@ func TestNormalizeFloatError(t *testing.T) {
Path: dyn.EmptyPath,
}, err[0])
}

func TestNormalizeAnchors(t *testing.T) {
type Tmp struct {
Foo string `json:"foo"`
}

var typ Tmp
vin := dyn.V(map[string]dyn.Value{
"foo": dyn.V("bar"),
"anchor": dyn.V("anchor").MarkAnchor(),
})

vout, err := Normalize(typ, vin)
assert.Len(t, err, 0)

// The field that can be mapped to the struct field is retained.
assert.Equal(t, map[string]any{
"foo": "bar",
}, vout.AsAny())
}

0 comments on commit d914a1b

Please sign in to comment.