(branch interface{}, path []interface{}, value interface{})
| 200 | } |
| 201 | |
| 202 | func set(branch interface{}, path []interface{}, value interface{}) (interface{}, bool) { |
| 203 | switch branch := branch.(type) { |
| 204 | case TreeBranch: |
| 205 | for i, item := range branch { |
| 206 | if item.Key == path[0] { |
| 207 | var changed bool |
| 208 | if len(path) == 1 { |
| 209 | changed = !equals(branch[i].Value, value) |
| 210 | branch[i].Value = value |
| 211 | } else { |
| 212 | branch[i].Value, changed = set(item.Value, path[1:], value) |
| 213 | } |
| 214 | return branch, changed |
| 215 | } |
| 216 | } |
| 217 | // Not found, need to add the next path entry to the branch |
| 218 | value := valueFromPathAndLeaf(path, value) |
| 219 | if newBranch, ok := value.(TreeBranch); ok && len(newBranch) > 0 { |
| 220 | return append(branch, newBranch[0]), true |
| 221 | } |
| 222 | return branch, true |
| 223 | case []interface{}: |
| 224 | position := path[0].(int) |
| 225 | var changed bool |
| 226 | if len(path) == 1 { |
| 227 | if position >= len(branch) { |
| 228 | return append(branch, value), true |
| 229 | } |
| 230 | changed = !equals(branch[position], value) |
| 231 | branch[position] = value |
| 232 | } else { |
| 233 | if position >= len(branch) { |
| 234 | branch = append(branch, valueFromPathAndLeaf(path[1:], value)) |
| 235 | changed = true |
| 236 | } else { |
| 237 | branch[position], changed = set(branch[position], path[1:], value) |
| 238 | } |
| 239 | } |
| 240 | return branch, changed |
| 241 | default: |
| 242 | newValue := valueFromPathAndLeaf(path, value) |
| 243 | return newValue, !equals(branch, newValue) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Set sets a value on a given tree for the specified path |
| 248 | func (branch TreeBranch) Set(path []interface{}, value interface{}) (TreeBranch, bool) { |
no test coverage detected