Skip to content

Commit

Permalink
🐛 fix the way lists get applied (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie-haley authored Aug 22, 2022
1 parent ab286fc commit 81b658a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
33 changes: 24 additions & 9 deletions pkg/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,20 @@ func CmdsToData(cmds []string, op string) []api.Cmd {
return res
}

// mapToCmds
// mapToCmds maps an interface to an array of VyOS commands
func mapToCmds(top bool, cmds *[]string, nm interface{}, prefix string) error {
assign := func(cmd string, v interface{}) error {
switch v.(type) {
assign := func(cmd string, v interface{}, array bool) error {
switch v := v.(type) {
case map[string]interface{}, []interface{}:
if err := mapToCmds(false, cmds, v, cmd); err != nil {
return err
}
case string:
*cmds = append(*cmds, cmd)
if array {
*cmds = append(*cmds, cmd+" "+v)
} else {
*cmds = append(*cmds, cmd)
}
default:
*cmds = append(*cmds, cmd+" "+v.(string))
}
Expand All @@ -84,22 +88,33 @@ func mapToCmds(top bool, cmds *[]string, nm interface{}, prefix string) error {
cmd := buildCmd(top, prefix, k)

// this is pretty ugly, basically when building the cmds we only care about the key if the value is {}
res, _ := json.Marshal(v)
if string(res) == "{}" {
if err := assign(cmd, k); err != nil {
r, _ := json.Marshal(v)
res := string(r)

if res == "{}" {
if err := assign(cmd, k, false); err != nil {
return err
}
continue
}
// again, very crude but check if we're looking at an array of string
if strings.HasPrefix(res, "[") && strings.HasSuffix(res, "]") {
for _, val := range v.([]interface{}) {
if err := assign(cmd, val, true); err != nil {
return err
}
}
continue
}

if err := assign(cmd, v); err != nil {
if err := assign(cmd, v, false); err != nil {
return err
}
}
case []interface{}:
for _, v := range nm {
cmd := buildCmd(true, prefix, "")
if err := assign(cmd, v); err != nil {
if err := assign(cmd, v, false); err != nil {
return err
}
}
Expand Down
24 changes: 22 additions & 2 deletions pkg/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ var (
"test firewall ipv6-name WAN_IN rule 20 action",
"test firewall ipv6-name WAN_IN rule 20 protocol",
"test firewall ipv6-name WAN_IN rule 30",
"test service mdns repeater interface eth1.10",
"test service mdns repeater interface eth2.20",
"test service mdns repeater interface eth1.50",
}
)

// YamlToCmds
func Test_YamlToCmds(t *testing.T) {
yaml := `
firewall:
Expand All @@ -36,6 +38,13 @@ firewall:
action: accept
protocol: ipv6-icmp
"30": {}
service:
mdns:
repeater:
interface:
- eth1.10
- eth2.20
- eth1.50
`

res, err := YamlToCmds([]byte(yaml), "test ")
Expand Down Expand Up @@ -67,8 +76,19 @@ func Test_JsonToCmds(t *testing.T) {
}
}
}
},
"service": {
"mdns": {
"repeater": {
"interface": [
"eth1.10",
"eth2.20",
"eth1.50"
]
}
}
}
}
}
`

res, err := JsonToCmds([]byte(json), "test ")
Expand Down

0 comments on commit 81b658a

Please sign in to comment.