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
199204func (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
204213func (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
216235func (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
317337func (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