From e41ea0345eaec04eb3dd543a24b827bd42ac282e Mon Sep 17 00:00:00 2001 From: Benjamin DENEUX Date: Tue, 2 May 2023 15:29:56 +0200 Subject: [PATCH] style(logic): clean some comment and tests --- x/logic/predicate/json.go | 58 ++++++++++++++++++---------------- x/logic/predicate/json_test.go | 10 ++++++ 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/x/logic/predicate/json.go b/x/logic/predicate/json.go index f3a8a99c..3fbffc07 100644 --- a/x/logic/predicate/json.go +++ b/x/logic/predicate/json.go @@ -1,6 +1,7 @@ package predicate import ( + "context" "encoding/json" "fmt" "sort" @@ -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) { @@ -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 diff --git a/x/logic/predicate/json_test.go b/x/logic/predicate/json_test.go index 5d8f7c4a..19c13334 100644 --- a/x/logic/predicate/json_test.go +++ b/x/logic/predicate/json_test.go @@ -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'])])",