Skip to content

Commit

Permalink
feat(logic): json_prolog/2 handle null json value
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Apr 26, 2023
1 parent 7679f94 commit 94e9c5b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions x/logic/predicate/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func jsonToTerms(value any) (engine.Term, error) {
return engine.Integer(r.Int64()), nil
case bool:
return AtomBool(v), nil
case nil:
return AtomNull, nil
case map[string]any:
keys := lo.Keys(v)
sort.Strings(keys)
Expand Down
10 changes: 10 additions & 0 deletions x/logic/predicate/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ func TestJsonProlog(t *testing.T) {
}},
wantSuccess: true,
},
// ** JSON -> Prolog **
// Null
{
description: "convert json null value into prolog",
query: `json_prolog('null', Term).`,
wantResult: []types.TermResults{{
"Term": "@(null)",
}},
wantSuccess: true,
},
}
for nc, tc := range cases {
Convey(fmt.Sprintf("Given the query #%d: %s", nc, tc.query), func() {
Expand Down
2 changes: 2 additions & 0 deletions x/logic/predicate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,5 @@ func AtomBool(b bool) engine.Term {
}
return engine.NewAtom("@").Apply(r)
}

var AtomNull = engine.NewAtom("@").Apply(engine.NewAtom("null"))

0 comments on commit 94e9c5b

Please sign in to comment.