generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logic): convert json string to terms
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package predicate | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
"github.com/okp4/okp4d/x/logic/util" | ||
) | ||
|
||
// AtomJSON is a term which represents a json as a compound term `json([Pair])`. | ||
var AtomJSON = engine.NewAtom("json") | ||
|
||
// JsonProlog is a predicate that will unify a JSON string into prolog terms and vice versa. | ||
// | ||
// json_prolog(?Json, ?Term) is det | ||
// TODO: | ||
func JsonProlog(vm *engine.VM, j, term engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise { | ||
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)) | ||
} | ||
|
||
return engine.Unify(vm, term, terms, cont, env) | ||
default: | ||
return engine.Error(fmt.Errorf("did_components/2: cannot unify json with %T", t1)) | ||
} | ||
|
||
switch env.Resolve(term).(type) { | ||
default: | ||
return engine.Error(fmt.Errorf("json_prolog/2: not implemented")) | ||
} | ||
} | ||
|
||
func jsonStringToTerms(j string) (engine.Term, error) { | ||
var values any | ||
json.Unmarshal([]byte(j), &values) | ||
|
||
return jsonToTerms(values) | ||
} | ||
|
||
func jsonToTerms(value any) (engine.Term, error) { | ||
switch v := value.(type) { | ||
case string: | ||
return util.StringToTerm(v), nil | ||
default: | ||
return nil, fmt.Errorf("could not convert %s (%T) to a prolog term", v, v) | ||
} | ||
} |