MCPcopy Create free account
hub / github.com/MG-RAST/Shock / parseOption

Function parseOption

shock-server/controller/node/multi.go:257–355  ·  view source on GitHub ↗
(key string, value string)

Source from the content-addressed store, hash-verified

255}
256
257func parseOption(key string, value string) bson.M {
258 not := false
259 // If value starts with ! then set flag to encapsulate query with $not operator and
260 // remove ! character from the beginning of the value string
261 if value[0] == '!' {
262 value = value[1:]
263 not = true
264 }
265
266 // Parsing query option into bson.M query object
267 opt := bson.M{}
268
269 // Only one of the following conditions can be met at a time
270 // mongodb doesn't allow for negating the entire query so the logic
271 // has to be written for each case below when the not flag is set.
272 if numValue, err := strconv.Atoi(value); err == nil {
273 // numeric values
274 if not {
275 opt = bson.M{"$and": []bson.M{bson.M{key: bson.M{"$ne": value}}, bson.M{key: bson.M{"$ne": numValue}}}}
276 } else {
277 opt = bson.M{"$or": []bson.M{bson.M{key: value}, bson.M{key: numValue}}}
278 }
279 } else if value == "null" {
280 // value is "null" => nil
281 if not {
282 opt = bson.M{"$and": []bson.M{bson.M{key: bson.M{"$ne": value}}, bson.M{key: bson.M{"$ne": nil}}}}
283 } else {
284 opt = bson.M{"$or": []bson.M{bson.M{key: value}, bson.M{key: nil}}}
285 }
286 } else if matches := RangeRegex.FindStringSubmatch(value); len(matches) > 0 {
287 // value matches the regex for a range
288 lowerBound := bson.M{}
289 upperBound := bson.M{}
290 var val1 interface{} = matches[2]
291 var val2 interface{} = matches[3]
292 parseTypedValue(&val1)
293 parseTypedValue(&val2)
294 if not {
295 if matches[1] == "[" {
296 lowerBound = bson.M{key: bson.M{"$lt": val1}}
297 } else {
298 lowerBound = bson.M{key: bson.M{"$lte": val1}}
299 }
300 if matches[4] == "]" {
301 upperBound = bson.M{key: bson.M{"$gt": val2}}
302 } else {
303 upperBound = bson.M{key: bson.M{"$gte": val2}}
304 }
305 opt = bson.M{"$or": []bson.M{lowerBound, upperBound}}
306 } else {
307 if matches[1] == "[" {
308 lowerBound = bson.M{key: bson.M{"$gte": val1}}
309 } else {
310 lowerBound = bson.M{key: bson.M{"$gt": val1}}
311 }
312 if matches[4] == "]" {
313 upperBound = bson.M{key: bson.M{"$lte": val2}}
314 } else {

Callers 1

ReadManyMethod · 0.85

Calls 1

parseTypedValueFunction · 0.85

Tested by

no test coverage detected