2023-10-20 12:56:59 +00:00
|
|
|
package yamlloader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-10-20 12:56:59 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type loader struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func errorf(loc dyn.Location, format string, args ...interface{}) error {
|
2023-10-20 12:56:59 +00:00
|
|
|
return fmt.Errorf("yaml (%s): %s", loc, fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLoader(path string) *loader {
|
|
|
|
return &loader{
|
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func (d *loader) location(node *yaml.Node) dyn.Location {
|
|
|
|
return dyn.Location{
|
2023-10-20 12:56:59 +00:00
|
|
|
File: d.path,
|
|
|
|
Line: node.Line,
|
|
|
|
Column: node.Column,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func (d *loader) load(node *yaml.Node) (dyn.Value, error) {
|
|
|
|
loc := dyn.Location{
|
2023-10-20 12:56:59 +00:00
|
|
|
File: d.path,
|
|
|
|
Line: node.Line,
|
|
|
|
Column: node.Column,
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
var value dyn.Value
|
2023-10-20 12:56:59 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
switch node.Kind {
|
|
|
|
case yaml.DocumentNode:
|
|
|
|
value, err = d.loadDocument(node, loc)
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
value, err = d.loadSequence(node, loc)
|
|
|
|
case yaml.MappingNode:
|
|
|
|
value, err = d.loadMapping(node, loc)
|
|
|
|
case yaml.ScalarNode:
|
|
|
|
value, err = d.loadScalar(node, loc)
|
|
|
|
case yaml.AliasNode:
|
|
|
|
value, err = d.loadAlias(node, loc)
|
|
|
|
default:
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, errorf(loc, "unknown node kind: %v", node.Kind)
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark value as anchor if needed.
|
|
|
|
// If this node doesn't map to a type, we don't need to warn about it.
|
|
|
|
if node.Anchor != "" {
|
|
|
|
value = value.MarkAnchor()
|
|
|
|
}
|
|
|
|
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func (d *loader) loadDocument(node *yaml.Node, loc dyn.Location) (dyn.Value, error) {
|
2023-10-20 12:56:59 +00:00
|
|
|
return d.load(node.Content[0])
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func (d *loader) loadSequence(node *yaml.Node, loc dyn.Location) (dyn.Value, error) {
|
|
|
|
acc := make([]dyn.Value, len(node.Content))
|
2023-10-20 12:56:59 +00:00
|
|
|
for i, n := range node.Content {
|
|
|
|
v, err := d.load(n)
|
|
|
|
if err != nil {
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, err
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
acc[i] = v
|
|
|
|
}
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(acc, []dyn.Location{loc}), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func (d *loader) loadMapping(node *yaml.Node, loc dyn.Location) (dyn.Value, error) {
|
2023-10-20 12:56:59 +00:00
|
|
|
var merge *yaml.Node
|
|
|
|
|
2024-03-25 11:01:09 +00:00
|
|
|
acc := dyn.NewMapping()
|
2023-10-20 12:56:59 +00:00
|
|
|
for i := 0; i < len(node.Content); i += 2 {
|
|
|
|
key := node.Content[i]
|
|
|
|
val := node.Content[i+1]
|
|
|
|
|
|
|
|
// Assert that keys are strings
|
|
|
|
if key.Kind != yaml.ScalarNode {
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, errorf(loc, "key is not a scalar")
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st := key.ShortTag()
|
|
|
|
switch st {
|
|
|
|
case "!!str":
|
|
|
|
// OK
|
2024-10-17 13:13:30 +00:00
|
|
|
case "!!null":
|
|
|
|
// A literal unquoted "null" is treated as a null value by the YAML parser.
|
|
|
|
// However, when used as a key, it is treated as the string "null".
|
2023-10-20 12:56:59 +00:00
|
|
|
case "!!merge":
|
|
|
|
if merge != nil {
|
|
|
|
panic("merge node already set")
|
|
|
|
}
|
|
|
|
merge = val
|
|
|
|
continue
|
|
|
|
default:
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, errorf(loc, "invalid key tag: %v", st)
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 13:13:30 +00:00
|
|
|
k := dyn.NewValue(key.Value, []dyn.Location{{
|
|
|
|
File: d.path,
|
|
|
|
Line: key.Line,
|
|
|
|
Column: key.Column,
|
|
|
|
}})
|
2024-03-25 11:01:09 +00:00
|
|
|
|
2023-10-20 12:56:59 +00:00
|
|
|
v, err := d.load(val)
|
|
|
|
if err != nil {
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, err
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
2024-03-25 11:01:09 +00:00
|
|
|
acc.Set(k, v)
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if merge == nil {
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(acc, []dyn.Location{loc}), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build location for the merge node.
|
|
|
|
var mloc = d.location(merge)
|
|
|
|
var merr = errorf(mloc, "map merge requires map or sequence of maps as the value")
|
|
|
|
|
|
|
|
// Flatten the merge node into a slice of nodes.
|
|
|
|
// It can be either a single node or a sequence of nodes.
|
|
|
|
var mnodes []*yaml.Node
|
|
|
|
switch merge.Kind {
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
mnodes = merge.Content
|
|
|
|
case yaml.AliasNode:
|
|
|
|
mnodes = []*yaml.Node{merge}
|
|
|
|
default:
|
2023-12-22 13:20:45 +00:00
|
|
|
return dyn.NilValue, merr
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build a sequence of values to merge.
|
|
|
|
// The entries that we already accumulated have precedence.
|
2024-03-25 11:01:09 +00:00
|
|
|
var seq []dyn.Mapping
|
2023-10-20 12:56:59 +00:00
|
|
|
for _, n := range mnodes {
|
|
|
|
v, err := d.load(n)
|
|
|
|
if err != nil {
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, err
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
m, ok := v.AsMap()
|
|
|
|
if !ok {
|
2023-12-22 13:20:45 +00:00
|
|
|
return dyn.NilValue, merr
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
seq = append(seq, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the accumulated entries to the sequence.
|
|
|
|
seq = append(seq, acc)
|
2024-03-25 11:01:09 +00:00
|
|
|
out := dyn.NewMapping()
|
2023-10-20 12:56:59 +00:00
|
|
|
for _, m := range seq {
|
2024-03-25 11:01:09 +00:00
|
|
|
out.Merge(m)
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(out, []dyn.Location{loc}), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
|
2024-10-17 13:13:30 +00:00
|
|
|
func newIntValue(i64 int64, loc dyn.Location) dyn.Value {
|
|
|
|
// Use regular int type instead of int64 if possible.
|
|
|
|
if i64 >= math.MinInt32 && i64 <= math.MaxInt32 {
|
|
|
|
return dyn.NewValue(int(i64), []dyn.Location{loc})
|
|
|
|
}
|
|
|
|
return dyn.NewValue(i64, []dyn.Location{loc})
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func (d *loader) loadScalar(node *yaml.Node, loc dyn.Location) (dyn.Value, error) {
|
2023-10-20 12:56:59 +00:00
|
|
|
st := node.ShortTag()
|
|
|
|
switch st {
|
|
|
|
case "!!str":
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(node.Value, []dyn.Location{loc}), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
case "!!bool":
|
|
|
|
switch strings.ToLower(node.Value) {
|
|
|
|
case "true":
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(true, []dyn.Location{loc}), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
case "false":
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(false, []dyn.Location{loc}), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
default:
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, errorf(loc, "invalid bool value: %v", node.Value)
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
case "!!int":
|
2024-10-17 13:13:30 +00:00
|
|
|
// Try to parse the an integer value in base 10.
|
|
|
|
// We trim leading zeros to avoid octal parsing of the "0" prefix.
|
|
|
|
// See "testdata/spec_example_2.19.yml" for background.
|
|
|
|
i64, err := strconv.ParseInt(strings.TrimLeft(node.Value, "0"), 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
return newIntValue(i64, loc), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
2024-10-17 13:13:30 +00:00
|
|
|
// Let the [ParseInt] function figure out the base.
|
|
|
|
i64, err = strconv.ParseInt(node.Value, 0, 64)
|
|
|
|
if err == nil {
|
|
|
|
return newIntValue(i64, loc), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
2024-10-17 13:13:30 +00:00
|
|
|
return dyn.InvalidValue, errorf(loc, "invalid int value: %v", node.Value)
|
2023-10-20 12:56:59 +00:00
|
|
|
case "!!float":
|
|
|
|
f64, err := strconv.ParseFloat(node.Value, 64)
|
|
|
|
if err != nil {
|
2024-10-17 13:13:30 +00:00
|
|
|
// Deal with infinity prefixes.
|
|
|
|
v := strings.ToLower(node.Value)
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(v, "+"):
|
|
|
|
v = strings.TrimPrefix(v, "+")
|
|
|
|
f64 = math.Inf(1)
|
|
|
|
case strings.HasPrefix(v, "-"):
|
|
|
|
v = strings.TrimPrefix(v, "-")
|
|
|
|
f64 = math.Inf(-1)
|
|
|
|
default:
|
|
|
|
// No prefix.
|
|
|
|
f64 = math.Inf(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with infinity and NaN values.
|
|
|
|
switch v {
|
|
|
|
case ".inf":
|
|
|
|
return dyn.NewValue(f64, []dyn.Location{loc}), nil
|
|
|
|
case ".nan":
|
|
|
|
return dyn.NewValue(math.NaN(), []dyn.Location{loc}), nil
|
|
|
|
}
|
|
|
|
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, errorf(loc, "invalid float value: %v", node.Value)
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(f64, []dyn.Location{loc}), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
case "!!null":
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(nil, []dyn.Location{loc}), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
case "!!timestamp":
|
2024-08-29 13:02:34 +00:00
|
|
|
t, err := dyn.NewTime(node.Value)
|
|
|
|
if err == nil {
|
|
|
|
return dyn.NewValue(t, []dyn.Location{loc}), nil
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, errorf(loc, "invalid timestamp value: %v", node.Value)
|
2023-10-20 12:56:59 +00:00
|
|
|
default:
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, errorf(loc, "unknown tag: %v", st)
|
2023-10-20 12:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
func (d *loader) loadAlias(node *yaml.Node, loc dyn.Location) (dyn.Value, error) {
|
2023-10-20 12:56:59 +00:00
|
|
|
return d.load(node.Alias)
|
|
|
|
}
|