Skip to content

Commit 4d3863a

Browse files
Merge pull request #33 from coreruleset/feat/operator-less-verbose
feat: add compacted format for operator
2 parents 75fc090 + 9393a68 commit 4d3863a

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

types/operator_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package types
33
import (
44
"testing"
55

6+
"github.com/stretchr/testify/require"
67
"go.yaml.in/yaml/v4"
78
)
89

@@ -51,6 +52,50 @@ var (
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

56101
func 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+
}

types/operators.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)