()
| 196 | } |
| 197 | |
| 198 | func (o *BinaryOperation) validate() error { |
| 199 | if err := o.LHS.validate(); err != nil { |
| 200 | return err |
| 201 | } |
| 202 | if err := o.RHS.validate(); err != nil { |
| 203 | return err |
| 204 | } |
| 205 | |
| 206 | lhsT := o.LHS.impliedType() |
| 207 | rhsT := o.RHS.impliedType() |
| 208 | |
| 209 | if o.Op == OpNotExists || o.Op == OpExists { |
| 210 | return fmt.Errorf("illegal operation for the given type: %s", o.String()) |
| 211 | } |
| 212 | |
| 213 | if !lhsT.isMatchingOperand(rhsT) { |
| 214 | return fmt.Errorf("binary operations must operate on the same type: %s", o.String()) |
| 215 | } |
| 216 | |
| 217 | if !o.Op.binaryTypesValid(lhsT, rhsT) { |
| 218 | return fmt.Errorf("illegal operation for the given types: %s", o.String()) |
| 219 | } |
| 220 | |
| 221 | // if this is a regex operator confirm the RHS is a valid regex |
| 222 | if o.Op == OpRegex || o.Op == OpNotRegex { |
| 223 | static, ok := o.RHS.(Static) |
| 224 | if !ok { |
| 225 | return fmt.Errorf("invalid type for %s or %s: %s", OpRegex, OpNotRegex, o.RHS.String()) |
| 226 | } |
| 227 | |
| 228 | switch rhsT { |
| 229 | case TypeString: |
| 230 | _, err := regexp.Compile(static.EncodeToString(false)) |
| 231 | if err != nil { |
| 232 | return fmt.Errorf("invalid regex: %s", o.RHS.String()) |
| 233 | } |
| 234 | case TypeStringArray: |
| 235 | strs, _ := static.StringArray() |
| 236 | for _, str := range strs { |
| 237 | _, err := regexp.Compile(str) |
| 238 | if err != nil { |
| 239 | return fmt.Errorf("invalid regex: %s", o.RHS.String()) |
| 240 | } |
| 241 | } |
| 242 | default: |
| 243 | return fmt.Errorf("invalid type for %s or %s: %s", OpRegex, OpNotRegex, o.RHS.String()) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // this condition may not be possible to hit since it's not parseable. |
| 248 | // however, if we did somehow end up this situation, it would be good to return |
| 249 | // a reasonable error |
| 250 | switch o.Op { |
| 251 | case OpSpansetChild, |
| 252 | OpSpansetParent, |
| 253 | OpSpansetDescendant, |
| 254 | OpSpansetAncestor, |
| 255 | OpSpansetSibling, |
no test coverage detected