-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_query.go
147 lines (130 loc) · 3.29 KB
/
yaml_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Package util util consists of general utility functions and structures.
Copyright 2021 Michael Bungenstock
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
package util
import "gopkg.in/yaml.v3"
// YAMLQuery supports the querying and editing of YAML documents without loosing
// the formating
type YAMLQuery struct {
node *yaml.Node
}
// NewYAMLQuery creates an initialized YAMLQuery object
func NewYAMLQuery(node *yaml.Node) *YAMLQuery {
return &YAMLQuery{node: node}
}
// Path queries the passed path. Each path element must match an entry in the
// current map, otherwise an empty object will be returned.
func (q *YAMLQuery) Path(path ...string) *YAMLQuery {
if len(path) == 0 {
return q
}
r := q.First()
if !r.IsMap() {
return &YAMLQuery{}
}
cur := r.node
found := true
for _, p := range path {
if cur.Kind != yaml.MappingNode {
return &YAMLQuery{}
}
found = false
for i := 0; i < len(cur.Content); i = i + 2 {
key := cur.Content[i]
if key.Value == p {
cur = cur.Content[i+1]
found = true
break
}
}
if !found {
break
}
}
if found {
return &YAMLQuery{node: cur}
}
return &YAMLQuery{}
}
// IsNil tests if the current object is nil
func (q *YAMLQuery) IsNil() bool {
return q.node == nil
}
// String returns the current object as string
func (q *YAMLQuery) String() string {
if q.node == nil {
return ""
}
return q.node.Value
}
// Str returns the string of the referenced path
func (q *YAMLQuery) Str(path ...string) string {
return q.Path(path...).String()
}
// Set sets the value of the current object
func (q *YAMLQuery) Set(value string) {
if q.node != nil {
q.node.Value = value
q.node.Tag = "!!str"
}
}
// First skips the document node if necessary and returns the first real object
func (q *YAMLQuery) First() *YAMLQuery {
if q.node != nil && len(q.node.Content) > 0 {
if q.node.Kind == yaml.DocumentNode {
return &YAMLQuery{node: q.node.Content[0]}
}
}
return q
}
// IsMap returns true if this object is a map
func (q *YAMLQuery) IsMap() bool {
return q.node != nil && q.node.Kind == yaml.MappingNode
}
// IsDocument returns true if this object is a document
func (q *YAMLQuery) IsDocument() bool {
return q.node != nil && q.node.Kind == yaml.DocumentNode
}
// SetMap sets the key-value pair
func (q *YAMLQuery) SetMap(key, value string) {
r := q.First()
if r.IsMap() {
c := r.node.Content
l := len(c)
for i := 0; i < l; i = i + 2 {
k := c[i]
if k.Value == key {
c[i+1].Value = value
return
}
}
// append new entry
//row := q.LastLine() + 1
node := &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!str",
//Column: 1,
//Line: row,
Value: key,
}
c = append(c, node)
node = &yaml.Node{
Kind: yaml.ScalarNode,
Tag: "!!str",
//Column: 1,
//Line: row + 1,
Value: value,
}
c = append(c, node)
r.node.Content = c
}
}