From 10022d0197c031c1d6c0ee81facc5f3a53bef611 Mon Sep 17 00:00:00 2001 From: ccamel Date: Sat, 24 Jun 2023 18:05:26 +0200 Subject: [PATCH] refactor: move json related atoms to the atom file --- x/logic/predicate/atom.go | 32 ++++++++++++++++++++++++++++++++ x/logic/predicate/json.go | 13 +++++-------- x/logic/predicate/util.go | 12 ------------ 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/x/logic/predicate/atom.go b/x/logic/predicate/atom.go index e4b06a9e..88048e0e 100644 --- a/x/logic/predicate/atom.go +++ b/x/logic/predicate/atom.go @@ -7,3 +7,35 @@ import ( // AtomPair are terms with principal functor (-)/2. // For example, the term -(A, B) denotes the pair of elements A and B. var AtomPair = engine.NewAtom("-") + +// AtomJSON are terms with principal functor json/1. +// It is used to represent json objects. +var AtomJSON = engine.NewAtom("json") + +// AtomAt are terms with principal functor (@)/1. +// It is used to represent special values in json objects. +var AtomAt = engine.NewAtom("@") + +// AtomTrue is the term true. +var AtomTrue = engine.NewAtom("true") + +// AtomFalse is the term false. +var AtomFalse = engine.NewAtom("false") + +// AtomNull is the term null. +var AtomNull = engine.NewAtom("null") + +// MakeNull returns the compound term @(null). +// It is used to represent the null value in json objects. +func MakeNull() engine.Term { + return AtomAt.Apply(AtomNull) +} + +// MakeBool returns the compound term @(true) if b is true, otherwise @(false). +func MakeBool(b bool) engine.Term { + if b { + return AtomAt.Apply(AtomTrue) + } + + return AtomAt.Apply(AtomFalse) +} diff --git a/x/logic/predicate/json.go b/x/logic/predicate/json.go index aeb615d6..19e1e94a 100644 --- a/x/logic/predicate/json.go +++ b/x/logic/predicate/json.go @@ -17,9 +17,6 @@ import ( "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 @@ -125,11 +122,11 @@ func termsToJSON(term engine.Term, env *engine.Env) ([]byte, error) { } switch { - case AtomBool(true).Compare(t, env) == 0: + case MakeBool(true).Compare(t, env) == 0: return json.Marshal(true) - case AtomBool(false).Compare(t, env) == 0: + case MakeBool(false).Compare(t, env) == 0: return json.Marshal(false) - case AtomNull.Compare(t, env) == 0: + case MakeNull().Compare(t, env) == 0: return json.Marshal(nil) } @@ -153,9 +150,9 @@ func jsonToTerms(value any) (engine.Term, error) { } return engine.Integer(r.Int64()), nil case bool: - return AtomBool(v), nil + return MakeBool(v), nil case nil: - return AtomNull, nil + return MakeNull(), nil case map[string]any: keys := lo.Keys(v) sort.Strings(keys) diff --git a/x/logic/predicate/util.go b/x/logic/predicate/util.go index 73a2e9dd..4718f3ee 100644 --- a/x/logic/predicate/util.go +++ b/x/logic/predicate/util.go @@ -78,18 +78,6 @@ func ListToBytes(terms engine.ListIterator, env *engine.Env) ([]byte, error) { return bt, nil } -func AtomBool(b bool) engine.Term { - var r engine.Atom - if b { - r = engine.NewAtom("true") - } else { - r = engine.NewAtom("false") - } - return engine.NewAtom("@").Apply(r) -} - -var AtomNull = engine.NewAtom("@").Apply(engine.NewAtom("null")) - // ExtractJSONTerm is an utility function that would extract all attribute of a JSON object // that is represented in prolog with the `json` atom. //