Skip to content

Commit

Permalink
style(logic): clean some comment and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed May 2, 2023
1 parent b04b56e commit e41ea03
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
58 changes: 30 additions & 28 deletions x/logic/predicate/json.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package predicate

import (
"context"
"encoding/json"
"fmt"
"sort"
Expand Down Expand Up @@ -31,38 +32,40 @@ var AtomJSON = engine.NewAtom("json")
// # JSON conversion to Prolog.
// - json_prolog('{"foo": "bar"}', json([foo-bar])).
func JSONProlog(vm *engine.VM, j, term engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise {
var result engine.Term
return engine.Delay(func(ctx context.Context) *engine.Promise {
var result engine.Term

switch t1 := env.Resolve(j).(type) {
case engine.Variable:
case engine.Atom:
terms, err := jsonStringToTerms(t1.String())
if err != nil {
return engine.Error(fmt.Errorf("json_prolog/2: %w", err))
switch t1 := env.Resolve(j).(type) {
case engine.Variable:
case engine.Atom:
terms, err := jsonStringToTerms(t1.String())
if err != nil {
return engine.Error(fmt.Errorf("json_prolog/2: %w", err))
}
result = terms
default:
return engine.Error(fmt.Errorf("json_prolog/2: cannot unify json with %T", t1))
}
result = terms
default:
return engine.Error(fmt.Errorf("json_prolog/2: cannot unify json with %T", t1))
}

switch t2 := env.Resolve(term).(type) {
case engine.Variable:
if result == nil {
return engine.Error(fmt.Errorf("json_prolog/2: could not unify two variable"))
}
return engine.Unify(vm, term, result, cont, env)
default:
b, err := termsToJSON(t2, env)
if err != nil {
return engine.Error(fmt.Errorf("json_prolog/2: %w", err))
}
switch t2 := env.Resolve(term).(type) {
case engine.Variable:
if result == nil {
return engine.Error(fmt.Errorf("json_prolog/2: could not unify two variable"))
}
return engine.Unify(vm, term, result, cont, env)
default:
b, err := termsToJSON(t2, env)
if err != nil {
return engine.Error(fmt.Errorf("json_prolog/2: %w", err))
}

b, err = sdk.SortJSON(b)
if err != nil {
return engine.Error(fmt.Errorf("json_prolog/2: %w", err))
b, err = sdk.SortJSON(b)
if err != nil {
return engine.Error(fmt.Errorf("json_prolog/2: %w", err))
}
return engine.Unify(vm, j, util.StringToTerm(string(b)), cont, env)
}
return engine.Unify(vm, j, util.StringToTerm(string(b)), cont, env)
}
})
}

func jsonStringToTerms(j string) (engine.Term, error) {
Expand Down Expand Up @@ -102,7 +105,6 @@ func termsToJSON(term engine.Term, env *engine.Env) ([]byte, error) {
}
return json.Marshal(elements)
case AtomJSON.String():
// It's a json atom
terms, err := ExtractJSONTerm(t, env)
if err != nil {
return nil, err
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 @@ -370,6 +370,16 @@ func TestJsonPrologWithMoreComplexStructBidirectional(t *testing.T) {
term: "json([foo-bar])",
wantSuccess: true,
},
{
json: "'{\"foo\":\"null\"}'",
term: "json([foo-null])",
wantSuccess: true,
},
{
json: "'{\"foo\":null}'",
term: "json([foo- @(null)])",
wantSuccess: true,
},
{
json: "'{\"employee\":{\"age\":30,\"city\":\"New York\",\"name\":\"John\"}}'",
term: "json([employee-json([age-30,city-'New York',name-'John'])])",
Expand Down

0 comments on commit e41ea03

Please sign in to comment.