-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.go
48 lines (41 loc) · 991 Bytes
/
array.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
package yamlx
import (
"gopkg.in/specgen-io/yaml.v3"
)
type YamlArray struct {
Node yaml.Node
}
func Array(items ...interface{}) *YamlArray {
self := &YamlArray{yaml.Node{Kind: yaml.SequenceNode, Content: []*yaml.Node{}}}
self.Add(items...)
return self
}
func (self *YamlArray) MarshalYAML() (interface{}, error) {
return self.Node, nil
}
func (self *YamlArray) Add(values ...interface{}) error {
for _, value := range values {
valueNode := yaml.Node{}
err := valueNode.Encode(value)
if err != nil {
return err
}
self.Node.Content = append(self.Node.Content, &valueNode)
}
return nil
}
func (self *YamlArray) AddWithComment(value interface{}, comment *string) error {
valueNode := yaml.Node{}
err := valueNode.Encode(value)
if err != nil {
return err
}
if comment != nil {
valueNode.LineComment = *comment
}
self.Node.Content = append(self.Node.Content, &valueNode)
return nil
}
func (self *YamlArray) Length() int {
return len(self.Node.Content)
}