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