0
0
Fork 0

Refactor AddFieldValue method

Removing one level of nesting makes the method easier to read.
This commit is contained in:
Silvan Jegen 2015-09-21 20:46:33 +02:00
parent 3414701fca
commit 650d48427d
1 changed files with 12 additions and 11 deletions

View File

@ -50,19 +50,20 @@ func (dm *DocumentMatch) AddFieldValue(name string, value interface{}) {
dm.Fields = make(map[string]interface{})
}
existingVal, ok := dm.Fields[name]
if ok {
valSlice, ok := existingVal.([]interface{})
if ok {
// already a slice, append to it
valSlice = append(valSlice, value)
} else {
// create a slice
valSlice = []interface{}{existingVal, value}
}
dm.Fields[name] = valSlice
} else {
if !ok {
dm.Fields[name] = value
return
}
valSlice, ok := existingVal.([]interface{})
if ok {
// already a slice, append to it
valSlice = append(valSlice, value)
} else {
// create a slice
valSlice = []interface{}{existingVal, value}
}
dm.Fields[name] = valSlice
}
type DocumentMatchCollection []*DocumentMatch