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