Skip to content
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
6 changes: 6 additions & 0 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (

// ValueOf converts the type to our value
func ValueOf(v any) Value {
if v == nil || reflect.TypeOf(v).Size() == 0 {
return Nil{}
}

switch v := v.(type) {
case Number:
return v
Expand Down Expand Up @@ -92,6 +96,8 @@ func ValueOf(v any) Value {
return sliceAsArray(v)
case nil, Nil:
return Nil{}
case struct{}:
return Nil{}
default:
out, err := json.Marshal(v)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

func TestValueOf(t *testing.T) {
type myNil struct{}
tests := []struct {
input any
output Value
Expand Down Expand Up @@ -48,6 +49,8 @@ func TestValueOf(t *testing.T) {
{input: []float64{1, 2, 3}, output: Numbers{1, 2, 3}},
{input: []bool{false, true}, output: Bools{false, true}},
{input: nil, output: Nil{}},
{input: struct{}{}, output: Nil{}},
{input: myNil{}, output: Nil{}},
{input: Nil{}, output: Nil{}},
{input: map[string]any{
"A": "foo",
Expand Down