Skip to content

Commit 2bbfd5c

Browse files
denikIsaac
andcommitted
structvar: map YAML locations in the YAML source; drop mappedSource
Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 8186b25 commit 2bbfd5c

2 files changed

Lines changed: 29 additions & 39 deletions

File tree

‎libs/structs/structvar/decode.go‎

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,9 @@ func DecodeYAMLNode(file string, node *yaml.Node, dst any, mapLocations Location
6969
if err := validate(src); err != nil {
7070
return nil, nil, err
7171
}
72+
src.mapLocations = mapLocations
7273
var d decoder
73-
var s source = src
74-
if mapLocations != nil {
75-
s = mappedSource{source: src, fn: mapLocations}
76-
}
77-
n, ok, err := d.decode(v, s, nil)
74+
n, ok, err := d.decode(v, src, nil)
7875
if err == nil && !ok {
7976
// The value could not be converted to dst at all (e.g. a list for a struct).
8077
err = fmt.Errorf("expected a %s, found a %s", kindOf(v.Type()), src.kind())
@@ -727,33 +724,6 @@ func (s viewSource) elems() ([]source, error) {
727724
return out, nil
728725
}
729726

730-
// mappedSource is a [source] whose locations are mapped by fn.
731-
type mappedSource struct {
732-
source
733-
path *structpath.PathNode
734-
fn LocationMapper
735-
}
736-
737-
func (s mappedSource) locations() []diag.Location {
738-
return s.fn(s.path, s.source.locations())
739-
}
740-
741-
func (s mappedSource) pairs() ([]sourcePair, error) {
742-
pairs, err := s.source.pairs()
743-
for i := range pairs {
744-
pairs[i].value = mappedSource{source: pairs[i].value, path: structpath.NewStringKey(s.path, pairs[i].key), fn: s.fn}
745-
}
746-
return pairs, err
747-
}
748-
749-
func (s mappedSource) elems() ([]source, error) {
750-
elems, err := s.source.elems()
751-
for i := range elems {
752-
elems[i] = mappedSource{source: elems[i], path: structpath.NewIndex(s.path, i), fn: s.fn}
753-
}
754-
return elems, err
755-
}
756-
757727
// kindOf returns the kind of configuration tree values of type t.
758728
func kindOf(t reflect.Type) Kind {
759729
for t.Kind() == reflect.Pointer {

‎libs/structs/structvar/decode_yaml.go‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/databricks/cli/libs/diag"
12+
"github.com/databricks/cli/libs/structs/structpath"
1213
"go.yaml.in/yaml/v3"
1314
)
1415

@@ -35,6 +36,10 @@ type yamlSource struct {
3536
// cyclic anchors.
3637
aliases *aliasChain
3738

39+
// If set, mapLocations is applied to the locations of the value at path.
40+
mapLocations LocationMapper
41+
path *structpath.PathNode
42+
3843
// Resolved scalar.
3944
k Kind
4045
v any
@@ -198,7 +203,11 @@ func (s *yamlSource) kind() Kind {
198203

199204
func (s *yamlSource) locations() []diag.Location {
200205
// An alias has the location of the anchored value.
201-
return []diag.Location{s.loc(s.target)}
206+
locs := []diag.Location{s.loc(s.target)}
207+
if s.mapLocations != nil {
208+
return s.mapLocations(s.path, locs)
209+
}
210+
return locs
202211
}
203212

204213
func (s *yamlSource) anchor() bool {
@@ -209,8 +218,18 @@ func (s *yamlSource) scalar() any {
209218
return s.v
210219
}
211220

212-
func (s *yamlSource) child(n *yaml.Node) (source, error) {
213-
return newYAMLSource(s.file, n, s.aliases)
221+
// child returns the source of n, the value at path below this one (computed by path,
222+
// only when locations are mapped).
223+
func (s *yamlSource) child(n *yaml.Node, path func() *structpath.PathNode) (*yamlSource, error) {
224+
c, err := newYAMLSource(s.file, n, s.aliases)
225+
if err != nil {
226+
return nil, err
227+
}
228+
if s.mapLocations != nil {
229+
c.mapLocations = s.mapLocations
230+
c.path = path()
231+
}
232+
return c, nil
214233
}
215234

216235
func (s *yamlSource) pairs() ([]sourcePair, error) {
@@ -245,7 +264,7 @@ func (s *yamlSource) pairs() ([]sourcePair, error) {
245264
return nil, yamlErrorf(loc, "invalid key tag: %v", st)
246265
}
247266

248-
v, err := s.child(val)
267+
v, err := s.child(val, func() *structpath.PathNode { return structpath.NewStringKey(s.path, key.Value) })
249268
if err != nil {
250269
return nil, err
251270
}
@@ -272,7 +291,8 @@ func (s *yamlSource) pairs() ([]sourcePair, error) {
272291
// Merged maps come first; the entries of this mapping take precedence.
273292
var out pairSet
274293
for _, n := range mnodes {
275-
ms, err := s.child(n)
294+
// The merged mapping's values are at the paths of this mapping's.
295+
ms, err := s.child(n, func() *structpath.PathNode { return s.path })
276296
if err != nil {
277297
return nil, err
278298
}
@@ -316,8 +336,8 @@ func (s *pairSet) set(p sourcePair) {
316336

317337
func (s *yamlSource) elems() ([]source, error) {
318338
var out []source
319-
for _, n := range s.target.Content {
320-
v, err := s.child(n)
339+
for i, n := range s.target.Content {
340+
v, err := s.child(n, func() *structpath.PathNode { return structpath.NewIndex(s.path, i) })
321341
if err != nil {
322342
return nil, err
323343
}

0 commit comments

Comments
 (0)