github.com/cloudwan/edgelq-sdk@v1.15.4/limits/resources/v1alpha2/limit/limit.pb.filter.go (about) 1 // Code generated by protoc-gen-goten-resource 2 // Resource: Limit 3 // DO NOT EDIT!!! 4 5 package limit 6 7 import ( 8 "fmt" 9 "math" 10 "strings" 11 12 "google.golang.org/grpc/codes" 13 "google.golang.org/grpc/status" 14 "google.golang.org/protobuf/proto" 15 16 gotenobject "github.com/cloudwan/goten-sdk/runtime/object" 17 gotenresource "github.com/cloudwan/goten-sdk/runtime/resource" 18 filterParser "github.com/cloudwan/goten-sdk/runtime/resource/filter" 19 utils "github.com/cloudwan/goten-sdk/runtime/utils" 20 ) 21 22 // proto imports 23 import ( 24 iam_project "github.com/cloudwan/edgelq-sdk/iam/resources/v1alpha2/project" 25 limit_pool "github.com/cloudwan/edgelq-sdk/limits/resources/v1alpha2/limit_pool" 26 meta_resource "github.com/cloudwan/edgelq-sdk/meta/resources/v1alpha2/resource" 27 meta_service "github.com/cloudwan/edgelq-sdk/meta/resources/v1alpha2/service" 28 meta "github.com/cloudwan/goten-sdk/types/meta" 29 ) 30 31 var ( 32 _ = new(fmt.Stringer) 33 _ = strings.Builder{} 34 _ = math.IsNaN 35 36 _ = new(proto.Message) 37 38 _ = new(gotenobject.FieldPath) 39 _ = gotenresource.WildcardId 40 ) 41 42 // make sure we're using proto imports 43 var ( 44 _ = &iam_project.Project{} 45 _ = &limit_pool.LimitPool{} 46 _ = &meta_resource.Resource{} 47 _ = &meta_service.Service{} 48 _ = &meta.Meta{} 49 ) 50 51 type FilterCondition interface { 52 gotenresource.FilterCondition 53 _IsFilterCondition() 54 _IsLimitFilterBuilderOrCondition() 55 And(...FilterCondition) FilterCondition 56 Evaluate(res *Limit) bool 57 58 // Whether this condition is at least as specific as other. 59 // When true, any Limit that passes this condition will also pass other condition. 60 Satisfies(other FilterCondition) bool 61 62 // Checks whether condition specifies given field path 63 // Useful for blacklisting protected paths in iam policy conditions 64 SpecifiesFieldPath(fp Limit_FieldPath) bool 65 } 66 67 func AndFilterConditions(conds ...FilterCondition) FilterCondition { 68 result := &FilterConditionComposite{ 69 Operator: filterParser.AND, 70 } 71 for _, condi := range conds { 72 switch cond := condi.(type) { 73 case *FilterConditionComposite: 74 if cond.Operator == filterParser.AND { 75 result.Conditions = append(result.Conditions, cond.Conditions...) 76 continue 77 } 78 default: 79 } 80 result.Conditions = append(result.Conditions, condi) 81 } 82 return result 83 } 84 85 type FilterConditionComposite struct { 86 Operator filterParser.CompositeOperator 87 Conditions []FilterCondition 88 } 89 90 func (cond *FilterConditionComposite) String() string { 91 substrs := make([]string, 0, len(cond.Conditions)) 92 for _, subcond := range cond.Conditions { 93 substrs = append(substrs, subcond.String()) 94 } 95 sep := fmt.Sprintf(" %s ", cond.Operator) 96 return "(" + strings.Join(substrs, sep) + ")" 97 } 98 99 func (cond *FilterConditionComposite) _IsFilterCondition() {} 100 101 func (cond *FilterConditionComposite) _IsLimitFilterBuilderOrCondition() {} 102 103 func (cond *FilterConditionComposite) And(conds ...FilterCondition) FilterCondition { 104 return AndFilterConditions(append([]FilterCondition{cond}, conds...)...) 105 } 106 107 func (cond *FilterConditionComposite) Evaluate(res *Limit) bool { 108 switch cond.Operator { 109 case filterParser.OR: 110 for _, subCond := range cond.Conditions { 111 if subCond.Evaluate(res) { 112 return true 113 } 114 } 115 return false 116 case filterParser.AND: 117 for _, subCond := range cond.Conditions { 118 if !subCond.Evaluate(res) { 119 return false 120 } 121 } 122 return true 123 default: 124 panic(fmt.Sprintf("Unsupported composite condition operator: %s", cond.Operator)) 125 } 126 } 127 128 func (cond *FilterConditionComposite) EvaluateRaw(res gotenresource.Resource) bool { 129 if typedRes, ok := res.(*Limit); !ok { 130 return false 131 } else { 132 return cond.Evaluate(typedRes) 133 } 134 } 135 136 func (cond *FilterConditionComposite) flattenConditions() (results []FilterCondition) { 137 for _, subcnd := range cond.Conditions { 138 switch tsubcnd := subcnd.(type) { 139 case *FilterConditionComposite: 140 if tsubcnd.Operator == cond.Operator { 141 results = append(results, tsubcnd.flattenConditions()...) 142 } else { 143 results = append(results, subcnd) // take it as it is 144 } 145 default: 146 results = append(results, subcnd) 147 } 148 } 149 return 150 } 151 152 func (cond *FilterConditionComposite) Satisfies(other FilterCondition) bool { 153 flattened := cond.flattenConditions() 154 switch cond.Operator { 155 case filterParser.AND: 156 switch tother := other.(type) { 157 case *FilterConditionComposite: 158 switch tother.Operator { 159 case filterParser.AND: 160 otherFlattened := tother.flattenConditions() 161 OtherSubcnds: 162 for _, otherSubcnd := range otherFlattened { 163 for _, subcnd := range flattened { 164 if subcnd.Satisfies(otherSubcnd) { 165 continue OtherSubcnds 166 } 167 } 168 return false 169 } 170 return true 171 case filterParser.OR: 172 otherFlattened := tother.flattenConditions() 173 for _, otherSubcnd := range otherFlattened { 174 if cond.Satisfies(otherSubcnd) { 175 return true 176 } 177 } 178 return false 179 default: 180 return false 181 } 182 default: 183 for _, subcnd := range flattened { 184 if subcnd.Satisfies(other) { 185 return true 186 } 187 } 188 return false 189 } 190 default: 191 panic(fmt.Errorf("unsupported condition type %s", cond.Operator)) 192 } 193 return false 194 } 195 196 func (cond *FilterConditionComposite) SatisfiesRaw(other gotenresource.FilterCondition) bool { 197 if typedCond, ok := other.(FilterCondition); !ok { 198 return false 199 } else { 200 return cond.Satisfies(typedCond) 201 } 202 } 203 204 func (cond *FilterConditionComposite) SpecifiesFieldPath(fp Limit_FieldPath) bool { 205 for _, subcnd := range cond.Conditions { 206 if subcnd.SpecifiesFieldPath(fp) { 207 return true 208 } 209 } 210 return false 211 } 212 213 func (cond *FilterConditionComposite) SpecifiesRawFieldPath(fp gotenobject.FieldPath) bool { 214 if typedFp, ok := fp.(Limit_FieldPath); !ok { 215 return false 216 } else { 217 return cond.SpecifiesFieldPath(typedFp) 218 } 219 } 220 221 func (cond *FilterConditionComposite) GetOperator() filterParser.CompositeOperator { 222 return cond.Operator 223 } 224 225 func (cond *FilterConditionComposite) GetSubConditions() []gotenresource.FilterCondition { 226 subConds := make([]gotenresource.FilterCondition, len(cond.Conditions)) 227 for idx, subCond := range cond.Conditions { 228 subConds[idx] = subCond 229 } 230 return subConds 231 } 232 233 func (cond *FilterConditionComposite) ConditionComposite() {} 234 235 type FilterConditionNot struct { 236 FilterCondition 237 } 238 239 func (cond *FilterConditionNot) String() string { 240 return "NOT " + cond.FilterCondition.String() 241 } 242 243 func (cond *FilterConditionNot) _IsFilterCondition() {} 244 245 func (cond *FilterConditionNot) _IsLimitFilterBuilderOrCondition() {} 246 247 func (cond *FilterConditionNot) And(conds ...FilterCondition) FilterCondition { 248 return AndFilterConditions(append([]FilterCondition{cond}, conds...)...) 249 } 250 251 func (cond *FilterConditionNot) Evaluate(res *Limit) bool { 252 return !cond.FilterCondition.Evaluate(res) 253 } 254 255 func (cond *FilterConditionNot) EvaluateRaw(res gotenresource.Resource) bool { 256 if typedRes, ok := res.(*Limit); !ok { 257 return false 258 } else { 259 return cond.Evaluate(typedRes) 260 } 261 } 262 263 func (cond *FilterConditionNot) Satisfies(other FilterCondition) bool { 264 switch tother := other.(type) { 265 case *FilterConditionNot: 266 return cond.FilterCondition.Satisfies(tother.FilterCondition) 267 default: 268 return !cond.FilterCondition.Satisfies(other) 269 } 270 } 271 272 func (cond *FilterConditionNot) SatisfiesRaw(other gotenresource.FilterCondition) bool { 273 if typedCond, ok := other.(FilterCondition); !ok { 274 return false 275 } else { 276 return cond.Satisfies(typedCond) 277 } 278 } 279 280 func (cond *FilterConditionNot) SpecifiesFieldPath(fp Limit_FieldPath) bool { 281 return cond.FilterCondition.SpecifiesFieldPath(fp) 282 } 283 284 func (cond *FilterConditionNot) SpecifiesRawFieldPath(fp gotenobject.FieldPath) bool { 285 if typedFp, ok := fp.(Limit_FieldPath); !ok { 286 return false 287 } else { 288 return cond.SpecifiesFieldPath(typedFp) 289 } 290 } 291 292 func (cond *FilterConditionNot) GetSubCondition() gotenresource.FilterCondition { 293 return cond.FilterCondition 294 } 295 296 func (cond *FilterConditionNot) ConditionNot() {} 297 298 type FilterConditionIsNull struct { 299 Not bool 300 FieldPath Limit_FieldPath 301 } 302 303 func (cond *FilterConditionIsNull) String() string { 304 if cond.Not { 305 return cond.FieldPath.String() + " IS NOT NULL" 306 } else { 307 return cond.FieldPath.String() + " IS NULL" 308 } 309 } 310 311 func (cond *FilterConditionIsNull) _IsFilterCondition() {} 312 313 func (cond *FilterConditionIsNull) _IsLimitFilterBuilderOrCondition() {} 314 315 func (cond *FilterConditionIsNull) And(conds ...FilterCondition) FilterCondition { 316 return AndFilterConditions(append([]FilterCondition{cond}, conds...)...) 317 } 318 319 func (cond *FilterConditionIsNull) Evaluate(res *Limit) bool { 320 if v, ok := cond.FieldPath.GetSingleRaw(res); !ok { 321 return !cond.Not 322 } else { 323 return cond.Not != utils.IsNil(v) 324 } 325 } 326 327 func (cond *FilterConditionIsNull) EvaluateRaw(res gotenresource.Resource) bool { 328 if typedRes, ok := res.(*Limit); !ok { 329 return false 330 } else { 331 return cond.Evaluate(typedRes) 332 } 333 } 334 335 func (cond *FilterConditionIsNull) asCompare() FilterCondition { 336 res := &FilterConditionCompare{ 337 Operator: filterParser.Eq, 338 Limit_FieldPathValue: cond.FieldPath.WithIValue(nil), 339 } 340 if cond.Not { 341 res.Operator = filterParser.Neq 342 } 343 return res 344 } 345 346 func (cond *FilterConditionIsNull) Satisfies(other FilterCondition) bool { 347 switch tother := other.(type) { 348 case *FilterConditionIsNull: 349 return cond.FieldPath.String() == tother.FieldPath.String() && cond.Not == tother.Not 350 case *FilterConditionCompare: 351 return cond.asCompare().Satisfies(tother) 352 default: 353 return false 354 } 355 } 356 357 func (cond *FilterConditionIsNull) SatisfiesRaw(other gotenresource.FilterCondition) bool { 358 if typedCond, ok := other.(FilterCondition); !ok { 359 return false 360 } else { 361 return cond.Satisfies(typedCond) 362 } 363 } 364 365 func (cond *FilterConditionIsNull) SpecifiesFieldPath(fp Limit_FieldPath) bool { 366 return cond.FieldPath.String() == fp.String() 367 } 368 369 func (cond *FilterConditionIsNull) SpecifiesRawFieldPath(fp gotenobject.FieldPath) bool { 370 if typedFp, ok := fp.(Limit_FieldPath); !ok { 371 return false 372 } else { 373 return cond.SpecifiesFieldPath(typedFp) 374 } 375 } 376 377 func (cond *FilterConditionIsNull) NotNull() bool { 378 return cond.Not 379 } 380 381 func (cond *FilterConditionIsNull) GetRawFieldPath() gotenobject.FieldPath { 382 return cond.FieldPath 383 } 384 385 func (cond *FilterConditionIsNull) ConditionIsNull() {} 386 387 type FilterConditionIsNaN struct { 388 Not bool 389 FieldPath Limit_FieldPath 390 } 391 392 func (cond *FilterConditionIsNaN) String() string { 393 if cond.Not { 394 return cond.FieldPath.String() + " IS NOT NaN" 395 } else { 396 return cond.FieldPath.String() + " IS NaN" 397 } 398 } 399 400 func (cond *FilterConditionIsNaN) _IsFilterCondition() {} 401 402 func (cond *FilterConditionIsNaN) _IsLimitFilterBuilderOrCondition() {} 403 404 func (cond *FilterConditionIsNaN) And(conds ...FilterCondition) FilterCondition { 405 return AndFilterConditions(append([]FilterCondition{cond}, conds...)...) 406 } 407 408 func (cond *FilterConditionIsNaN) Evaluate(res *Limit) bool { 409 v, ok := cond.FieldPath.GetSingleRaw(res) 410 if !ok { 411 return false 412 } 413 fv, ok := v.(float64) 414 if !ok { 415 return false 416 } 417 return math.IsNaN(fv) 418 } 419 420 func (cond *FilterConditionIsNaN) EvaluateRaw(res gotenresource.Resource) bool { 421 if typedRes, ok := res.(*Limit); !ok { 422 return false 423 } else { 424 return cond.Evaluate(typedRes) 425 } 426 } 427 428 func (cond *FilterConditionIsNaN) Satisfies(other FilterCondition) bool { 429 switch tother := other.(type) { 430 case *FilterConditionIsNaN: 431 return cond.FieldPath.String() == tother.FieldPath.String() && cond.Not == tother.Not 432 default: 433 return false 434 } 435 } 436 437 func (cond *FilterConditionIsNaN) SatisfiesRaw(other gotenresource.FilterCondition) bool { 438 if typedCond, ok := other.(FilterCondition); !ok { 439 return false 440 } else { 441 return cond.Satisfies(typedCond) 442 } 443 } 444 445 func (cond *FilterConditionIsNaN) SpecifiesFieldPath(fp Limit_FieldPath) bool { 446 return cond.FieldPath.String() == fp.String() 447 } 448 449 func (cond *FilterConditionIsNaN) SpecifiesRawFieldPath(fp gotenobject.FieldPath) bool { 450 if typedFp, ok := fp.(Limit_FieldPath); !ok { 451 return false 452 } else { 453 return cond.SpecifiesFieldPath(typedFp) 454 } 455 } 456 457 func (cond *FilterConditionIsNaN) GetRawFieldPath() gotenobject.FieldPath { 458 return cond.FieldPath 459 } 460 461 func (cond *FilterConditionIsNaN) ConditionIsNaN() {} 462 463 type FilterConditionCompare struct { 464 Operator filterParser.CompareOperator 465 Limit_FieldPathValue 466 } 467 468 func (cond *FilterConditionCompare) String() string { 469 jsonValue, err := utils.JsonMarshal(cond.Limit_FieldPathValue.GetRawValue()) 470 if err != nil { 471 panic(err) 472 } 473 return fmt.Sprintf("%s %s %s", cond.Limit_FieldPathValue, cond.Operator, jsonValue) 474 } 475 476 func (cond *FilterConditionCompare) _IsFilterCondition() {} 477 478 func (cond *FilterConditionCompare) _IsLimitFilterBuilderOrCondition() {} 479 480 func (cond *FilterConditionCompare) And(conds ...FilterCondition) FilterCondition { 481 return AndFilterConditions(append([]FilterCondition{cond}, conds...)...) 482 } 483 484 func (cond *FilterConditionCompare) Evaluate(res *Limit) bool { 485 // Special evaluation for name or reference - may include wildcards 486 if nameOrRefFPV, ok := cond.Limit_FieldPathValue.GetRawValue().(gotenresource.Name); ok { 487 if otherObj, ok := cond.Limit_FieldPathValue.GetSingleRaw(res); ok { 488 match := nameOrRefFPV.Matches(otherObj) 489 switch cond.Operator { 490 case filterParser.Eq: 491 return match 492 case filterParser.Neq: 493 return !match 494 default: 495 return false 496 } 497 } 498 } 499 // special evaluation for objects 500 if objValue, ok := cond.Limit_FieldPathValue.GetRawValue().(proto.Message); ok { 501 if otherObj, ok := cond.Limit_FieldPathValue.GetSingleRaw(res); ok { 502 switch cond.Operator { 503 case filterParser.Eq: 504 return proto.Equal(objValue, otherObj.(proto.Message)) 505 case filterParser.Neq: 506 return !proto.Equal(objValue, otherObj.(proto.Message)) 507 default: 508 return false 509 } 510 } 511 } 512 cmpResult, comparable := cond.Limit_FieldPathValue.CompareWith(res) 513 if !comparable { 514 return false 515 } 516 return cond.Operator.MatchCompareResult(cmpResult) 517 } 518 519 func (cond *FilterConditionCompare) EvaluateRaw(res gotenresource.Resource) bool { 520 if typedRes, ok := res.(*Limit); !ok { 521 return false 522 } else { 523 return cond.Evaluate(typedRes) 524 } 525 } 526 527 func (cond *FilterConditionCompare) Satisfies(other FilterCondition) bool { 528 switch tother := other.(type) { 529 case *FilterConditionCompare: 530 if cond.Limit_FieldPathValue.String() != tother.Limit_FieldPathValue.String() { 531 return false 532 } 533 othertmp := new(Limit) 534 tother.SetTo(&othertmp) 535 if cmp, comparable := cond.CompareWith(othertmp); !comparable { 536 return false 537 } else { 538 return filterParser.CompareSatisfies(tother.Operator, cond.Operator, cmp) 539 } 540 case *FilterConditionIn: 541 if cond.Operator != filterParser.Eq { 542 return false 543 } 544 if cond.Limit_FieldPathValue.String() != tother.Limit_FieldPathArrayOfValues.String() { 545 return false 546 } 547 for _, inv := range tother.GetRawValues() { 548 othertmp := new(Limit) 549 tother.WithIValue(inv).SetTo(&othertmp) 550 if cmp, comparable := cond.Limit_FieldPathValue.CompareWith(othertmp); comparable && cmp == 0 { 551 return true 552 } 553 } 554 return false 555 case *FilterConditionComposite: 556 if tother.Operator == filterParser.AND { 557 for _, othersubcnd := range tother.flattenConditions() { 558 if !cond.Satisfies(othersubcnd) { 559 return false 560 } 561 } 562 return true 563 } else { // OR 564 for _, othersubcnd := range tother.flattenConditions() { 565 if cond.Satisfies(othersubcnd) { 566 return true 567 } 568 } 569 return false 570 } 571 default: 572 return false 573 } 574 } 575 576 func (cond *FilterConditionCompare) SatisfiesRaw(other gotenresource.FilterCondition) bool { 577 if typedCond, ok := other.(FilterCondition); !ok { 578 return false 579 } else { 580 return cond.Satisfies(typedCond) 581 } 582 } 583 584 func (cond *FilterConditionCompare) SpecifiesFieldPath(fp Limit_FieldPath) bool { 585 return cond.Limit_FieldPathValue.String() == fp.String() 586 } 587 588 func (cond *FilterConditionCompare) SpecifiesRawFieldPath(fp gotenobject.FieldPath) bool { 589 if typedFp, ok := fp.(Limit_FieldPath); !ok { 590 return false 591 } else { 592 return cond.SpecifiesFieldPath(typedFp) 593 } 594 } 595 596 func (cond *FilterConditionCompare) GetOperator() filterParser.CompareOperator { 597 return cond.Operator 598 } 599 600 func (cond *FilterConditionCompare) GetRawFieldPath() gotenobject.FieldPath { 601 return cond.Limit_FieldPathValue 602 } 603 604 func (cond *FilterConditionCompare) GetRawFieldPathValue() gotenobject.FieldPathValue { 605 return cond.Limit_FieldPathValue 606 } 607 608 func (cond *FilterConditionCompare) ConditionCompare() {} 609 610 type FilterConditionContains struct { 611 Type gotenresource.ConditionContainsType 612 FieldPath Limit_FieldPath 613 614 Value Limit_FieldPathArrayItemValue 615 Values []Limit_FieldPathArrayItemValue 616 } 617 618 func (cond *FilterConditionContains) String() string { 619 switch cond.ConditionContainsType() { 620 case gotenresource.ConditionContainsTypeValue: 621 jsonValue, err := utils.JsonMarshal(cond.Value.GetRawItemValue()) 622 if err != nil { 623 panic(err) 624 } 625 return fmt.Sprintf("%s CONTAINS %s", cond.FieldPath, string(jsonValue)) 626 case gotenresource.ConditionContainsTypeAny, gotenresource.ConditionContainsTypeAll: 627 jsonValues := make([]string, len(cond.Values)) 628 for i, v := range cond.Values { 629 if jsonValue, err := utils.JsonMarshal(v.GetRawItemValue()); err != nil { 630 panic(err) 631 } else { 632 jsonValues[i] = string(jsonValue) 633 } 634 } 635 return fmt.Sprintf("%s CONTAINS %s %s", cond.FieldPath, cond.ConditionContainsType(), fmt.Sprintf("(%s)", strings.Join(jsonValues, ", "))) 636 default: 637 panic(gotenresource.NewUnknownConditionContainsType(cond.ConditionContainsType())) 638 } 639 } 640 641 func (cond *FilterConditionContains) ConditionContainsType() gotenresource.ConditionContainsType { 642 return cond.Type 643 } 644 645 func (cond *FilterConditionContains) _IsFilterCondition() {} 646 647 func (cond *FilterConditionContains) _IsLimitFilterBuilderOrCondition() {} 648 649 func (cond *FilterConditionContains) And(conds ...FilterCondition) FilterCondition { 650 return AndFilterConditions(append([]FilterCondition{cond}, conds...)...) 651 } 652 653 func (cond *FilterConditionContains) Evaluate(res *Limit) bool { 654 switch cond.ConditionContainsType() { 655 case gotenresource.ConditionContainsTypeValue: 656 return cond.Value.ContainsValue(res) 657 case gotenresource.ConditionContainsTypeAny: 658 for _, v := range cond.Values { 659 if v.ContainsValue(res) { 660 return true 661 } 662 } 663 return false 664 case gotenresource.ConditionContainsTypeAll: 665 for _, v := range cond.Values { 666 if !v.ContainsValue(res) { 667 return false 668 } 669 } 670 return true 671 default: 672 panic(gotenresource.NewUnknownConditionContainsType(cond.ConditionContainsType())) 673 } 674 } 675 676 func (cond *FilterConditionContains) EvaluateRaw(res gotenresource.Resource) bool { 677 if typedRes, ok := res.(*Limit); !ok { 678 return false 679 } else { 680 return cond.Evaluate(typedRes) 681 } 682 } 683 684 func (cond *FilterConditionContains) Satisfies(other FilterCondition) bool { 685 switch tother := other.(type) { 686 case *FilterConditionContains: 687 if cond.ConditionContainsType().IsValue() && tother.ConditionContainsType().IsValue() { 688 othertmp := new(Limit) 689 tother.Value.WithIValue(tother.GetRawFieldPathItemValue().GetRawItemValue()).SetTo(&othertmp) 690 return cond.Value.ContainsValue(othertmp) 691 } 692 return false 693 default: 694 return false 695 } 696 } 697 698 func (cond *FilterConditionContains) SatisfiesRaw(other gotenresource.FilterCondition) bool { 699 if typedCond, ok := other.(FilterCondition); !ok { 700 return false 701 } else { 702 return cond.Satisfies(typedCond) 703 } 704 } 705 706 func (cond *FilterConditionContains) SpecifiesFieldPath(fp Limit_FieldPath) bool { 707 return cond.FieldPath.String() == fp.String() 708 } 709 710 func (cond *FilterConditionContains) SpecifiesRawFieldPath(fp gotenobject.FieldPath) bool { 711 if typedFp, ok := fp.(Limit_FieldPath); !ok { 712 return false 713 } else { 714 return cond.SpecifiesFieldPath(typedFp) 715 } 716 } 717 718 func (cond *FilterConditionContains) GetFieldPath() Limit_FieldPath { 719 return cond.FieldPath 720 } 721 722 func (cond *FilterConditionContains) GetRawFieldPath() gotenobject.FieldPath { 723 return cond.FieldPath 724 } 725 726 func (cond *FilterConditionContains) GetRawFieldPathItemValue() gotenobject.FieldPathArrayItemValue { 727 switch cond.ConditionContainsType() { 728 case gotenresource.ConditionContainsTypeValue: 729 return cond.Value 730 default: 731 panic(fmt.Errorf("unable to get value for condition contains type %s", cond.ConditionContainsType())) 732 } 733 } 734 735 func (cond *FilterConditionContains) GetRawFieldPathItemValues() (res []gotenobject.FieldPathArrayItemValue) { 736 switch cond.ConditionContainsType() { 737 case gotenresource.ConditionContainsTypeAny, gotenresource.ConditionContainsTypeAll: 738 for _, fpaiv := range cond.Values { 739 res = append(res, fpaiv) 740 } 741 default: 742 panic(fmt.Errorf("unable to get values for condition contains type %s", cond.ConditionContainsType())) 743 } 744 return 745 } 746 747 func (cond *FilterConditionContains) ConditionContains() {} 748 749 type FilterConditionIn struct { 750 Limit_FieldPathArrayOfValues 751 } 752 753 func (cond *FilterConditionIn) String() string { 754 jsonValues, err := utils.JsonMarshal(cond.Limit_FieldPathArrayOfValues.GetRawValues()) 755 if err != nil { 756 panic(err) 757 } 758 return fmt.Sprintf("%s IN %s", cond.Limit_FieldPathArrayOfValues, jsonValues) 759 } 760 761 func (cond *FilterConditionIn) _IsFilterCondition() {} 762 763 func (cond *FilterConditionIn) _IsLimitFilterBuilderOrCondition() {} 764 765 func (cond *FilterConditionIn) And(conds ...FilterCondition) FilterCondition { 766 return AndFilterConditions(append([]FilterCondition{cond}, conds...)...) 767 } 768 769 func (cond *FilterConditionIn) Evaluate(res *Limit) bool { 770 for _, inValue := range cond.Limit_FieldPathArrayOfValues.GetRawValues() { 771 if cmp, ok := cond.Limit_FieldPathArrayOfValues.WithIValue(inValue).CompareWith(res); ok && cmp == 0 { 772 return true 773 } 774 } 775 return false 776 } 777 778 func (cond *FilterConditionIn) EvaluateRaw(res gotenresource.Resource) bool { 779 if typedRes, ok := res.(*Limit); !ok { 780 return false 781 } else { 782 return cond.Evaluate(typedRes) 783 } 784 } 785 786 func (cond *FilterConditionIn) Satisfies(other FilterCondition) bool { 787 switch tother := other.(type) { 788 case *FilterConditionIn: 789 outer: 790 for _, cval := range cond.Limit_FieldPathArrayOfValues.GetRawValues() { 791 for _, otherval := range tother.Limit_FieldPathArrayOfValues.GetRawValues() { 792 othertmp := new(Limit) 793 tother.Limit_FieldPathArrayOfValues.WithIValue(otherval).SetTo(&othertmp) 794 if cmp, comparable := cond.Limit_FieldPathArrayOfValues.WithIValue(cval).CompareWith(othertmp); comparable && cmp == 0 { 795 continue outer 796 } 797 } 798 return false 799 } 800 return true 801 default: 802 for _, cval := range cond.Limit_FieldPathArrayOfValues.GetRawValues() { 803 subcnd := &FilterConditionCompare{ 804 Operator: filterParser.Eq, 805 Limit_FieldPathValue: cond.Limit_FieldPathArrayOfValues.WithIValue(cval), 806 } 807 if !subcnd.Satisfies(tother) { 808 return false 809 } 810 } 811 return true 812 } 813 } 814 815 func (cond *FilterConditionIn) SatisfiesRaw(other gotenresource.FilterCondition) bool { 816 if typedCond, ok := other.(FilterCondition); !ok { 817 return false 818 } else { 819 return cond.Satisfies(typedCond) 820 } 821 } 822 823 func (cond *FilterConditionIn) SpecifiesFieldPath(fp Limit_FieldPath) bool { 824 return cond.Limit_FieldPathArrayOfValues.String() == fp.String() 825 } 826 827 func (cond *FilterConditionIn) SpecifiesRawFieldPath(fp gotenobject.FieldPath) bool { 828 if typedFp, ok := fp.(Limit_FieldPath); !ok { 829 return false 830 } else { 831 return cond.SpecifiesFieldPath(typedFp) 832 } 833 } 834 835 func (cond *FilterConditionIn) GetRawFieldPath() gotenobject.FieldPath { 836 return cond.Limit_FieldPathArrayOfValues 837 } 838 839 func (cond *FilterConditionIn) GetRawFieldPathArrayOfValues() gotenobject.FieldPathArrayOfValues { 840 return cond.Limit_FieldPathArrayOfValues 841 } 842 843 func (cond *FilterConditionIn) ConditionIn() {} 844 845 type FilterConditionNotIn struct { 846 Limit_FieldPathArrayOfValues 847 } 848 849 func (cond *FilterConditionNotIn) String() string { 850 jsonValues, err := utils.JsonMarshal(cond.Limit_FieldPathArrayOfValues.GetRawValues()) 851 if err != nil { 852 panic(err) 853 } 854 return fmt.Sprintf("%s NOT IN %s", cond.Limit_FieldPathArrayOfValues, jsonValues) 855 } 856 857 func (cond *FilterConditionNotIn) _IsFilterCondition() {} 858 859 func (cond *FilterConditionNotIn) _IsLimitFilterBuilderOrCondition() {} 860 861 func (cond *FilterConditionNotIn) And(conds ...FilterCondition) FilterCondition { 862 return AndFilterConditions(append([]FilterCondition{cond}, conds...)...) 863 } 864 865 func (cond *FilterConditionNotIn) Evaluate(res *Limit) bool { 866 for _, inValue := range cond.Limit_FieldPathArrayOfValues.GetRawValues() { 867 if cmp, ok := cond.Limit_FieldPathArrayOfValues.WithIValue(inValue).CompareWith(res); ok && cmp == 0 { 868 return false 869 } 870 } 871 return true 872 } 873 874 func (cond *FilterConditionNotIn) EvaluateRaw(res gotenresource.Resource) bool { 875 if typedRes, ok := res.(*Limit); !ok { 876 return false 877 } else { 878 return cond.Evaluate(typedRes) 879 } 880 } 881 882 func (cond *FilterConditionNotIn) Satisfies(other FilterCondition) bool { 883 return false 884 } 885 886 func (cond *FilterConditionNotIn) SatisfiesRaw(other gotenresource.FilterCondition) bool { 887 if typedCond, ok := other.(FilterCondition); !ok { 888 return false 889 } else { 890 return cond.Satisfies(typedCond) 891 } 892 } 893 894 func (cond *FilterConditionNotIn) SpecifiesFieldPath(fp Limit_FieldPath) bool { 895 return cond.Limit_FieldPathArrayOfValues.String() == fp.String() 896 } 897 898 func (cond *FilterConditionNotIn) SpecifiesRawFieldPath(fp gotenobject.FieldPath) bool { 899 if typedFp, ok := fp.(Limit_FieldPath); !ok { 900 return false 901 } else { 902 return cond.SpecifiesFieldPath(typedFp) 903 } 904 } 905 906 func (cond *FilterConditionNotIn) GetRawFieldPath() gotenobject.FieldPath { 907 return cond.Limit_FieldPathArrayOfValues 908 } 909 910 func (cond *FilterConditionNotIn) GetRawFieldPathArrayOfValues() gotenobject.FieldPathArrayOfValues { 911 return cond.Limit_FieldPathArrayOfValues 912 } 913 914 func (cond *FilterConditionNotIn) ConditionNotIn() {} 915 916 type Filter struct { 917 FilterCondition 918 } 919 920 func (filter *Filter) _IsLimitFilterBuilderOrCondition() {} 921 922 // GetCondition is a getter of FilterCondition, which also handles nil pointer 923 func (filter *Filter) GetCondition() FilterCondition { 924 if filter == nil { 925 return AndFilterConditions() 926 } else { 927 return filter.FilterCondition 928 } 929 } 930 931 // Evaluate is a wrapper on FilterCondition, which also handles nil pointer 932 func (filter *Filter) Evaluate(res *Limit) bool { 933 return filter.GetCondition().Evaluate(res) 934 } 935 936 func (filter *Filter) EvaluateRaw(res gotenresource.Resource) bool { 937 if typedRes, ok := res.(*Limit); !ok { 938 return false 939 } else { 940 return filter.Evaluate(typedRes) 941 } 942 } 943 944 func (filter *Filter) GetRawCondition() gotenresource.FilterCondition { 945 if filter == nil { 946 return nil 947 } 948 return filter.GetCondition() 949 } 950 951 // FilterSlice is a helper for filtering arrays 952 func (filter *Filter) FilterSlice(in []*Limit) (out []*Limit) { 953 for _, res := range in { 954 if filter.Evaluate(res) { 955 out = append(out, res) 956 } 957 } 958 return 959 } 960 961 // implement methods required by protobuf-go library for string-struct conversion 962 963 func (filter *Filter) ProtoString() (string, error) { 964 if filter == nil || filter.FilterCondition == nil { 965 return "", nil 966 } 967 return filter.FilterCondition.String(), nil 968 } 969 970 func (filter *Filter) ParseProtoString(data string) error { 971 expression, err := filterParser.Parse([]byte(data)) 972 if err != nil { 973 return status.Error(codes.InvalidArgument, err.Error()) 974 } 975 976 condition, err := makeFilterConditionFromOr(expression.And) 977 if err != nil { 978 return err 979 } 980 filter.FilterCondition = condition 981 return nil 982 } 983 984 func (filter *Filter) String() string { 985 if filter == nil || filter.FilterCondition == nil { 986 return "<nil>" 987 } 988 return filter.FilterCondition.String() 989 } 990 991 func (filter *Filter) SetFromCliFlag(raw string) error { 992 return filter.ParseProtoString(raw) 993 } 994 995 // helpers 996 997 func makeFilterConditionFromOperand(condition *filterParser.ConditionOperand) (FilterCondition, error) { 998 path, err := ParseLimit_FieldPath(condition.FieldPath) 999 if err != nil { 1000 return nil, err 1001 } 1002 rhs := condition.ConditionRHS 1003 valueJSON, err := rhs.JSONValue() 1004 if err != nil { 1005 return nil, status.Error(codes.Internal, err.Error()) 1006 } 1007 1008 if rhs.Compare != nil { 1009 cmp := rhs.Compare 1010 if !path.IsLeaf() && !(cmp.Operator == filterParser.Eq || cmp.Operator == filterParser.Neq) { 1011 return nil, status.Errorf(codes.InvalidArgument, "path '%s' is not comparable leaf value for operator %s", path, cmp.Operator) 1012 } 1013 1014 // translate null comparison to IS(NOT)NULL 1015 if cmp.Value.Null { 1016 switch cmp.Operator { 1017 case filterParser.Eq: 1018 return &FilterConditionIsNull{false, path}, nil 1019 case filterParser.Neq: 1020 return &FilterConditionIsNull{true, path}, nil 1021 default: 1022 return nil, status.Errorf(codes.InvalidArgument, "operator '%s' isn't valid when comparing null value", cmp.Operator) 1023 } 1024 } 1025 1026 pfv, err := ParseLimit_FieldPathValue(path.String(), string(valueJSON)) 1027 if err != nil { 1028 return nil, status.Errorf(codes.InvalidArgument, "error when parsing filter value for field path '%s': %s", path, err) 1029 } 1030 1031 return &FilterConditionCompare{ 1032 Operator: cmp.Operator, 1033 Limit_FieldPathValue: pfv, 1034 }, nil 1035 } else if rhs.Is != nil { 1036 if rhs.Is.Null { 1037 return &FilterConditionIsNull{rhs.Is.Not, path}, nil 1038 } else if rhs.Is.NaN { 1039 return &FilterConditionIsNaN{rhs.Is.Not, path}, nil 1040 } else { 1041 return nil, status.Error(codes.Internal, "unknown filter IS type - expected NULL or NaN") 1042 } 1043 } else if rhs.Contains != nil { 1044 ct := gotenresource.ConditionContainsTypeFromParser(rhs.Contains) 1045 fp, err := ParseLimit_FieldPath(path.String()) 1046 if err != nil { 1047 return nil, err 1048 } 1049 if rhs.Contains.Value != nil { 1050 pfav, err := ParseLimit_FieldPathArrayItemValue(path.String(), string(valueJSON)) 1051 if err != nil { 1052 return nil, err 1053 } 1054 return &FilterConditionContains{ct, fp, pfav, nil}, nil 1055 } else if rhs.Contains.Any != nil || rhs.Contains.All != nil { 1056 parrv := rhs.Contains.GetArray() 1057 vals := make([]Limit_FieldPathArrayItemValue, len(parrv)) 1058 for i, pv := range parrv { 1059 jsonv, err := utils.JsonMarshal(pv) 1060 if err != nil { 1061 return nil, err 1062 } 1063 if pfav, err := ParseLimit_FieldPathArrayItemValue(path.String(), string(jsonv)); err != nil { 1064 return nil, err 1065 } else { 1066 vals[i] = pfav 1067 } 1068 } 1069 return &FilterConditionContains{ct, fp, nil, vals}, nil 1070 } else { 1071 return nil, status.Error(codes.Internal, "unknown condition contains type") 1072 } 1073 } else if rhs.Like != nil { 1074 return nil, status.Errorf(codes.Unimplemented, "'LIKE' condition is not supported") 1075 } else if rhs.In != nil { 1076 if fpaov, err := ParseLimit_FieldPathArrayOfValues(path.String(), string(valueJSON)); err != nil { 1077 return nil, err 1078 } else { 1079 return &FilterConditionIn{fpaov}, nil 1080 } 1081 } else if rhs.NotIn != nil { 1082 if fpaov, err := ParseLimit_FieldPathArrayOfValues(path.String(), string(valueJSON)); err != nil { 1083 return nil, err 1084 } else { 1085 return &FilterConditionNotIn{fpaov}, nil 1086 } 1087 } 1088 return nil, status.Error(codes.Internal, "unknown filter RHS operand type") 1089 1090 } 1091 1092 func makeFilterConditionFromCondition(condition *filterParser.Condition) (FilterCondition, error) { 1093 if condition.SubExpression != nil { 1094 return makeFilterConditionFromOr(condition.SubExpression.And) 1095 } else if condition.Not != nil { 1096 not, err := makeFilterConditionFromCondition(condition.Not) 1097 if err != nil { 1098 return nil, err 1099 } 1100 return &FilterConditionNot{not}, nil 1101 } else if condition.Operand != nil { 1102 return makeFilterConditionFromOperand(condition.Operand) 1103 } else { 1104 return nil, status.Error(codes.Internal, "unknown condition type") 1105 } 1106 } 1107 1108 func makeFilterConditionFromAnd(conditions []filterParser.Condition) (FilterCondition, error) { 1109 if len(conditions) == 1 { 1110 return makeFilterConditionFromCondition(&conditions[0]) 1111 } else { 1112 cnds := make([]FilterCondition, 0, len(conditions)) 1113 for _, condition := range conditions { 1114 cnd, err := makeFilterConditionFromCondition(&condition) 1115 if err != nil { 1116 return nil, err 1117 } 1118 cnds = append(cnds, cnd) 1119 } 1120 return &FilterConditionComposite{Operator: filterParser.AND, Conditions: cnds}, nil 1121 } 1122 } 1123 1124 func makeFilterConditionFromOr(conditions []filterParser.AndCondition) (FilterCondition, error) { 1125 if len(conditions) == 1 { 1126 return makeFilterConditionFromAnd(conditions[0].Or) 1127 } else { 1128 cnds := make([]FilterCondition, 0, len(conditions)) 1129 for _, condition := range conditions { 1130 cnd, err := makeFilterConditionFromAnd(condition.Or) 1131 if err != nil { 1132 return nil, err 1133 } 1134 cnds = append(cnds, cnd) 1135 } 1136 return &FilterConditionComposite{Operator: filterParser.OR, Conditions: cnds}, nil 1137 } 1138 }