Skip to content

Commit 74bc3bb

Browse files
committed
apis/nfd: drop custom unmarshaller functions
Not needed in the external API.
1 parent a809292 commit 74bc3bb

File tree

2 files changed

+9
-132
lines changed

2 files changed

+9
-132
lines changed

pkg/apis/nfd/v1alpha1/expression.go

Lines changed: 1 addition & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20-
"encoding/json"
2120
"fmt"
2221
"regexp"
2322
"sort"
2423
"strconv"
25-
"strings"
24+
strings "strings"
2625

2726
"golang.org/x/exp/maps"
2827
"k8s.io/klog/v2"
@@ -42,14 +41,6 @@ var matchOps = map[MatchOp]struct{}{
4241
MatchIsFalse: {},
4342
}
4443

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-
5344
// Validate validates the expression.
5445
func (m *MatchExpression) Validate() error {
5546
if _, ok := matchOps[m.Op]; !ok {
@@ -340,48 +331,6 @@ func (m *MatchExpression) MatchInstanceAttributeNames(instances []InstanceFeatur
340331
return ret, nil
341332
}
342333

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-
385334
// MatchKeys evaluates the MatchExpressionSet against a set of keys.
386335
func (m *MatchExpressionSet) MatchKeys(keys map[string]Nil) (bool, error) {
387336
matched, _, err := m.MatchGetKeys(keys)
@@ -464,83 +413,3 @@ func (m *MatchExpressionSet) MatchGetInstances(instances []InstanceFeature) ([]M
464413
}
465414
return ret, nil
466415
}
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-
}

pkg/apis/nfd/v1alpha1/rule_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ import (
2222
"github.com/stretchr/testify/assert"
2323
)
2424

25+
// newMatchExpression returns a new MatchExpression instance.
26+
func newMatchExpression(op MatchOp, values ...string) *MatchExpression {
27+
return &MatchExpression{
28+
Op: op,
29+
Value: values,
30+
}
31+
}
32+
2533
func TestRule(t *testing.T) {
2634
f := &Features{}
2735
r1 := Rule{Labels: map[string]string{"label-1": "", "label-2": "true"}}

0 commit comments

Comments
 (0)