2023-10-25 11:56:42 +00:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2024-02-16 12:56:02 +00:00
|
|
|
"slices"
|
2023-10-25 11:56:42 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-12-22 13:20:45 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2024-02-19 10:44:51 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn/dynvar"
|
2023-10-25 11:56:42 +00:00
|
|
|
)
|
|
|
|
|
2024-02-15 15:16:40 +00:00
|
|
|
// NormalizeOption is the type for options that can be passed to Normalize.
|
|
|
|
type NormalizeOption int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// IncludeMissingFields causes the normalization to include fields that defined on the given
|
|
|
|
// type but are missing in the source value. They are included with their zero values.
|
|
|
|
IncludeMissingFields NormalizeOption = iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type normalizeOptions struct {
|
|
|
|
includeMissingFields bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func Normalize(dst any, src dyn.Value, opts ...NormalizeOption) (dyn.Value, diag.Diagnostics) {
|
|
|
|
var n normalizeOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
switch opt {
|
|
|
|
case IncludeMissingFields:
|
|
|
|
n.includeMissingFields = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
return n.normalizeType(reflect.TypeOf(dst), src, []reflect.Type{}, dyn.EmptyPath)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func (n normalizeOptions) normalizeType(typ reflect.Type, src dyn.Value, seen []reflect.Type, path dyn.Path) (dyn.Value, diag.Diagnostics) {
|
2023-10-25 11:56:42 +00:00
|
|
|
for typ.Kind() == reflect.Pointer {
|
|
|
|
typ = typ.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch typ.Kind() {
|
|
|
|
case reflect.Struct:
|
2024-04-03 08:56:46 +00:00
|
|
|
return n.normalizeStruct(typ, src, append(seen, typ), path)
|
2023-10-25 11:56:42 +00:00
|
|
|
case reflect.Map:
|
2024-04-03 08:56:46 +00:00
|
|
|
return n.normalizeMap(typ, src, append(seen, typ), path)
|
2023-10-25 11:56:42 +00:00
|
|
|
case reflect.Slice:
|
2024-04-03 08:56:46 +00:00
|
|
|
return n.normalizeSlice(typ, src, append(seen, typ), path)
|
2023-10-25 11:56:42 +00:00
|
|
|
case reflect.String:
|
2024-04-03 08:56:46 +00:00
|
|
|
return n.normalizeString(typ, src, path)
|
2023-10-25 11:56:42 +00:00
|
|
|
case reflect.Bool:
|
2024-04-03 08:56:46 +00:00
|
|
|
return n.normalizeBool(typ, src, path)
|
2023-10-25 11:56:42 +00:00
|
|
|
case reflect.Int, reflect.Int32, reflect.Int64:
|
2024-04-03 08:56:46 +00:00
|
|
|
return n.normalizeInt(typ, src, path)
|
2023-10-25 11:56:42 +00:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2024-04-03 08:56:46 +00:00
|
|
|
return n.normalizeFloat(typ, src, path)
|
2024-06-26 10:25:32 +00:00
|
|
|
case reflect.Interface:
|
|
|
|
return n.normalizeInterface(typ, src, path)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 14:12:19 +00:00
|
|
|
return dyn.InvalidValue, diag.Errorf("unsupported type: %s", typ.Kind())
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func nullWarning(expected dyn.Kind, src dyn.Value, path dyn.Path) diag.Diagnostic {
|
2024-04-02 12:17:29 +00:00
|
|
|
return diag.Diagnostic{
|
2024-07-23 17:20:11 +00:00
|
|
|
Severity: diag.Warning,
|
|
|
|
Summary: fmt.Sprintf("expected a %s value, found null", expected),
|
|
|
|
Locations: []dyn.Location{src.Location()},
|
2024-07-25 15:16:27 +00:00
|
|
|
Paths: []dyn.Path{path},
|
2024-04-02 12:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func typeMismatch(expected dyn.Kind, src dyn.Value, path dyn.Path) diag.Diagnostic {
|
2023-10-25 11:56:42 +00:00
|
|
|
return diag.Diagnostic{
|
2024-07-23 17:20:11 +00:00
|
|
|
Severity: diag.Warning,
|
|
|
|
Summary: fmt.Sprintf("expected %s, found %s", expected, src.Kind()),
|
|
|
|
Locations: []dyn.Location{src.Location()},
|
2024-07-25 15:16:27 +00:00
|
|
|
Paths: []dyn.Path{path},
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func (n normalizeOptions) normalizeStruct(typ reflect.Type, src dyn.Value, seen []reflect.Type, path dyn.Path) (dyn.Value, diag.Diagnostics) {
|
2023-10-25 11:56:42 +00:00
|
|
|
var diags diag.Diagnostics
|
|
|
|
|
|
|
|
switch src.Kind() {
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindMap:
|
2024-03-25 11:01:09 +00:00
|
|
|
out := dyn.NewMapping()
|
2023-10-25 11:56:42 +00:00
|
|
|
info := getStructInfo(typ)
|
2024-03-25 11:01:09 +00:00
|
|
|
for _, pair := range src.MustMap().Pairs() {
|
|
|
|
pk := pair.Key
|
|
|
|
pv := pair.Value
|
2024-04-10 09:55:02 +00:00
|
|
|
|
2024-03-25 11:01:09 +00:00
|
|
|
index, ok := info.Fields[pk.MustString()]
|
2023-10-25 11:56:42 +00:00
|
|
|
if !ok {
|
2024-04-10 09:55:02 +00:00
|
|
|
if !pv.IsAnchor() {
|
|
|
|
diags = diags.Append(diag.Diagnostic{
|
|
|
|
Severity: diag.Warning,
|
|
|
|
Summary: fmt.Sprintf("unknown field: %s", pk.MustString()),
|
2024-07-23 17:20:11 +00:00
|
|
|
// Show all locations the unknown field is defined at.
|
|
|
|
Locations: pk.Locations(),
|
2024-07-25 15:16:27 +00:00
|
|
|
Paths: []dyn.Path{path},
|
2024-04-10 09:55:02 +00:00
|
|
|
})
|
|
|
|
}
|
2023-10-25 11:56:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize the value according to the field type.
|
2024-04-03 08:56:46 +00:00
|
|
|
nv, err := n.normalizeType(typ.FieldByIndex(index).Type, pv, seen, path.Append(dyn.Key(pk.MustString())))
|
2023-10-25 11:56:42 +00:00
|
|
|
if err != nil {
|
|
|
|
diags = diags.Extend(err)
|
|
|
|
// Skip the element if it cannot be normalized.
|
2024-03-25 11:01:09 +00:00
|
|
|
if !nv.IsValid() {
|
2023-10-25 11:56:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 11:01:09 +00:00
|
|
|
out.Set(pk, nv)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 15:16:40 +00:00
|
|
|
// Return the normalized value if missing fields are not included.
|
|
|
|
if !n.includeMissingFields {
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(out, src.Locations()), diags
|
2024-02-15 15:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate missing fields with their zero values.
|
|
|
|
for k, index := range info.Fields {
|
2024-03-25 11:01:09 +00:00
|
|
|
if _, ok := out.GetByString(k); ok {
|
2024-02-15 15:16:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally dereference pointers to get the underlying field type.
|
|
|
|
ftyp := typ.FieldByIndex(index).Type
|
|
|
|
for ftyp.Kind() == reflect.Pointer {
|
|
|
|
ftyp = ftyp.Elem()
|
|
|
|
}
|
|
|
|
|
2024-02-16 12:56:02 +00:00
|
|
|
// Skip field if we have already seen its type to avoid infinite recursion
|
|
|
|
// when filling in the zero value of a recursive type.
|
|
|
|
if slices.Contains(seen, ftyp) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-15 15:16:40 +00:00
|
|
|
var v dyn.Value
|
|
|
|
switch ftyp.Kind() {
|
|
|
|
case reflect.Struct, reflect.Map:
|
2024-04-03 08:56:46 +00:00
|
|
|
v, _ = n.normalizeType(ftyp, dyn.V(map[string]dyn.Value{}), seen, path.Append(dyn.Key(k)))
|
2024-02-15 15:16:40 +00:00
|
|
|
case reflect.Slice:
|
2024-04-03 08:56:46 +00:00
|
|
|
v, _ = n.normalizeType(ftyp, dyn.V([]dyn.Value{}), seen, path.Append(dyn.Key(k)))
|
2024-02-15 15:16:40 +00:00
|
|
|
case reflect.String:
|
2024-04-03 08:56:46 +00:00
|
|
|
v, _ = n.normalizeType(ftyp, dyn.V(""), seen, path.Append(dyn.Key(k)))
|
2024-02-15 15:16:40 +00:00
|
|
|
case reflect.Bool:
|
2024-04-03 08:56:46 +00:00
|
|
|
v, _ = n.normalizeType(ftyp, dyn.V(false), seen, path.Append(dyn.Key(k)))
|
2024-02-15 15:16:40 +00:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
2024-04-03 08:56:46 +00:00
|
|
|
v, _ = n.normalizeType(ftyp, dyn.V(int64(0)), seen, path.Append(dyn.Key(k)))
|
2024-02-15 15:16:40 +00:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2024-04-03 08:56:46 +00:00
|
|
|
v, _ = n.normalizeType(ftyp, dyn.V(float64(0)), seen, path.Append(dyn.Key(k)))
|
2024-02-15 15:16:40 +00:00
|
|
|
default:
|
|
|
|
// Skip fields for which we do not have a natural [dyn.Value] equivalent.
|
|
|
|
// For example, we don't handle reflect.Complex* and reflect.Uint* types.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if v.IsValid() {
|
2024-03-25 11:01:09 +00:00
|
|
|
out.Set(dyn.V(k), v)
|
2024-02-15 15:16:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(out, src.Locations()), diags
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindNil:
|
2023-10-25 11:56:42 +00:00
|
|
|
return src, diags
|
2024-06-26 10:25:32 +00:00
|
|
|
|
|
|
|
case dyn.KindString:
|
|
|
|
// Return verbatim if it's a pure variable reference.
|
|
|
|
if dynvar.IsPureVariableReference(src.MustString()) {
|
|
|
|
return src, nil
|
|
|
|
}
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 10:25:32 +00:00
|
|
|
// Cannot interpret as a struct.
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindMap, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func (n normalizeOptions) normalizeMap(typ reflect.Type, src dyn.Value, seen []reflect.Type, path dyn.Path) (dyn.Value, diag.Diagnostics) {
|
2023-10-25 11:56:42 +00:00
|
|
|
var diags diag.Diagnostics
|
|
|
|
|
|
|
|
switch src.Kind() {
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindMap:
|
2024-03-25 11:01:09 +00:00
|
|
|
out := dyn.NewMapping()
|
|
|
|
for _, pair := range src.MustMap().Pairs() {
|
|
|
|
pk := pair.Key
|
|
|
|
pv := pair.Value
|
|
|
|
|
2023-10-25 11:56:42 +00:00
|
|
|
// Normalize the value according to the map element type.
|
2024-04-03 08:56:46 +00:00
|
|
|
nv, err := n.normalizeType(typ.Elem(), pv, seen, path.Append(dyn.Key(pk.MustString())))
|
2023-10-25 11:56:42 +00:00
|
|
|
if err != nil {
|
|
|
|
diags = diags.Extend(err)
|
|
|
|
// Skip the element if it cannot be normalized.
|
2024-03-25 11:01:09 +00:00
|
|
|
if !nv.IsValid() {
|
2023-10-25 11:56:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 11:01:09 +00:00
|
|
|
out.Set(pk, nv)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(out, src.Locations()), diags
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindNil:
|
2023-10-25 11:56:42 +00:00
|
|
|
return src, diags
|
2024-06-26 10:25:32 +00:00
|
|
|
|
|
|
|
case dyn.KindString:
|
|
|
|
// Return verbatim if it's a pure variable reference.
|
|
|
|
if dynvar.IsPureVariableReference(src.MustString()) {
|
|
|
|
return src, nil
|
|
|
|
}
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 10:25:32 +00:00
|
|
|
// Cannot interpret as a map.
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindMap, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func (n normalizeOptions) normalizeSlice(typ reflect.Type, src dyn.Value, seen []reflect.Type, path dyn.Path) (dyn.Value, diag.Diagnostics) {
|
2023-10-25 11:56:42 +00:00
|
|
|
var diags diag.Diagnostics
|
|
|
|
|
|
|
|
switch src.Kind() {
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindSequence:
|
|
|
|
out := make([]dyn.Value, 0, len(src.MustSequence()))
|
2023-10-25 11:56:42 +00:00
|
|
|
for _, v := range src.MustSequence() {
|
|
|
|
// Normalize the value according to the slice element type.
|
2024-04-03 08:56:46 +00:00
|
|
|
v, err := n.normalizeType(typ.Elem(), v, seen, path.Append(dyn.Index(len(out))))
|
2023-10-25 11:56:42 +00:00
|
|
|
if err != nil {
|
|
|
|
diags = diags.Extend(err)
|
|
|
|
// Skip the element if it cannot be normalized.
|
2024-02-13 14:12:19 +00:00
|
|
|
if !v.IsValid() {
|
2023-10-25 11:56:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, v)
|
|
|
|
}
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(out, src.Locations()), diags
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindNil:
|
2023-10-25 11:56:42 +00:00
|
|
|
return src, diags
|
2024-06-26 10:25:32 +00:00
|
|
|
|
|
|
|
case dyn.KindString:
|
|
|
|
// Return verbatim if it's a pure variable reference.
|
|
|
|
if dynvar.IsPureVariableReference(src.MustString()) {
|
|
|
|
return src, nil
|
|
|
|
}
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 10:25:32 +00:00
|
|
|
// Cannot interpret as a slice.
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindSequence, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func (n normalizeOptions) normalizeString(typ reflect.Type, src dyn.Value, path dyn.Path) (dyn.Value, diag.Diagnostics) {
|
2023-10-25 11:56:42 +00:00
|
|
|
var diags diag.Diagnostics
|
|
|
|
var out string
|
|
|
|
|
|
|
|
switch src.Kind() {
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindString:
|
2023-10-25 11:56:42 +00:00
|
|
|
out = src.MustString()
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindBool:
|
2023-10-25 11:56:42 +00:00
|
|
|
out = strconv.FormatBool(src.MustBool())
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindInt:
|
2023-10-25 11:56:42 +00:00
|
|
|
out = strconv.FormatInt(src.MustInt(), 10)
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindFloat:
|
2023-10-25 11:56:42 +00:00
|
|
|
out = strconv.FormatFloat(src.MustFloat(), 'f', -1, 64)
|
2024-04-02 12:17:29 +00:00
|
|
|
case dyn.KindNil:
|
|
|
|
// Return a warning if the field is present but has a null value.
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(nullWarning(dyn.KindString, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
default:
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindString, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(out, src.Locations()), diags
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func (n normalizeOptions) normalizeBool(typ reflect.Type, src dyn.Value, path dyn.Path) (dyn.Value, diag.Diagnostics) {
|
2023-10-25 11:56:42 +00:00
|
|
|
var diags diag.Diagnostics
|
|
|
|
var out bool
|
|
|
|
|
|
|
|
switch src.Kind() {
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindBool:
|
2023-10-25 11:56:42 +00:00
|
|
|
out = src.MustBool()
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindString:
|
2023-10-25 11:56:42 +00:00
|
|
|
// See https://github.com/go-yaml/yaml/blob/f6f7691b1fdeb513f56608cd2c32c51f8194bf51/decode.go#L684-L693.
|
|
|
|
switch src.MustString() {
|
|
|
|
case "true", "y", "Y", "yes", "Yes", "YES", "on", "On", "ON":
|
|
|
|
out = true
|
|
|
|
case "false", "n", "N", "no", "No", "NO", "off", "Off", "OFF":
|
|
|
|
out = false
|
|
|
|
default:
|
2024-02-19 10:44:51 +00:00
|
|
|
// Return verbatim if it's a pure variable reference.
|
|
|
|
if dynvar.IsPureVariableReference(src.MustString()) {
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
2023-10-25 11:56:42 +00:00
|
|
|
// Cannot interpret as a boolean.
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindBool, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
2024-04-02 12:17:29 +00:00
|
|
|
case dyn.KindNil:
|
|
|
|
// Return a warning if the field is present but has a null value.
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(nullWarning(dyn.KindBool, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
default:
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindBool, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(out, src.Locations()), diags
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func (n normalizeOptions) normalizeInt(typ reflect.Type, src dyn.Value, path dyn.Path) (dyn.Value, diag.Diagnostics) {
|
2023-10-25 11:56:42 +00:00
|
|
|
var diags diag.Diagnostics
|
|
|
|
var out int64
|
|
|
|
|
|
|
|
switch src.Kind() {
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindInt:
|
2023-10-25 11:56:42 +00:00
|
|
|
out = src.MustInt()
|
2024-04-17 08:58:07 +00:00
|
|
|
case dyn.KindFloat:
|
|
|
|
out = int64(src.MustFloat())
|
|
|
|
if src.MustFloat() != float64(out) {
|
|
|
|
return dyn.InvalidValue, diags.Append(diag.Diagnostic{
|
2024-07-23 17:20:11 +00:00
|
|
|
Severity: diag.Warning,
|
|
|
|
Summary: fmt.Sprintf(`cannot accurately represent "%g" as integer due to precision loss`, src.MustFloat()),
|
|
|
|
Locations: []dyn.Location{src.Location()},
|
2024-07-25 15:16:27 +00:00
|
|
|
Paths: []dyn.Path{path},
|
2024-04-17 08:58:07 +00:00
|
|
|
})
|
|
|
|
}
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindString:
|
2023-10-25 11:56:42 +00:00
|
|
|
var err error
|
|
|
|
out, err = strconv.ParseInt(src.MustString(), 10, 64)
|
|
|
|
if err != nil {
|
2024-02-19 10:44:51 +00:00
|
|
|
// Return verbatim if it's a pure variable reference.
|
|
|
|
if dynvar.IsPureVariableReference(src.MustString()) {
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 14:12:19 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(diag.Diagnostic{
|
2024-07-23 17:20:11 +00:00
|
|
|
Severity: diag.Warning,
|
|
|
|
Summary: fmt.Sprintf("cannot parse %q as an integer", src.MustString()),
|
|
|
|
Locations: []dyn.Location{src.Location()},
|
2024-07-25 15:16:27 +00:00
|
|
|
Paths: []dyn.Path{path},
|
2023-10-25 11:56:42 +00:00
|
|
|
})
|
|
|
|
}
|
2024-04-02 12:17:29 +00:00
|
|
|
case dyn.KindNil:
|
|
|
|
// Return a warning if the field is present but has a null value.
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(nullWarning(dyn.KindInt, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
default:
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindInt, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(out, src.Locations()), diags
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 08:56:46 +00:00
|
|
|
func (n normalizeOptions) normalizeFloat(typ reflect.Type, src dyn.Value, path dyn.Path) (dyn.Value, diag.Diagnostics) {
|
2023-10-25 11:56:42 +00:00
|
|
|
var diags diag.Diagnostics
|
|
|
|
var out float64
|
|
|
|
|
|
|
|
switch src.Kind() {
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindFloat:
|
2023-10-25 11:56:42 +00:00
|
|
|
out = src.MustFloat()
|
2024-04-17 08:58:07 +00:00
|
|
|
case dyn.KindInt:
|
|
|
|
out = float64(src.MustInt())
|
|
|
|
if src.MustInt() != int64(out) {
|
|
|
|
return dyn.InvalidValue, diags.Append(diag.Diagnostic{
|
2024-07-23 17:20:11 +00:00
|
|
|
Severity: diag.Warning,
|
|
|
|
Summary: fmt.Sprintf(`cannot accurately represent "%d" as floating point number due to precision loss`, src.MustInt()),
|
|
|
|
Locations: []dyn.Location{src.Location()},
|
2024-07-25 15:16:27 +00:00
|
|
|
Paths: []dyn.Path{path},
|
2024-04-17 08:58:07 +00:00
|
|
|
})
|
|
|
|
}
|
2023-12-22 13:20:45 +00:00
|
|
|
case dyn.KindString:
|
2023-10-25 11:56:42 +00:00
|
|
|
var err error
|
|
|
|
out, err = strconv.ParseFloat(src.MustString(), 64)
|
|
|
|
if err != nil {
|
2024-02-19 10:44:51 +00:00
|
|
|
// Return verbatim if it's a pure variable reference.
|
|
|
|
if dynvar.IsPureVariableReference(src.MustString()) {
|
|
|
|
return src, nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 14:12:19 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(diag.Diagnostic{
|
2024-07-23 17:20:11 +00:00
|
|
|
Severity: diag.Warning,
|
|
|
|
Summary: fmt.Sprintf("cannot parse %q as a floating point number", src.MustString()),
|
|
|
|
Locations: []dyn.Location{src.Location()},
|
2024-07-25 15:16:27 +00:00
|
|
|
Paths: []dyn.Path{path},
|
2023-10-25 11:56:42 +00:00
|
|
|
})
|
|
|
|
}
|
2024-04-02 12:17:29 +00:00
|
|
|
case dyn.KindNil:
|
|
|
|
// Return a warning if the field is present but has a null value.
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(nullWarning(dyn.KindFloat, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
default:
|
2024-04-03 08:56:46 +00:00
|
|
|
return dyn.InvalidValue, diags.Append(typeMismatch(dyn.KindFloat, src, path))
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 11:27:27 +00:00
|
|
|
return dyn.NewValue(out, src.Locations()), diags
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
2024-06-26 10:25:32 +00:00
|
|
|
|
|
|
|
func (n normalizeOptions) normalizeInterface(typ reflect.Type, src dyn.Value, path dyn.Path) (dyn.Value, diag.Diagnostics) {
|
|
|
|
return src, nil
|
|
|
|
}
|