databricks / cli

Databricks CLI
Other
132 stars 50 forks source link

Retain location metadata for values in `convert.FromTyped` #1523

Closed shreyas-goenka closed 3 months ago

shreyas-goenka commented 3 months ago

Changes

There are four different treatments location metadata can receive in the convert.FromTyped method.

  1. Location metadata is retained for maps, structs and slices if the value is not nil
  2. Location metadata is lost for maps, structs and slices if the value is is nil
  3. Location metadata is retained if a scalar type (eg. bool, string etc) does not change.
  4. Location metadata is lost if the value for a scalar type changes.

This PR ensures that location metadata is not lost in any case; that is, it's always preserved.

For (2), this serves as a bug fix so that location information is not lost on conversion to and from typed for nil values of complex types (struct, slices, and maps).

For (4) this is a change in semantics. For primitive values modified in a typed mutator, any references to .Location() for computed primitive fields will now return associated YAML location metadata (if any) instead of an empty location.

While arguable, these semantics are OK since:

  1. Situations like these will be rare.
  2. Knowing the YAML location (if any) is better than not knowing the location at all. These locations are typically visible to the user in errors and warnings.

Tests

Unit tests