| 1 |
// Copyright (c) 2022 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package configupgrade |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"os" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"gopkg.in/yaml.v3" |
| 15 |
) |
| 16 |
|
| 17 |
type YAMLMap map[string]YAMLNode |
| 18 |
type YAMLList []YAMLNode |
| 19 |
|
| 20 |
type YAMLNode struct { |
| 21 |
*yaml.Node |
| 22 |
Map YAMLMap |
| 23 |
List YAMLList |
| 24 |
Key *yaml.Node |
| 25 |
} |
| 26 |
|
| 27 |
type YAMLType uint32 |
| 28 |
|
| 29 |
const ( |
| 30 |
Null YAMLType = 1 << iota |
| 31 |
Bool |
| 32 |
Str |
| 33 |
Int |
| 34 |
Float |
| 35 |
Timestamp |
| 36 |
List |
| 37 |
Map |
| 38 |
Binary |
| 39 |
) |
| 40 |
|
| 41 |
func (t YAMLType) String() string { |
| 42 |
switch t { |
| 43 |
case Null: |
| 44 |
return NullTag |
| 45 |
case Bool: |
| 46 |
return BoolTag |
| 47 |
case Str: |
| 48 |
return StrTag |
| 49 |
case Int: |
| 50 |
return IntTag |
| 51 |
case Float: |
| 52 |
return FloatTag |
| 53 |
case Timestamp: |
| 54 |
return TimestampTag |
| 55 |
case List: |
| 56 |
return SeqTag |
| 57 |
case Map: |
| 58 |
return MapTag |
| 59 |
case Binary: |
| 60 |
return BinaryTag |
| 61 |
default: |
| 62 |
panic(fmt.Errorf("can't convert type %d to string", t)) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func tagToType(tag string) YAMLType { |
| 67 |
switch tag { |
| 68 |
case NullTag: |
| 69 |
return Null |
| 70 |
case BoolTag: |
| 71 |
return Bool |
| 72 |
case StrTag: |
| 73 |
return Str |
| 74 |
case IntTag: |
| 75 |
return Int |
| 76 |
case FloatTag: |
| 77 |
return Float |
| 78 |
case TimestampTag: |
| 79 |
return Timestamp |
| 80 |
case SeqTag: |
| 81 |
return List |
| 82 |
case MapTag: |
| 83 |
return Map |
| 84 |
case BinaryTag: |
| 85 |
return Binary |
| 86 |
default: |
| 87 |
return 0 |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
const ( |
| 92 |
NullTag = "!!null" |
| 93 |
BoolTag = "!!bool" |
| 94 |
StrTag = "!!str" |
| 95 |
IntTag = "!!int" |
| 96 |
FloatTag = "!!float" |
| 97 |
TimestampTag = "!!timestamp" |
| 98 |
SeqTag = "!!seq" |
| 99 |
MapTag = "!!map" |
| 100 |
BinaryTag = "!!binary" |
| 101 |
) |
| 102 |
|
| 103 |
func fromNode(node, key *yaml.Node) YAMLNode { |
| 104 |
switch node.Kind { |
| 105 |
case yaml.DocumentNode: |
| 106 |
return fromNode(node.Content[0], nil) |
| 107 |
case yaml.AliasNode: |
| 108 |
return fromNode(node.Alias, nil) |
| 109 |
case yaml.MappingNode: |
| 110 |
return YAMLNode{ |
| 111 |
Node: node, |
| 112 |
Map: parseYAMLMap(node), |
| 113 |
Key: key, |
| 114 |
} |
| 115 |
case yaml.SequenceNode: |
| 116 |
return YAMLNode{ |
| 117 |
Node: node, |
| 118 |
List: parseYAMLList(node), |
| 119 |
} |
| 120 |
default: |
| 121 |
return YAMLNode{Node: node, Key: key} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func (yn *YAMLNode) toNode() *yaml.Node { |
| 126 |
yn.UpdateContent() |
| 127 |
return yn.Node |
| 128 |
} |
| 129 |
|
| 130 |
func (yn *YAMLNode) UpdateContent() { |
| 131 |
switch { |
| 132 |
case yn.Map != nil && yn.Node.Kind == yaml.MappingNode: |
| 133 |
yn.Content = yn.Map.toNodes() |
| 134 |
case yn.List != nil && yn.Node.Kind == yaml.SequenceNode: |
| 135 |
yn.Content = yn.List.toNodes() |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
func parseYAMLList(node *yaml.Node) YAMLList { |
| 140 |
data := make(YAMLList, len(node.Content)) |
| 141 |
for i, item := range node.Content { |
| 142 |
data[i] = fromNode(item, nil) |
| 143 |
} |
| 144 |
return data |
| 145 |
} |
| 146 |
|
| 147 |
func (yl YAMLList) toNodes() []*yaml.Node { |
| 148 |
nodes := make([]*yaml.Node, len(yl)) |
| 149 |
for i, item := range yl { |
| 150 |
nodes[i] = item.toNode() |
| 151 |
} |
| 152 |
return nodes |
| 153 |
} |
| 154 |
|
| 155 |
func parseYAMLMap(node *yaml.Node) YAMLMap { |
| 156 |
if len(node.Content)%2 != 0 { |
| 157 |
panic(fmt.Errorf("uneven number of items in YAML map (%d)", len(node.Content))) |
| 158 |
} |
| 159 |
data := make(YAMLMap, len(node.Content)/2) |
| 160 |
for i := 0; i < len(node.Content); i += 2 { |
| 161 |
key := node.Content[i] |
| 162 |
value := node.Content[i+1] |
| 163 |
if key.Kind == yaml.ScalarNode { |
| 164 |
data[key.Value] = fromNode(value, key) |
| 165 |
} |
| 166 |
} |
| 167 |
return data |
| 168 |
} |
| 169 |
|
| 170 |
func (ym YAMLMap) toNodes() []*yaml.Node { |
| 171 |
nodes := make([]*yaml.Node, len(ym)*2) |
| 172 |
i := 0 |
| 173 |
for key, value := range ym { |
| 174 |
nodes[i] = makeStringNode(key) |
| 175 |
nodes[i+1] = value.toNode() |
| 176 |
i += 2 |
| 177 |
} |
| 178 |
return nodes |
| 179 |
} |
| 180 |
|
| 181 |
func makeStringNode(val string) *yaml.Node { |
| 182 |
var node yaml.Node |
| 183 |
node.SetString(val) |
| 184 |
return &node |
| 185 |
} |
| 186 |
|
| 187 |
func StringNode(val string) YAMLNode { |
| 188 |
return YAMLNode{Node: makeStringNode(val)} |
| 189 |
} |
| 190 |
|
| 191 |
type Helper interface { |
| 192 |
Copy(allowedTypes YAMLType, path ...string) |
| 193 |
Get(tag YAMLType, path ...string) (string, bool) |
| 194 |
GetNode(path ...string) *YAMLNode |
| 195 |
GetBase(path ...string) string |
| 196 |
GetBaseNode(path ...string) *YAMLNode |
| 197 |
Set(tag YAMLType, value string, path ...string) |
| 198 |
SetMap(value YAMLMap, path ...string) |
| 199 |
AddSpaceBeforeComment(path ...string) |
| 200 |
} |
| 201 |
|
| 202 |
type CopyHelper struct { |
| 203 |
Base YAMLNode |
| 204 |
Config YAMLNode |
| 205 |
} |
| 206 |
|
| 207 |
var _ Helper = (*CopyHelper)(nil) |
| 208 |
|
| 209 |
func NewHelper(base, cfg *yaml.Node) *CopyHelper { |
| 210 |
return &CopyHelper{ |
| 211 |
Base: fromNode(base, nil), |
| 212 |
Config: fromNode(cfg, nil), |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
func (helper *CopyHelper) AddSpaceBeforeComment(path ...string) { |
| 217 |
node := helper.GetBaseNode(path...) |
| 218 |
if node == nil || node.Key == nil { |
| 219 |
panic(fmt.Errorf("didn't find key at %+v", path)) |
| 220 |
} |
| 221 |
node.Key.HeadComment = "\n" + node.Key.HeadComment |
| 222 |
} |
| 223 |
|
| 224 |
func (helper *CopyHelper) Copy(allowedTypes YAMLType, path ...string) { |
| 225 |
base, cfg := helper.Base, helper.Config |
| 226 |
var ok bool |
| 227 |
for _, item := range path { |
| 228 |
cfg, ok = cfg.Map[item] |
| 229 |
if !ok { |
| 230 |
return |
| 231 |
} |
| 232 |
base, ok = base.Map[item] |
| 233 |
if !ok { |
| 234 |
_, _ = fmt.Fprintf(os.Stderr, "Ignoring config field %s which is missing in base config\n", strings.Join(path, "->")) |
| 235 |
return |
| 236 |
} |
| 237 |
} |
| 238 |
if allowedTypes&tagToType(cfg.Tag) == 0 { |
| 239 |
_, _ = fmt.Fprintf(os.Stderr, "Ignoring incorrect config field type %s at %s\n", cfg.Tag, strings.Join(path, "->")) |
| 240 |
return |
| 241 |
} |
| 242 |
base.Tag = cfg.Tag |
| 243 |
base.Style = cfg.Style |
| 244 |
switch base.Kind { |
| 245 |
case yaml.ScalarNode: |
| 246 |
base.Value = cfg.Value |
| 247 |
case yaml.SequenceNode, yaml.MappingNode: |
| 248 |
base.Content = cfg.Content |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
func getNode(cfg YAMLNode, path []string) *YAMLNode { |
| 253 |
var ok bool |
| 254 |
for _, item := range path { |
| 255 |
cfg, ok = cfg.Map[item] |
| 256 |
if !ok { |
| 257 |
return nil |
| 258 |
} |
| 259 |
} |
| 260 |
return &cfg |
| 261 |
} |
| 262 |
|
| 263 |
func (helper *CopyHelper) GetNode(path ...string) *YAMLNode { |
| 264 |
return getNode(helper.Config, path) |
| 265 |
} |
| 266 |
|
| 267 |
func (helper *CopyHelper) GetBaseNode(path ...string) *YAMLNode { |
| 268 |
return getNode(helper.Base, path) |
| 269 |
} |
| 270 |
|
| 271 |
func (helper *CopyHelper) Get(tag YAMLType, path ...string) (string, bool) { |
| 272 |
node := helper.GetNode(path...) |
| 273 |
if node == nil || node.Kind != yaml.ScalarNode || tag&tagToType(node.Tag) == 0 { |
| 274 |
return "", false |
| 275 |
} |
| 276 |
return node.Value, true |
| 277 |
} |
| 278 |
|
| 279 |
func (helper *CopyHelper) GetBase(path ...string) string { |
| 280 |
return helper.GetBaseNode(path...).Value |
| 281 |
} |
| 282 |
|
| 283 |
func (helper *CopyHelper) Set(tag YAMLType, value string, path ...string) { |
| 284 |
base := helper.Base |
| 285 |
for _, item := range path { |
| 286 |
base = base.Map[item] |
| 287 |
} |
| 288 |
base.Tag = tag.String() |
| 289 |
base.Value = value |
| 290 |
} |
| 291 |
|
| 292 |
func (helper *CopyHelper) SetMap(value YAMLMap, path ...string) { |
| 293 |
base := helper.Base |
| 294 |
for _, item := range path { |
| 295 |
base = base.Map[item] |
| 296 |
} |
| 297 |
if base.Tag != MapTag || base.Kind != yaml.MappingNode { |
| 298 |
panic(fmt.Errorf("invalid target for SetMap(%+v): tag:%s, kind:%d", path, base.Tag, base.Kind)) |
| 299 |
} |
| 300 |
base.Content = value.toNodes() |
| 301 |
} |