Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename libs/config -> libs/dyn #1086

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 0 additions & 76 deletions libs/config/path_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions libs/diag/diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package diag
import (
"fmt"

"github.com/databricks/cli/libs/config"
"github.com/databricks/cli/libs/dyn"
)

type Diagnostic struct {
Expand All @@ -19,7 +19,7 @@ type Diagnostic struct {

// Location is a source code location associated with the diagnostic message.
// It may be zero if there is no associated location.
Location config.Location
Location dyn.Location
}

// Errorf creates a new error diagnostic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package convert
import (
"testing"

"github.com/databricks/cli/libs/config"
"github.com/databricks/cli/libs/dyn"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func assertFromTypedToTypedEqual[T any](t *testing.T, src T) {
nv, err := FromTyped(src, config.NilValue)
nv, err := FromTyped(src, dyn.NilValue)
require.NoError(t, err)

var dst T
Expand Down
4 changes: 2 additions & 2 deletions libs/config/convert/error.go → libs/dyn/convert/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package convert
import (
"fmt"

"github.com/databricks/cli/libs/config"
"github.com/databricks/cli/libs/dyn"
)

type TypeError struct {
value config.Value
value dyn.Value
msg string
}

Expand Down
108 changes: 54 additions & 54 deletions libs/config/convert/from_typed.go → libs/dyn/convert/from_typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import (
"fmt"
"reflect"

"github.com/databricks/cli/libs/config"
"github.com/databricks/cli/libs/dyn"
)

// FromTyped converts changes made in the typed structure w.r.t. the configuration value
// back to the configuration value, retaining existing location information where possible.
func FromTyped(src any, ref config.Value) (config.Value, error) {
func FromTyped(src any, ref dyn.Value) (dyn.Value, error) {
srcv := reflect.ValueOf(src)

// Dereference pointer if necessary
for srcv.Kind() == reflect.Pointer {
if srcv.IsNil() {
return config.NilValue, nil
return dyn.NilValue, nil
}
srcv = srcv.Elem()
}
Expand All @@ -37,53 +37,53 @@ func FromTyped(src any, ref config.Value) (config.Value, error) {
return fromTypedFloat(srcv, ref)
}

return config.NilValue, fmt.Errorf("unsupported type: %s", srcv.Kind())
return dyn.NilValue, fmt.Errorf("unsupported type: %s", srcv.Kind())
}

func fromTypedStruct(src reflect.Value, ref config.Value) (config.Value, error) {
func fromTypedStruct(src reflect.Value, ref dyn.Value) (dyn.Value, error) {
// Check that the reference value is compatible or nil.
switch ref.Kind() {
case config.KindMap, config.KindNil:
case dyn.KindMap, dyn.KindNil:
default:
return config.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
return dyn.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
}

out := make(map[string]config.Value)
out := make(map[string]dyn.Value)
info := getStructInfo(src.Type())
for k, v := range info.FieldValues(src) {
// Convert the field taking into account the reference value (may be equal to config.NilValue).
nv, err := FromTyped(v.Interface(), ref.Get(k))
if err != nil {
return config.Value{}, err
return dyn.Value{}, err
}

if nv != config.NilValue {
if nv != dyn.NilValue {
out[k] = nv
}
}

// If the struct was equal to its zero value, emit a nil.
if len(out) == 0 {
return config.NilValue, nil
return dyn.NilValue, nil
}

return config.NewValue(out, ref.Location()), nil
return dyn.NewValue(out, ref.Location()), nil
}

func fromTypedMap(src reflect.Value, ref config.Value) (config.Value, error) {
func fromTypedMap(src reflect.Value, ref dyn.Value) (dyn.Value, error) {
// Check that the reference value is compatible or nil.
switch ref.Kind() {
case config.KindMap, config.KindNil:
case dyn.KindMap, dyn.KindNil:
default:
return config.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
return dyn.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
}

// Return nil if the map is nil.
if src.IsNil() {
return config.NilValue, nil
return dyn.NilValue, nil
}

out := make(map[string]config.Value)
out := make(map[string]dyn.Value)
iter := src.MapRange()
for iter.Next() {
k := iter.Key().String()
Expand All @@ -92,123 +92,123 @@ func fromTypedMap(src reflect.Value, ref config.Value) (config.Value, error) {
// Convert entry taking into account the reference value (may be equal to config.NilValue).
nv, err := FromTyped(v.Interface(), ref.Get(k))
if err != nil {
return config.Value{}, err
return dyn.Value{}, err
}

// Every entry is represented, even if it is a nil.
// Otherwise, a map with zero-valued structs would yield a nil as well.
out[k] = nv
}

return config.NewValue(out, ref.Location()), nil
return dyn.NewValue(out, ref.Location()), nil
}

func fromTypedSlice(src reflect.Value, ref config.Value) (config.Value, error) {
func fromTypedSlice(src reflect.Value, ref dyn.Value) (dyn.Value, error) {
// Check that the reference value is compatible or nil.
switch ref.Kind() {
case config.KindSequence, config.KindNil:
case dyn.KindSequence, dyn.KindNil:
default:
return config.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
return dyn.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
}

// Return nil if the slice is nil.
if src.IsNil() {
return config.NilValue, nil
return dyn.NilValue, nil
}

out := make([]config.Value, src.Len())
out := make([]dyn.Value, src.Len())
for i := 0; i < src.Len(); i++ {
v := src.Index(i)

// Convert entry taking into account the reference value (may be equal to config.NilValue).
nv, err := FromTyped(v.Interface(), ref.Index(i))
if err != nil {
return config.Value{}, err
return dyn.Value{}, err
}

out[i] = nv
}

return config.NewValue(out, ref.Location()), nil
return dyn.NewValue(out, ref.Location()), nil
}

func fromTypedString(src reflect.Value, ref config.Value) (config.Value, error) {
func fromTypedString(src reflect.Value, ref dyn.Value) (dyn.Value, error) {
switch ref.Kind() {
case config.KindString:
case dyn.KindString:
value := src.String()
if value == ref.MustString() {
return ref, nil
}

return config.V(value), nil
case config.KindNil:
return dyn.V(value), nil
case dyn.KindNil:
// This field is not set in the reference, so we only include it if it has a non-zero value.
// Otherwise, we would always include all zero valued fields.
if src.IsZero() {
return config.NilValue, nil
return dyn.NilValue, nil
}
return config.V(src.String()), nil
return dyn.V(src.String()), nil
}

return config.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
return dyn.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
}

func fromTypedBool(src reflect.Value, ref config.Value) (config.Value, error) {
func fromTypedBool(src reflect.Value, ref dyn.Value) (dyn.Value, error) {
switch ref.Kind() {
case config.KindBool:
case dyn.KindBool:
value := src.Bool()
if value == ref.MustBool() {
return ref, nil
}
return config.V(value), nil
case config.KindNil:
return dyn.V(value), nil
case dyn.KindNil:
// This field is not set in the reference, so we only include it if it has a non-zero value.
// Otherwise, we would always include all zero valued fields.
if src.IsZero() {
return config.NilValue, nil
return dyn.NilValue, nil
}
return config.V(src.Bool()), nil
return dyn.V(src.Bool()), nil
}

return config.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
return dyn.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
}

func fromTypedInt(src reflect.Value, ref config.Value) (config.Value, error) {
func fromTypedInt(src reflect.Value, ref dyn.Value) (dyn.Value, error) {
switch ref.Kind() {
case config.KindInt:
case dyn.KindInt:
value := src.Int()
if value == ref.MustInt() {
return ref, nil
}
return config.V(value), nil
case config.KindNil:
return dyn.V(value), nil
case dyn.KindNil:
// This field is not set in the reference, so we only include it if it has a non-zero value.
// Otherwise, we would always include all zero valued fields.
if src.IsZero() {
return config.NilValue, nil
return dyn.NilValue, nil
}
return config.V(src.Int()), nil
return dyn.V(src.Int()), nil
}

return config.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
return dyn.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
}

func fromTypedFloat(src reflect.Value, ref config.Value) (config.Value, error) {
func fromTypedFloat(src reflect.Value, ref dyn.Value) (dyn.Value, error) {
switch ref.Kind() {
case config.KindFloat:
case dyn.KindFloat:
value := src.Float()
if value == ref.MustFloat() {
return ref, nil
}
return config.V(value), nil
case config.KindNil:
return dyn.V(value), nil
case dyn.KindNil:
// This field is not set in the reference, so we only include it if it has a non-zero value.
// Otherwise, we would always include all zero valued fields.
if src.IsZero() {
return config.NilValue, nil
return dyn.NilValue, nil
}
return config.V(src.Float()), nil
return dyn.V(src.Float()), nil
}

return config.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
return dyn.Value{}, fmt.Errorf("unhandled type: %s", ref.Kind())
}
Loading