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

Define constant for the invalid dyn.Value #1101

Merged
merged 1 commit into from
Jan 5, 2024
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
19 changes: 12 additions & 7 deletions libs/dyn/kind.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dyn

import "time"
import (
"fmt"
"time"
)

type Kind int

Expand All @@ -9,12 +12,12 @@ const (
KindInvalid Kind = iota
KindMap
KindSequence
KindNil
KindString
KindBool
KindInt
KindFloat
KindTime
KindNil
)

func kindOf(v any) Kind {
Expand All @@ -23,8 +26,6 @@ func kindOf(v any) Kind {
return KindMap
case []Value:
return KindSequence
case nil:
return KindNil
case string:
return KindString
case bool:
Expand All @@ -35,19 +36,21 @@ func kindOf(v any) Kind {
return KindFloat
case time.Time:
return KindTime
case nil:
return KindNil
default:
panic("not handled")
}
}

func (k Kind) String() string {
switch k {
case KindInvalid:
return "invalid"
case KindMap:
return "map"
case KindSequence:
return "sequence"
case KindNil:
return "nil"
case KindString:
return "string"
case KindBool:
Expand All @@ -58,7 +61,9 @@ func (k Kind) String() string {
return "float"
case KindTime:
return "time"
case KindNil:
return "nil"
default:
return "invalid"
panic(fmt.Sprintf("invalid kind value: %d", k))
}
}
38 changes: 38 additions & 0 deletions libs/dyn/kind_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dyn_test

import (
"testing"

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

func TestKindZeroValue(t *testing.T) {
// Assert that the zero value of [dyn.Kind] is the invalid kind.
var k dyn.Kind
assert.Equal(t, dyn.KindInvalid, k)
}

func TestKindToString(t *testing.T) {
for _, tt := range []struct {
k dyn.Kind
s string
}{
{dyn.KindInvalid, "invalid"},
{dyn.KindMap, "map"},
{dyn.KindSequence, "sequence"},
{dyn.KindString, "string"},
{dyn.KindBool, "bool"},
{dyn.KindInt, "int"},
{dyn.KindFloat, "float"},
{dyn.KindTime, "time"},
{dyn.KindNil, "nil"},
} {
assert.Equal(t, tt.s, tt.k.String())
}

// Panic on unknown kind.
assert.PanicsWithValue(t, "invalid kind value: 100", func() {
_ = dyn.Kind(100).String()
})
}
7 changes: 6 additions & 1 deletion libs/dyn/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ type Value struct {
anchor bool
}

// NilValue is equal to the zero-value of Value.
// InvalidValue is equal to the zero-value of Value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the purpose of it EmptyValue or ZeroValue might be a better name, WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what it is, essentially, though I'd like to convey the validity of a value.

Both EmptyValue and ZeroValue sound like they may be valid, but it shouldn't be.

It's also symmetric with the name of the Kind and the IsValid function:

func (v Value) IsValid() bool {
	return v.k != KindInvalid
}

Then when we do some operation (like a get) and verify it returned an actual node in the tree, we can do:

out, _ := v.Get("foo.bar")
if !out.IsValid() {
  // foo.bar doesn't exist
}

var InvalidValue = Value{
k: KindInvalid,
}

// NilValue is a convenient constant for a nil value.
var NilValue = Value{
k: KindNil,
}
Expand Down
6 changes: 6 additions & 0 deletions libs/dyn/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"github.com/stretchr/testify/assert"
)

func TestInvalidValue(t *testing.T) {
// Assert that the zero value of [dyn.Value] is the invalid value.
var zero dyn.Value
assert.Equal(t, zero, dyn.InvalidValue)
}

func TestValueIsAnchor(t *testing.T) {
var zero dyn.Value
assert.False(t, zero.IsAnchor())
Expand Down
Loading