Skip to content

Commit 417b49a

Browse files
authored
fix: (null) return correct value (#8)
1 parent 04c526f commit 417b49a

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

jlib/jlib.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ func (s StringCallable) toInterface() interface{} {
5050
// TypeOf implements the jsonata $type function that returns the data type of
5151
// the argument
5252
func TypeOf(x interface{}) (string, error) {
53+
if x == nil {
54+
return "null", nil
55+
}
5356
v := reflect.ValueOf(x)
5457
if jtypes.IsCallable(v) {
5558
return "function", nil

jsonata_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8122,7 +8122,49 @@ func readJSON(filename string) interface{} {
81228122
func TestNullValue(t *testing.T) {
81238123
nv := nullValue()
81248124
require.True(t, nv.IsValid())
8125+
require.True(t, nv.IsZero())
81258126
require.True(t, nv.IsNil())
81268127
require.True(t, nv.CanInterface())
81278128
require.True(t, nv.Interface() == nil)
81288129
}
8130+
8131+
func TestFuncType(t *testing.T) {
8132+
runTestCases(t, nil, []*testCase{
8133+
{
8134+
Expression: []string{
8135+
`$type("Hello World")`,
8136+
},
8137+
Output: "string",
8138+
},
8139+
{
8140+
Expression: []string{
8141+
`$type(true)`,
8142+
},
8143+
Output: "boolean",
8144+
},
8145+
{
8146+
Expression: []string{
8147+
`$type(2.3)`,
8148+
},
8149+
Output: "number",
8150+
},
8151+
{
8152+
Expression: []string{
8153+
`$type(3)`,
8154+
},
8155+
Output: "number",
8156+
},
8157+
{
8158+
Expression: []string{
8159+
`$type(null)`,
8160+
},
8161+
Output: "null",
8162+
},
8163+
{
8164+
Expression: []string{
8165+
`$type(a)`,
8166+
},
8167+
Error: ErrUndefined,
8168+
},
8169+
})
8170+
}

0 commit comments

Comments
 (0)