File tree Expand file tree Collapse file tree 2 files changed +102
-0
lines changed Expand file tree Collapse file tree 2 files changed +102
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package types
33import (
44 "testing"
55
6+ "github.com/stretchr/testify/require"
67 "go.yaml.in/yaml/v4"
78)
89
5152 {VerifySVNR , "verifySVNR" },
5253 {Within , "within" },
5354 }
55+ operatorUnmarshalTests = []struct {
56+ name string
57+ input string
58+ expected Operator
59+ }{
60+ {
61+ name : "Standard format" ,
62+ input : `name: rx
63+ value: ^.*$` ,
64+ expected : Operator {
65+ Name : Rx ,
66+ Value : "^.*$" ,
67+ },
68+ },
69+ {
70+ name : "Standard format, negated" ,
71+ input : `name: rx
72+ value: ^.*$
73+ negate: true` ,
74+ expected : Operator {
75+ Name : Rx ,
76+ Value : "^.*$" ,
77+ Negate : true ,
78+ },
79+ },
80+ {
81+ name : "Compact format" ,
82+ input : `rx: ^.*$` ,
83+ expected : Operator {
84+ Name : Rx ,
85+ Value : "^.*$" ,
86+ },
87+ },
88+ {
89+ name : "Compact format, negated" ,
90+ input : `rx: ^.*$
91+ negate: true` ,
92+ expected : Operator {
93+ Name : Rx ,
94+ Value : "^.*$" ,
95+ Negate : true ,
96+ },
97+ },
98+ }
5499)
55100
56101func TestOperatorTypeToString (t * testing.T ) {
@@ -87,3 +132,16 @@ func TestMarshalOperatorType(t *testing.T) {
87132 })
88133 }
89134}
135+
136+ func TestUnmarshalOperator (t * testing.T ) {
137+ for _ , tt := range operatorUnmarshalTests {
138+ t .Run (tt .name , func (t * testing.T ) {
139+ var result Operator
140+ err := yaml .Unmarshal ([]byte (tt .input ), & result )
141+ if err != nil {
142+ t .Fatalf ("Failed to unmarshal: %v" , err )
143+ }
144+ require .Equal (t , tt .expected , result )
145+ })
146+ }
147+ }
Original file line number Diff line number Diff line change @@ -270,3 +270,47 @@ func (o *Operator) ToString() string {
270270 return "@" + o .Name .String ()
271271 }
272272}
273+
274+ func (o Operator ) MarshalYAML () (interface {}, error ) {
275+ mapOperator := make (map [interface {}]interface {})
276+ oName , err := o .Name .MarshalYAML ()
277+ if err != nil {
278+ return nil , err
279+ }
280+ mapOperator [oName ] = o .Value
281+ return mapOperator , nil
282+ }
283+
284+ func (o * Operator ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
285+ var rawData map [string ]interface {}
286+ if err := unmarshal (& rawData ); err != nil {
287+ return err
288+ }
289+ for k , v := range rawData {
290+ switch k {
291+ case "negate" :
292+ negate , ok := v .(bool )
293+ if ! ok {
294+ return fmt .Errorf ("negate should be a boolean" )
295+ }
296+ o .Negate = negate
297+ case "name" :
298+ if err := o .SetOperatorName (v .(string )); err != nil {
299+ return err
300+ }
301+ case "value" :
302+ o .SetOperatorValue (v .(string ))
303+ default :
304+ err := o .SetOperatorName (k )
305+ if err != nil {
306+ return err
307+ }
308+ if strVal , ok := v .(string ); ok {
309+ o .SetOperatorValue (strVal )
310+ } else {
311+ return fmt .Errorf ("operator value should be a string" )
312+ }
313+ }
314+ }
315+ return nil
316+ }
You can’t perform that action at this time.
0 commit comments