@@ -17,12 +17,11 @@ limitations under the License.
17
17
package v1alpha1
18
18
19
19
import (
20
- "encoding/json"
21
20
"fmt"
22
21
"regexp"
23
22
"sort"
24
23
"strconv"
25
- "strings"
24
+ strings "strings"
26
25
27
26
"golang.org/x/exp/maps"
28
27
"k8s.io/klog/v2"
@@ -42,14 +41,6 @@ var matchOps = map[MatchOp]struct{}{
42
41
MatchIsFalse : {},
43
42
}
44
43
45
- // newMatchExpression returns a new MatchExpression instance.
46
- func newMatchExpression (op MatchOp , values ... string ) * MatchExpression {
47
- return & MatchExpression {
48
- Op : op ,
49
- Value : values ,
50
- }
51
- }
52
-
53
44
// Validate validates the expression.
54
45
func (m * MatchExpression ) Validate () error {
55
46
if _ , ok := matchOps [m .Op ]; ! ok {
@@ -340,48 +331,6 @@ func (m *MatchExpression) MatchInstanceAttributeNames(instances []InstanceFeatur
340
331
return ret , nil
341
332
}
342
333
343
- // matchExpression is a helper type for unmarshalling MatchExpression
344
- type matchExpression MatchExpression
345
-
346
- // UnmarshalJSON implements the Unmarshaler interface of "encoding/json"
347
- func (m * MatchExpression ) UnmarshalJSON (data []byte ) error {
348
- raw := new (interface {})
349
-
350
- err := json .Unmarshal (data , raw )
351
- if err != nil {
352
- return err
353
- }
354
-
355
- switch v := (* raw ).(type ) {
356
- case string :
357
- * m = * newMatchExpression (MatchIn , v )
358
- case bool :
359
- * m = * newMatchExpression (MatchIn , strconv .FormatBool (v ))
360
- case float64 :
361
- * m = * newMatchExpression (MatchIn , strconv .FormatFloat (v , 'f' , - 1 , 64 ))
362
- case []interface {}:
363
- values := make ([]string , len (v ))
364
- for i , value := range v {
365
- str , ok := value .(string )
366
- if ! ok {
367
- return fmt .Errorf ("invalid value %v in %v" , value , v )
368
- }
369
- values [i ] = str
370
- }
371
- * m = * newMatchExpression (MatchIn , values ... )
372
- case map [string ]interface {}:
373
- helper := & matchExpression {}
374
- if err := json .Unmarshal (data , & helper ); err != nil {
375
- return err
376
- }
377
- * m = * newMatchExpression (helper .Op , helper .Value ... )
378
- default :
379
- return fmt .Errorf ("invalid rule '%v' (%T)" , v , v )
380
- }
381
-
382
- return m .Validate ()
383
- }
384
-
385
334
// MatchKeys evaluates the MatchExpressionSet against a set of keys.
386
335
func (m * MatchExpressionSet ) MatchKeys (keys map [string ]Nil ) (bool , error ) {
387
336
matched , _ , err := m .MatchGetKeys (keys )
@@ -464,83 +413,3 @@ func (m *MatchExpressionSet) MatchGetInstances(instances []InstanceFeature) ([]M
464
413
}
465
414
return ret , nil
466
415
}
467
-
468
- // UnmarshalJSON implements the Unmarshaler interface of "encoding/json".
469
- func (m * MatchExpressionSet ) UnmarshalJSON (data []byte ) error {
470
- * m = MatchExpressionSet {}
471
-
472
- names := make ([]string , 0 )
473
- if err := json .Unmarshal (data , & names ); err == nil {
474
- // Simplified slice form
475
- for _ , name := range names {
476
- split := strings .SplitN (name , "=" , 2 )
477
- if len (split ) == 1 {
478
- (* m )[split [0 ]] = newMatchExpression (MatchExists )
479
- } else {
480
- (* m )[split [0 ]] = newMatchExpression (MatchIn , split [1 ])
481
- }
482
- }
483
- } else {
484
- // Unmarshal the full map form
485
- expressions := make (map [string ]* MatchExpression )
486
- if err := json .Unmarshal (data , & expressions ); err != nil {
487
- return err
488
- }
489
- for k , v := range expressions {
490
- if v != nil {
491
- (* m )[k ] = v
492
- } else {
493
- (* m )[k ] = newMatchExpression (MatchExists )
494
- }
495
- }
496
- }
497
-
498
- return nil
499
- }
500
-
501
- // UnmarshalJSON implements the Unmarshaler interface of "encoding/json".
502
- func (m * MatchOp ) UnmarshalJSON (data []byte ) error {
503
- var raw string
504
-
505
- if err := json .Unmarshal (data , & raw ); err != nil {
506
- return err
507
- }
508
-
509
- if _ , ok := matchOps [MatchOp (raw )]; ! ok {
510
- return fmt .Errorf ("invalid Op %q" , raw )
511
- }
512
- * m = MatchOp (raw )
513
- return nil
514
- }
515
-
516
- // UnmarshalJSON implements the Unmarshaler interface of "encoding/json".
517
- func (m * MatchValue ) UnmarshalJSON (data []byte ) error {
518
- var raw interface {}
519
-
520
- if err := json .Unmarshal (data , & raw ); err != nil {
521
- return err
522
- }
523
-
524
- switch v := raw .(type ) {
525
- case string :
526
- * m = []string {v }
527
- case bool :
528
- * m = []string {strconv .FormatBool (v )}
529
- case float64 :
530
- * m = []string {strconv .FormatFloat (v , 'f' , - 1 , 64 )}
531
- case []interface {}:
532
- values := make ([]string , len (v ))
533
- for i , value := range v {
534
- str , ok := value .(string )
535
- if ! ok {
536
- return fmt .Errorf ("invalid value %v in %v" , value , v )
537
- }
538
- values [i ] = str
539
- }
540
- * m = values
541
- default :
542
- return fmt .Errorf ("invalid values '%v' (%T)" , v , v )
543
- }
544
-
545
- return nil
546
- }
0 commit comments