Skip to content

Commit

Permalink
feat(logic): json_prolog/2 handle json array
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Apr 27, 2023
1 parent 94e9c5b commit ff1f248
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions x/logic/predicate/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ func jsonToTerms(value any) (engine.Term, error) {
}

return AtomJSON.Apply(engine.List(attributes...)), nil
case []any:
elements := make([]engine.Term, 0, len(v))
for _, element := range v {
term, err := jsonToTerms(element)
if err != nil {
return nil, err
}
elements = append(elements, term)
}
return engine.List(elements...), nil
default:
return nil, fmt.Errorf("could not convert %s (%T) to a prolog term", v, v)
}
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 @@ -128,6 +128,24 @@ func TestJsonProlog(t *testing.T) {
}},
wantSuccess: true,
},
// ** JSON -> Prolog **
// Array
{
description: "convert json array into prolog",
query: `json_prolog('["foo", "bar"]', Term).`,
wantResult: []types.TermResults{{
"Term": "[foo,bar]",
}},
wantSuccess: true,
},
{
description: "convert json string array into prolog",
query: `json_prolog('["string with space", "bar"]', Term).`,
wantResult: []types.TermResults{{
"Term": "['string with space',bar]",
}},
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 ff1f248

Please sign in to comment.