I have Json structure like below.
{
"type":"LogicalExpression",
"operator":"&&",
"left":{
"type":"BinaryExpression",
"operator":">",
"left":{
"name":"stage3_num1",
"type":"Identifier"
},
"right":{
"value":50,
"raw":"50",
"type":"Literal"
}
},
"right":{
"type":"LogicalExpression",
"operator":"||",
"left":{
"type":"BinaryExpression",
"operator":">",
"left":{
"name":"stage3_num2",
"type":"Identifier"
},
"right":{
"type":"Literal",
"value":200,
"raw":"200"
}
},
"right":{
"type":"BinaryExpression",
"operator":"<=",
"left":{
"type":"Identifier",
"name":"stage3_num3"
},
"right":{
"value":2000,
"raw":"2000",
"type":"Literal"
}
}
}
}
I want to build Mongo query from above structure where 'operator' denotes binary or logical expression, and 'left','right' denotes fields and value. But I am unable to handle logical expression inside logical expression in an infinte loop. Any ideas, suggestion towards solution will be a great help.
Edit:
query := bson.M{}
filter := jsonmap
for key, val := range filter {
if key == "type" && val == "BinaryExpression" {
a := sel(filter)
query = a
} else if key == "type" && val == "LogicalExpression" {
mongoparam := GetmongoParam(query["operator"].(string))
query[mongoparam] = []bson.M{}
//Further operations..
}
}
Where sel is func -
func sel(query map[string]interface{}) bson.M {
result := make(bson.M, len(query))
mongoparam := GetmongoParam(query["operator"].(string))
var left string
var right interface{}
md, ok := query["left"].(map[string]interface{})
if ok {
if md["type"] == "Identifier" {
left = md["name"].(string)
}
}
rd, ok := query["right"].(map[string]interface{})
if ok {
if rd["type"] == "Identifier" {
right = rd["name"]
} else {
right = rd["value"]
}
}
result[left] = bson.M{mongoparam: right}
return result
}
I am just unable to proceed in nested maps and not getting how to iterate till 'type' is 'LogicalExpression', and append queries together.