Skip to content

Commit

Permalink
refactor: move json related atoms to the atom file
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jun 25, 2023
1 parent 1ea3237 commit 10022d0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
32 changes: 32 additions & 0 deletions x/logic/predicate/atom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 5 additions & 8 deletions x/logic/predicate/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down
12 changes: 0 additions & 12 deletions x/logic/predicate/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down

0 comments on commit 10022d0

Please sign in to comment.