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