generated from under-the-hood/go
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
props.go
86 lines (73 loc) · 1.64 KB
/
props.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
package main
import (
"bytes"
yaml1 "github.com/goccy/go-yaml"
yaml2 "gopkg.in/yaml.v3"
)
type PropsVar1 struct {
yaml1.MapSlice
}
func (props *PropsVar1) Get(key string) any {
for _, item := range props.MapSlice {
if item.Key == key {
return item.Value
}
}
return nil
}
func (props *PropsVar1) Set(key string, value any) {
for i, item := range props.MapSlice {
if item.Key == key {
props.MapSlice[i].Value = value
return
}
}
props.MapSlice = append(props.MapSlice, yaml1.MapItem{
Key: key,
Value: value,
})
return
}
func (props *PropsVar1) Delete(key string) {
for i, item := range props.MapSlice {
if item.Key == key {
props.MapSlice = append(props.MapSlice[:i], props.MapSlice[i+1:]...)
return
}
}
return
}
func (props *PropsVar1) UnmarshalYAML(val []byte) error {
dec := yaml1.NewDecoder(bytes.NewBuffer(val))
return dec.Decode(&props.MapSlice)
}
func (props *PropsVar1) MarshalYAML() ([]byte, error) {
buf := bytes.NewBuffer(nil)
enc := yaml1.NewEncoder(buf, yaml1.Indent(2), yaml1.IndentSequence(true))
err := enc.Encode(props.MapSlice)
return buf.Bytes(), err
}
type PropsVar2 struct {
*yaml2.Node
data map[string]any
}
func (props *PropsVar2) Get(key string) any {
if props.data == nil {
return nil
}
return props.data[key]
}
func (props *PropsVar2) Set(key string, value any) {
if props.data == nil {
props.data = make(map[string]any)
}
props.data[key] = value
}
func (props *PropsVar2) UnmarshalYAML(val *yaml2.Node) error {
props.Node = val
return val.Decode(&props.data)
}
func (props *PropsVar2) MarshalYAML() (any, error) {
err := props.Encode(props.data)
return props.Node, err
}