|
| 1 | +// Copyright © 2022 Kaleido, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package migrate |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | +) |
| 23 | + |
| 24 | +type ConfigItem struct { |
| 25 | + value interface{} |
| 26 | + parent *ConfigItem |
| 27 | + name string |
| 28 | + writer io.Writer |
| 29 | +} |
| 30 | + |
| 31 | +type ConfigItemIterator struct { |
| 32 | + items []*ConfigItem |
| 33 | +} |
| 34 | + |
| 35 | +func NewConfigItem(value interface{}, writer io.Writer) *ConfigItem { |
| 36 | + return &ConfigItem{value: value, writer: writer} |
| 37 | +} |
| 38 | + |
| 39 | +func (c *ConfigItem) hasChild(name string) (exists bool) { |
| 40 | + if v, ok := c.value.(map[interface{}]interface{}); ok { |
| 41 | + _, exists = v[name] |
| 42 | + } |
| 43 | + return exists |
| 44 | +} |
| 45 | + |
| 46 | +func (c *ConfigItem) deleteChild(name string) { |
| 47 | + if v, ok := c.value.(map[interface{}]interface{}); ok { |
| 48 | + delete(v, name) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func (c *ConfigItem) setChild(name string, value interface{}) { |
| 53 | + if v, ok := c.value.(map[interface{}]interface{}); ok { |
| 54 | + v[name] = value |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (c *ConfigItem) Get(name string) *ConfigItem { |
| 59 | + if v, ok := c.value.(map[interface{}]interface{}); ok { |
| 60 | + if child, ok := v[name]; ok { |
| 61 | + return &ConfigItem{value: child, parent: c, name: name, writer: c.writer} |
| 62 | + } |
| 63 | + } |
| 64 | + return &ConfigItem{value: nil, parent: c, name: name, writer: c.writer} |
| 65 | +} |
| 66 | + |
| 67 | +func (c *ConfigItemIterator) Get(name string) *ConfigItemIterator { |
| 68 | + items := make([]*ConfigItem, len(c.items)) |
| 69 | + for i, item := range c.items { |
| 70 | + items[i] = item.Get(name) |
| 71 | + } |
| 72 | + return &ConfigItemIterator{items: items} |
| 73 | +} |
| 74 | + |
| 75 | +func (c *ConfigItem) Path() string { |
| 76 | + if c.parent == nil || c.parent.name == "" { |
| 77 | + return c.name |
| 78 | + } |
| 79 | + return c.parent.Path() + "." + c.name |
| 80 | +} |
| 81 | + |
| 82 | +func (c *ConfigItem) Exists() bool { |
| 83 | + return c.value != nil |
| 84 | +} |
| 85 | + |
| 86 | +func (c *ConfigItem) Length() int { |
| 87 | + if v, ok := c.value.([]interface{}); ok { |
| 88 | + return len(v) |
| 89 | + } |
| 90 | + return 0 |
| 91 | +} |
| 92 | + |
| 93 | +func (c *ConfigItem) Each() *ConfigItemIterator { |
| 94 | + list, ok := c.value.([]interface{}) |
| 95 | + if !ok { |
| 96 | + return &ConfigItemIterator{items: make([]*ConfigItem, 0)} |
| 97 | + } |
| 98 | + items := make([]*ConfigItem, len(list)) |
| 99 | + for i, val := range list { |
| 100 | + items[i] = &ConfigItem{ |
| 101 | + value: val, |
| 102 | + parent: c.parent, |
| 103 | + name: c.name, |
| 104 | + writer: c.writer, |
| 105 | + } |
| 106 | + } |
| 107 | + return &ConfigItemIterator{items: items} |
| 108 | +} |
| 109 | + |
| 110 | +func (c *ConfigItemIterator) Run(fn func(item *ConfigItem)) *ConfigItemIterator { |
| 111 | + for _, item := range c.items { |
| 112 | + fn(item) |
| 113 | + } |
| 114 | + return c |
| 115 | +} |
| 116 | + |
| 117 | +func (c *ConfigItem) Create() *ConfigItem { |
| 118 | + if !c.Exists() { |
| 119 | + if c.parent != nil { |
| 120 | + c.parent.Create() |
| 121 | + } |
| 122 | + c.value = make(map[interface{}]interface{}) |
| 123 | + c.parent.setChild(c.name, c.value) |
| 124 | + } |
| 125 | + return c |
| 126 | +} |
| 127 | + |
| 128 | +func (c *ConfigItem) Set(value interface{}) *ConfigItem { |
| 129 | + fmt.Fprintf(c.writer, "Create: %s: %s\n", c.Path(), value) |
| 130 | + c.value = value |
| 131 | + c.parent.Create() |
| 132 | + c.parent.setChild(c.name, c.value) |
| 133 | + return c |
| 134 | +} |
| 135 | + |
| 136 | +func (c *ConfigItemIterator) Set(value interface{}) *ConfigItemIterator { |
| 137 | + for _, item := range c.items { |
| 138 | + item.Set(value) |
| 139 | + } |
| 140 | + return c |
| 141 | +} |
| 142 | + |
| 143 | +func (c *ConfigItem) SetIfEmpty(value interface{}) *ConfigItem { |
| 144 | + if !c.Exists() { |
| 145 | + c.Set(value) |
| 146 | + } |
| 147 | + return c |
| 148 | +} |
| 149 | + |
| 150 | +func (c *ConfigItem) Delete() *ConfigItem { |
| 151 | + if c.Exists() { |
| 152 | + fmt.Fprintf(c.writer, "Delete: %s\n", c.Path()) |
| 153 | + c.parent.deleteChild(c.name) |
| 154 | + } |
| 155 | + return c |
| 156 | +} |
| 157 | + |
| 158 | +func (c *ConfigItemIterator) Delete() *ConfigItemIterator { |
| 159 | + for _, item := range c.items { |
| 160 | + item.Delete() |
| 161 | + } |
| 162 | + return c |
| 163 | +} |
| 164 | + |
| 165 | +func (c *ConfigItem) RenameTo(name string) *ConfigItem { |
| 166 | + if c.Exists() { |
| 167 | + if c.parent.hasChild(name) { |
| 168 | + // Don't overwrite if the new key already exists |
| 169 | + c.Delete() |
| 170 | + } else { |
| 171 | + fmt.Fprintf(c.writer, "Rename: %s -> .%s\n", c.Path(), name) |
| 172 | + c.parent.deleteChild(c.name) |
| 173 | + c.parent.setChild(name, c.value) |
| 174 | + c.name = name |
| 175 | + } |
| 176 | + } |
| 177 | + return c |
| 178 | +} |
| 179 | + |
| 180 | +func (c *ConfigItemIterator) RenameTo(name string) *ConfigItemIterator { |
| 181 | + for _, item := range c.items { |
| 182 | + item.RenameTo(name) |
| 183 | + } |
| 184 | + return c |
| 185 | +} |
| 186 | + |
| 187 | +func (c *ConfigItem) ReplaceValue(old interface{}, new interface{}) *ConfigItem { |
| 188 | + if c.value == old { |
| 189 | + fmt.Fprintf(c.writer, "Change: %s: %s -> %s\n", c.Path(), old, new) |
| 190 | + c.value = new |
| 191 | + c.parent.setChild(c.name, new) |
| 192 | + } |
| 193 | + return c |
| 194 | +} |
| 195 | + |
| 196 | +func (c *ConfigItemIterator) ReplaceValue(old interface{}, new interface{}) *ConfigItemIterator { |
| 197 | + for _, item := range c.items { |
| 198 | + item.ReplaceValue(old, new) |
| 199 | + } |
| 200 | + return c |
| 201 | +} |
0 commit comments