Skip to content

Commit

Permalink
fix(logic): fix empty array management in json predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
ccamel committed Jun 25, 2023
1 parent 10022d0 commit 054f854
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions x/logic/predicate/atom.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ var AtomTrue = engine.NewAtom("true")
// AtomFalse is the term false.
var AtomFalse = engine.NewAtom("false")

// AtomEmptyArray is the term [].
var AtomEmptyArray = engine.NewAtom("[]")

// AtomNull is the term null.
var AtomNull = engine.NewAtom("null")

Expand All @@ -39,3 +42,9 @@ func MakeBool(b bool) engine.Term {

return AtomAt.Apply(AtomFalse)
}

// MakeEmptyArray returns is the compound term @([]).
// It is used to represent the empty array in json objects.
func MakeEmptyArray() engine.Term {
return AtomAt.Apply(AtomEmptyArray)
}
6 changes: 6 additions & 0 deletions x/logic/predicate/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func termsToJSON(term engine.Term, env *engine.Env) ([]byte, error) {
return json.Marshal(true)
case MakeBool(false).Compare(t, env) == 0:
return json.Marshal(false)
case MakeEmptyArray().Compare(t, env) == 0:
return json.Marshal([]json.RawMessage{})
case MakeNull().Compare(t, env) == 0:
return json.Marshal(nil)
}
Expand Down Expand Up @@ -168,6 +170,10 @@ func jsonToTerms(value any) (engine.Term, error) {
return AtomJSON.Apply(engine.List(attributes...)), nil
case []any:
elements := make([]engine.Term, 0, len(v))
if len(v) == 0 {
return MakeEmptyArray(), nil
}

for _, element := range v {
term, err := jsonToTerms(element)
if err != nil {
Expand Down

0 comments on commit 054f854

Please sign in to comment.