Skip to content

Commit

Permalink
feat(logic): json_prolog/2 handle boolean and null
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Apr 28, 2023
1 parent 4e2b8b6 commit 9c3b7f8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
21 changes: 18 additions & 3 deletions x/logic/predicate/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ func termsToJson(term engine.Term, env *engine.Env) ([]byte, error) {
case engine.Integer:
return json.Marshal(t)
case engine.Compound:
if t.Arity() == 2 && t.Functor().String() == "." {
// It's engine.List
switch t.Functor().String() {
case ".": // Represent an engine.List
if t.Arity() != 2 {
return nil, fmt.Errorf("wrong term arity for array, give %d, expected %d", t.Arity(), 2)
}

iter := engine.ListIterator{List: t, Env: env}

elements := make([]json.RawMessage, 0)
Expand All @@ -83,7 +87,7 @@ func termsToJson(term engine.Term, env *engine.Env) ([]byte, error) {
elements = append(elements, element)
}
return json.Marshal(elements)
} else {
case AtomJSON.String():
// It's a json atom
terms, err := ExtractJsonTerm(t, env)
if err != nil {
Expand All @@ -100,6 +104,17 @@ func termsToJson(term engine.Term, env *engine.Env) ([]byte, error) {
}
return json.Marshal(attributes)
}

if AtomBool(true).Compare(t, env) == 0 {
return json.Marshal(true)
} else if AtomBool(false).Compare(t, env) == 0 {
return json.Marshal(false)
} else if AtomNull.Compare(t, env) == 0 {
return json.Marshal(nil)
}

return nil, fmt.Errorf("invalid functor %s", t.Functor())

default:
return nil, fmt.Errorf("could not convert %s {%T} to json", t, t)
}
Expand Down
30 changes: 29 additions & 1 deletion x/logic/predicate/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestJsonProlog(t *testing.T) {
description: "invalid json term compound",
query: `json_prolog(Json, foo([a-b])).`,
wantSuccess: false,
wantError: fmt.Errorf("json_prolog/2: invalid functor foo. Expected json"),
wantError: fmt.Errorf("json_prolog/2: invalid functor foo"),
},
// ** Prolog -> JSON **
// Number
Expand Down Expand Up @@ -239,6 +239,34 @@ func TestJsonProlog(t *testing.T) {
}},
wantSuccess: true,
},
// ** Prolog -> JSON **
// Bool
{
description: "convert true boolean from prolog",
query: `json_prolog(Json, @(true)).`,
wantResult: []types.TermResults{{
"Json": "true",
}},
wantSuccess: true,
},
{
description: "convert false boolean from prolog",
query: `json_prolog(Json, @(false)).`,
wantResult: []types.TermResults{{
"Json": "false",
}},
wantSuccess: true,
},
// ** Prolog -> Json **
// Null
{
description: "convert json null value into prolog",
query: `json_prolog(Json, @(null)).`,
wantResult: []types.TermResults{{
"Json": "null",
}},
wantSuccess: true,
},
}
for nc, tc := range cases {
Convey(fmt.Sprintf("Given the query #%d: %s", nc, tc.query), func() {
Expand Down

0 comments on commit 9c3b7f8

Please sign in to comment.