Skip to content

Instantly share code, notes, and snippets.

@pranavgore09
Last active July 13, 2017 05:55
Show Gist options
  • Select an option

  • Save pranavgore09/1b7cfc71c31ae66a8348ceef3579b547 to your computer and use it in GitHub Desktop.

Select an option

Save pranavgore09/1b7cfc71c31ae66a8348ceef3579b547 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/fabric8-services/fabric8-wit/criteria"
"github.com/fabric8-services/fabric8-wit/workitem"
)
type Node struct {
Name string
Value *string
Children []*Node
}
func main() {
planner := "planner"
osio := "openshiftio"
rhel := "rhel"
a := Node{
Name: "OR",
Value: nil,
Children: []*Node{
&Node{
Name: "AND",
Value: nil,
Children: []*Node{
&Node{
Name: "space",
Value: &osio,
Children: nil,
},
&Node{
Name: "area",
Value: &planner,
Children: nil,
},
},
},
&Node{
Name: "space",
Value: &rhel,
Children: nil,
},
},
}
b := Node{
Name: "AND",
Value: nil,
Children: []*Node{
&Node{
Name: "space",
Value: &osio,
Children: nil,
}, &Node{
Name: "area",
Value: &planner,
Children: nil,
},
},
}
e1 := generateExpression2(&a)
where1, parameters1, compileError1 := workitem.Compile(e1)
fmt.Println(where1, parameters1, compileError1)
e2 := generateExpression2(&b)
where2, parameters2, compileError2 := workitem.Compile(e2)
fmt.Println(where2, parameters2, compileError2)
}
func isOperator(str string) bool {
return str == "AND" || str == "OR"
}
func generateExpression2(n *Node) criteria.Expression {
var myexpr []criteria.Expression
currentOperator := n.Name
if !isOperator(currentOperator) {
q := criteria.Equals(criteria.Field(n.Name), criteria.Literal([]string{*n.Value}))
myexpr = append(myexpr, q)
}
for _, child := range n.Children {
if isOperator(child.Name) {
q := generateExpression2(child)
myexpr = append(myexpr, q)
} else {
q := criteria.Equals(criteria.Field(child.Name), criteria.Literal([]string{*child.Value}))
myexpr = append(myexpr, q)
}
}
var e criteria.Expression
switch currentOperator {
case "AND":
for _, o := range myexpr {
if e == nil {
e = o
} else {
e = criteria.And(e, o)
}
}
case "OR":
for _, o := range myexpr {
if e == nil {
e = o
} else {
e = criteria.Or(e, o)
}
}
}
return e
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment