Define constant for the invalid `dyn.Value` (#1101)

## Changes

The nil value is a real valid value that we need to represent. To
accommodate this we introduced `dyn.KindInvalid` as the zero-value for
`dyn.Kind` (see #904), but did not yet update the comments on
`dyn.NilValue` or add tests for `kind.go`.

This also moves `KindNil` to be last in the definition order (least
likely to care about it).

## Tests

Tests pass.
This commit is contained in:
Pieter Noordhuis 2024-01-05 14:02:04 +01:00 committed by GitHub
parent bae220d1bc
commit d8a64e6617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 8 deletions

View File

@ -1,6 +1,9 @@
package dyn package dyn
import "time" import (
"fmt"
"time"
)
type Kind int type Kind int
@ -9,12 +12,12 @@ const (
KindInvalid Kind = iota KindInvalid Kind = iota
KindMap KindMap
KindSequence KindSequence
KindNil
KindString KindString
KindBool KindBool
KindInt KindInt
KindFloat KindFloat
KindTime KindTime
KindNil
) )
func kindOf(v any) Kind { func kindOf(v any) Kind {
@ -23,8 +26,6 @@ func kindOf(v any) Kind {
return KindMap return KindMap
case []Value: case []Value:
return KindSequence return KindSequence
case nil:
return KindNil
case string: case string:
return KindString return KindString
case bool: case bool:
@ -35,6 +36,8 @@ func kindOf(v any) Kind {
return KindFloat return KindFloat
case time.Time: case time.Time:
return KindTime return KindTime
case nil:
return KindNil
default: default:
panic("not handled") panic("not handled")
} }
@ -42,12 +45,12 @@ func kindOf(v any) Kind {
func (k Kind) String() string { func (k Kind) String() string {
switch k { switch k {
case KindInvalid:
return "invalid"
case KindMap: case KindMap:
return "map" return "map"
case KindSequence: case KindSequence:
return "sequence" return "sequence"
case KindNil:
return "nil"
case KindString: case KindString:
return "string" return "string"
case KindBool: case KindBool:
@ -58,7 +61,9 @@ func (k Kind) String() string {
return "float" return "float"
case KindTime: case KindTime:
return "time" return "time"
case KindNil:
return "nil"
default: default:
return "invalid" panic(fmt.Sprintf("invalid kind value: %d", k))
} }
} }

38
libs/dyn/kind_test.go Normal file
View File

@ -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()
})
}

View File

@ -15,7 +15,12 @@ type Value struct {
anchor bool anchor bool
} }
// NilValue is equal to the zero-value of Value. // InvalidValue is equal to the zero-value of Value.
var InvalidValue = Value{
k: KindInvalid,
}
// NilValue is a convenient constant for a nil value.
var NilValue = Value{ var NilValue = Value{
k: KindNil, k: KindNil,
} }

View File

@ -7,6 +7,12 @@ import (
"github.com/stretchr/testify/assert" "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) { func TestValueIsAnchor(t *testing.T) {
var zero dyn.Value var zero dyn.Value
assert.False(t, zero.IsAnchor()) assert.False(t, zero.IsAnchor())