Skip to content

Commit

Permalink
feat(logic): json_prolog/2 handle boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Apr 26, 2023
1 parent a60f332 commit 7679f94
Show file tree
Hide file tree
Showing 3 changed files with 30 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 @@ -64,6 +64,8 @@ func jsonToTerms(value any) (engine.Term, error) {
return nil, fmt.Errorf("could not convert number '%s' into integer term, overflow", v)
}
return engine.Integer(r.Int64()), nil
case bool:
return AtomBool(v), nil
case map[string]any:
keys := lo.Keys(v)
sort.Strings(keys)
Expand Down
18 changes: 18 additions & 0 deletions x/logic/predicate/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ func TestJsonProlog(t *testing.T) {
wantSuccess: false,
wantError: fmt.Errorf("json_prolog/2: could not convert number '10.4' into integer term, decimal number is not handled yet"),
},
// ** JSON -> Prolog **
// Bool
{
description: "convert json true boolean into prolog",
query: `json_prolog('true', Term).`,
wantResult: []types.TermResults{{
"Term": "@(true)",
}},
wantSuccess: true,
},
{
description: "convert json false boolean into prolog",
query: `json_prolog('false', Term).`,
wantResult: []types.TermResults{{
"Term": "@(false)",
}},
wantSuccess: true,
},
}
for nc, tc := range cases {
Convey(fmt.Sprintf("Given the query #%d: %s", nc, tc.query), func() {
Expand Down
10 changes: 10 additions & 0 deletions x/logic/predicate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,13 @@ func ListToBytes(terms engine.ListIterator, env *engine.Env) ([]byte, error) {
}
return bt, nil
}

func AtomBool(b bool) engine.Term {
var r engine.Atom
if b {
r = engine.NewAtom("true")
} else {
r = engine.NewAtom("false")
}
return engine.NewAtom("@").Apply(r)
}

0 comments on commit 7679f94

Please sign in to comment.