SetPath modifies `Json`, recursively checking/creating map keys for the supplied path, and then finally writing in the value
(branch []string, val interface{})
| 66 | // SetPath modifies `Json`, recursively checking/creating map keys for the supplied path, |
| 67 | // and then finally writing in the value |
| 68 | func (j *Json) SetPath(branch []string, val interface{}) { |
| 69 | if len(branch) == 0 { |
| 70 | j.data = val |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | // in order to insert our branch, we need map[string]interface{} |
| 75 | if _, ok := (j.data).(map[string]interface{}); !ok { |
| 76 | // have to replace with something suitable |
| 77 | j.data = make(map[string]interface{}) |
| 78 | } |
| 79 | curr := j.data.(map[string]interface{}) |
| 80 | |
| 81 | for i := 0; i < len(branch)-1; i++ { |
| 82 | b := branch[i] |
| 83 | // key exists? |
| 84 | if _, ok := curr[b]; !ok { |
| 85 | n := make(map[string]interface{}) |
| 86 | curr[b] = n |
| 87 | curr = n |
| 88 | continue |
| 89 | } |
| 90 | |
| 91 | // make sure the value is the right sort of thing |
| 92 | if _, ok := curr[b].(map[string]interface{}); !ok { |
| 93 | // have to replace with something suitable |
| 94 | n := make(map[string]interface{}) |
| 95 | curr[b] = n |
| 96 | } |
| 97 | |
| 98 | curr = curr[b].(map[string]interface{}) |
| 99 | } |
| 100 | |
| 101 | // add remaining k/v |
| 102 | curr[branch[len(branch)-1]] = val |
| 103 | } |
| 104 | |
| 105 | // Del modifies `Json` map by deleting `key` if it is present. |
| 106 | func (j *Json) Del(key string) { |
no outgoing calls