normalizeFilter will replace all quoted string with ? and return the list of quoted strings.
(filter string)
| 160 | |
| 161 | // normalizeFilter will replace all quoted string with ? and return the list of quoted strings. |
| 162 | func normalizeFilter(filter string) (string, []string, error) { |
| 163 | var ( |
| 164 | normalizedFilter string |
| 165 | quotedStrings []string |
| 166 | ) |
| 167 | inQuotes := false |
| 168 | lastQuoteIndex := 0 |
| 169 | for i, s := range filter { |
| 170 | if s == '"' { |
| 171 | if inQuotes { |
| 172 | quotedStrings = append(quotedStrings, filter[lastQuoteIndex+1:i]) |
| 173 | normalizedFilter += "?" |
| 174 | } else { |
| 175 | lastQuoteIndex = i |
| 176 | } |
| 177 | inQuotes = !inQuotes |
| 178 | } else if !inQuotes { |
| 179 | // If we are not in quotes, we need to normalize the filter. |
| 180 | // We need to add space before and after the comparator. |
| 181 | // For example, "a>b" should be normalized to "a > b". |
| 182 | switch s { |
| 183 | case '!': |
| 184 | normalizedFilter += " " |
| 185 | normalizedFilter += string(s) |
| 186 | case '<', '>': |
| 187 | normalizedFilter += " " |
| 188 | normalizedFilter += string(s) |
| 189 | if i+1 < len(filter) && filter[i+1] != '=' { |
| 190 | normalizedFilter += " " |
| 191 | } |
| 192 | case '=': |
| 193 | if i > 0 && (filter[i-1] != '!' && filter[i-1] != '<' && filter[i-1] != '>') { |
| 194 | normalizedFilter += " " |
| 195 | } |
| 196 | normalizedFilter += string(s) |
| 197 | normalizedFilter += " " |
| 198 | default: |
| 199 | normalizedFilter += string(s) |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if inQuotes { |
| 205 | return "", nil, errors.Errorf("invalid filter %q", filter) |
| 206 | } |
| 207 | |
| 208 | return normalizedFilter, quotedStrings, nil |
| 209 | } |
| 210 | |
| 211 | func convertToEngine(engine storepb.Engine) v1pb.Engine { |
| 212 | switch engine { |