parseRoomMemberCountCondition parses a string like "2", "==2", "<2" into a function that checks if the argument to it fulfils the condition.
(s string)
| 96 | // into a function that checks if the argument to it fulfils the |
| 97 | // condition. |
| 98 | func parseRoomMemberCountCondition(s string) (func(int) bool, error) { |
| 99 | var b int |
| 100 | var cmp = func(a int) bool { return a == b } |
| 101 | switch { |
| 102 | case strings.HasPrefix(s, "<="): |
| 103 | cmp = func(a int) bool { return a <= b } |
| 104 | s = s[2:] |
| 105 | case strings.HasPrefix(s, ">="): |
| 106 | cmp = func(a int) bool { return a >= b } |
| 107 | s = s[2:] |
| 108 | case strings.HasPrefix(s, "<"): |
| 109 | cmp = func(a int) bool { return a < b } |
| 110 | s = s[1:] |
| 111 | case strings.HasPrefix(s, ">"): |
| 112 | cmp = func(a int) bool { return a > b } |
| 113 | s = s[1:] |
| 114 | case strings.HasPrefix(s, "=="): |
| 115 | // Same cmp as the default. |
| 116 | s = s[2:] |
| 117 | } |
| 118 | |
| 119 | v, err := strconv.ParseInt(s, 10, 64) |
| 120 | if err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | b = int(v) |
| 124 | return cmp, nil |
| 125 | } |
no outgoing calls