diff --git a/keyvalues.go b/keyvalues.go index 7feb766..e289e6e 100644 --- a/keyvalues.go +++ b/keyvalues.go @@ -490,6 +490,20 @@ func hasSubKeys(v interface{}, subkeys map[string]interface{}) bool { return false } +// Per: https://github.com/clbanning/mxj/issues/37#issuecomment-278651862 +var subkeySep string = ":" + +func SetSubkeyFieldSeparator(s ...string) { + switch { + case len(s) == 0: + subkeySep = ":" // the default + case s[0] == "": + subkeySep = ":" // the default + default: + subkeySep = s[0] + } +} + // Generate map of key:value entries as map[string]string. // 'kv' arguments are "name:value" pairs: attribute keys are designated with prepended hyphen, '-'. // If len(kv) == 0, the return is (nil, nil). @@ -499,12 +513,12 @@ func getSubKeyMap(kv ...string) (map[string]interface{}, error) { } m := make(map[string]interface{}, 0) for _, v := range kv { - vv := strings.Split(v, ":") + vv := strings.Split(v, subkeySep) switch len(vv) { case 2: m[vv[0]] = interface{}(vv[1]) case 3: - switch vv[3] { + switch vv[2] { case "string", "char", "text": m[vv[0]] = interface{}(vv[1]) case "bool", "boolean": diff --git a/keyvalues2_test.go b/keyvalues2_test.go new file mode 100644 index 0000000..fb6c632 --- /dev/null +++ b/keyvalues2_test.go @@ -0,0 +1,47 @@ +package mxj + +import ( + "fmt" + "testing" +) + +func TestSetSubkeyFieldSeparator(t *testing.T) { + PrependAttrWithHyphen(true) + + fmt.Println("----------- TestSetSubkeyFieldSeparator") + data := ` + + value 1 + value 2 + value 3 + ` + + m, err := NewMapXml([]byte(data)) + if err != nil { + t.Fatal(err) + } + vals, err := m.ValuesForKey("elem", "-attr:2:text") + if err != nil { + t.Fatal(err) + } + if len(vals) != 1 { + t.Fatal(":len(vals);", len(vals), vals) + } + if vals[0].(map[string]interface{})["#text"].(string) != "value 2" { + t.Fatal(":expecting: value 2; got:", vals[0].(map[string]interface{})["#text"]) + } + + SetSubkeyFieldSeparator("|") + defer SetSubkeyFieldSeparator() + vals, err = m.ValuesForKey("elem", "-attr|2|text") + if err != nil { + t.Fatal(err) + } + if len(vals) != 1 { + t.Fatal("|len(vals);", len(vals), vals) + } + if vals[0].(map[string]interface{})["#text"].(string) != "value 2" { + t.Fatal("|expecting: value 2; got:", vals[0].(map[string]interface{})["#text"]) + } +} +