forked from fabric8-services/fabric8-wit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_parser.go
38 lines (35 loc) · 1.14 KB
/
simple_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Package query This package implements a super basic parser that takes a string, converts it to json and
// constructs an AndExpression of "key == value" expressions of all the fields in the json object
// this is just a stand-in until we have defined a proper query language
package query
import (
"encoding/json"
. "github.com/fabric8io/almighty-core/criteria"
"github.com/pkg/errors"
)
// Parse parses strings of the form { "attribute1":value1,"attribute2":value2} into an expression of the form "attribute1=value1 and attribute2=value2"
// returns the expression "true" if empty
func Parse(exp *string) (Expression, error) {
if exp == nil || len(*exp) == 0 {
return Literal(true), nil
}
var unmarshalled map[string]interface{}
err := json.Unmarshal([]byte(*exp), &unmarshalled)
if err != nil {
return nil, errors.WithStack(err)
}
var result *Expression
if len(unmarshalled) > 0 {
for key, value := range unmarshalled {
current := Equals(Field(key), Literal(value))
if result == nil {
result = ¤t
} else {
current = And(*result, current)
result = ¤t
}
}
return *result, nil
}
return Literal(true), nil
}