Skip to content

Commit

Permalink
merge SetNewvalFieldSeparator and SetSubkeyFieldSeparator into SetFie…
Browse files Browse the repository at this point in the history
…ldSeparator
  • Loading branch information
Charles Banning committed Feb 10, 2017
1 parent 692b8e4 commit 1e68ec6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 48 deletions.
26 changes: 3 additions & 23 deletions keyvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func SetArraySize(size int) int {
// - The subkey can be wildcarded - "key:*" - to require that it's there with some value.
// - If a subkey is preceeded with the '!' character, the key:value[:type] entry is treated as an
// exclusion critera - e.g., "!author:William T. Gaddis".
// - If val contains ":" symbol, use SetSubkeyFieldSeparator to a unused symbol, perhaps "|".
// - If val contains ":" symbol, use SetFieldSeparator to a unused symbol, perhaps "|".
func (mv Map) ValuesForKey(key string, subkeys ...string) ([]interface{}, error) {
m := map[string]interface{}(mv)
var subKeyMap map[string]interface{}
Expand Down Expand Up @@ -152,7 +152,7 @@ func hasKey(iv interface{}, key string, ret *[]interface{}, cnt *int, subkeys ma
// - The subkey can be wildcarded - "key:*" - to require that it's there with some value.
// - If a subkey is preceeded with the '!' character, the key:value[:type] entry is treated as an
// exclusion critera - e.g., "!author:William T. Gaddis".
// - If val contains ":" symbol, use SetSubkeyFieldSeparator to a unused symbol, perhaps "|".
// - If val contains ":" symbol, use SetFieldSeparator to a unused symbol, perhaps "|".
func (mv Map) ValuesForPath(path string, subkeys ...string) ([]interface{}, error) {
// If there are no array indexes in path, use legacy ValuesForPath() logic.
if strings.Index(path, "[") < 0 {
Expand Down Expand Up @@ -492,26 +492,6 @@ func hasSubKeys(v interface{}, subkeys map[string]interface{}) bool {
return false
}

// Per: https://github.com/clbanning/mxj/issues/37#issuecomment-278651862
var subkeySep string = ":"

// SetSubkeyFieldSeparator changes the default field separator, ":", for optional
// subkey arguments in mv.ValuesForKey and mv.ValuesForPath. E.g., if the subkey
// value is "http://blah/blah", setting the field separator to "|" will allow
// the subkey specification, "<key>|http://blah/blah" to parse properly.
// If called with no argument or an empty string value, the field separator is
// set to the default, ":".
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).
Expand All @@ -521,7 +501,7 @@ func getSubKeyMap(kv ...string) (map[string]interface{}, error) {
}
m := make(map[string]interface{}, 0)
for _, v := range kv {
vv := strings.Split(v, subkeySep)
vv := strings.Split(v, fieldSep)
switch len(vv) {
case 2:
m[vv[0]] = interface{}(vv[1])
Expand Down
4 changes: 2 additions & 2 deletions keyvalues2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestSetSubkeyFieldSeparator(t *testing.T) {
t.Fatal(":expecting: value 2; got:", vals[0].(map[string]interface{})["#text"])
}

SetSubkeyFieldSeparator("|")
defer SetSubkeyFieldSeparator()
SetFieldSeparator("|")
defer SetFieldSeparator()
vals, err = m.ValuesForKey("elem", "-attr|2|text")
if err != nil {
t.Fatal(err)
Expand Down
22 changes: 22 additions & 0 deletions setfieldsep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mxj

// Per: https://github.com/clbanning/mxj/issues/37#issuecomment-278651862
var fieldSep string = ":"

// SetFieldSeparator changes the default field separator, ":", for the
// newVal argument in mv.UpdateValuesForPath and the optional 'subkey' arguments
// in mv.ValuesForKey and mv.ValuesForPath.
// E.g., if the newVal // value is "http://blah/blah", setting the field separator
// to "|" will allow the newVal specification, "<key>|http://blah/blah" to parse
// properly. If called with no argument or an empty string value, the field
// separator is // set to the default, ":".
func SetFieldSeparator(s ...string) {
switch {
case len(s) == 0:
fieldSep = ":" // the default
case s[0] == "":
fieldSep = ":" // the default
default:
fieldSep = s[0]
}
}
26 changes: 3 additions & 23 deletions updatevalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
// NOTES:
// 1. Simple elements with attributes need a path terminated as ".#text" to modify the actual value.
// 2. Values in Maps created using NewMapXmlSeq are map[string]interface{} values with a "#text" key.
// 3. If values in 'newVal' or 'subkeys' args contain ":", use SetNewvalFieldSeparator or
// SetSubkeyFieldSeparator to a unused symbol, perhaps "|".
// 3. If values in 'newVal' or 'subkeys' args contain ":", use SetFieldSeparator to an unused symbol,
// perhaps "|".
func (mv Map) UpdateValuesForPath(newVal interface{}, path string, subkeys ...string) (int, error) {
m := map[string]interface{}(mv)

Expand Down Expand Up @@ -58,7 +58,7 @@ func (mv Map) UpdateValuesForPath(newVal interface{}, path string, subkeys ...st
for key, val = range newVal.(map[string]interface{}) {
}
case string: // split it as a key:value pair
ss := strings.Split(newVal.(string), newvalSep)
ss := strings.Split(newVal.(string), fieldSep)
n := len(ss)
if n < 2 || n > 3 {
return 0, fmt.Errorf("unknown newVal spec - %+v", newVal)
Expand Down Expand Up @@ -97,26 +97,6 @@ func (mv Map) UpdateValuesForPath(newVal interface{}, path string, subkeys ...st
return count, nil
}

// Per: https://github.com/clbanning/mxj/issues/37#issuecomment-278651862
var newvalSep string = ":"

// SetNewvalFieldSeparator changes the default field separator, ":", for the
// newVal argument in mv.UpdateValuesForPath. E.g., if the newVal
// value is "http://blah/blah", setting the field separator to "|" will allow
// the newVal specification, "<key>|http://blah/blah" to parse properly.
// If called with no argument or an empty string value, the field separator is
// set to the default, ":".
func SetNewvalFieldSeparator(s ...string) {
switch {
case len(s) == 0:
newvalSep = ":" // the default
case s[0] == "":
newvalSep = ":" // the default
default:
newvalSep = s[0]
}
}

// navigate the path
func updateValuesForKeyPath(key string, value interface{}, m interface{}, keys []string, subkeys map[string]interface{}, cnt *int) {
// ----- at end node: looking at possible node to get 'key' ----
Expand Down

0 comments on commit 1e68ec6

Please sign in to comment.