MCPcopy Create free account
hub / github.com/daodst/chat / parseRoomMemberCountCondition

Function parseRoomMemberCountCondition

internal/pushrules/util.go:98–125  ·  view source on GitHub ↗

parseRoomMemberCountCondition parses a string like "2", "==2", "<2" into a function that checks if the argument to it fulfils the condition.

(s string)

Source from the content-addressed store, hash-verified

96// into a function that checks if the argument to it fulfils the
97// condition.
98func 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}

Callers 2

conditionMatchesFunction · 0.85

Calls

no outgoing calls

Tested by 1