diff --git a/pkg/convert/convert.go b/pkg/convert/convert.go index 6ae38e4..df29dff 100644 --- a/pkg/convert/convert.go +++ b/pkg/convert/convert.go @@ -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)) } @@ -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 } } diff --git a/pkg/convert/convert_test.go b/pkg/convert/convert_test.go index 082b9a4..a3f53d2 100644 --- a/pkg/convert/convert_test.go +++ b/pkg/convert/convert_test.go @@ -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: @@ -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 ") @@ -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 ")