-
Notifications
You must be signed in to change notification settings - Fork 158
/
yaml.go
80 lines (75 loc) · 2.09 KB
/
yaml.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
package config
import (
"github.com/goccy/go-yaml"
"github.com/k1LoW/tbls/schema"
)
func (d DSN) MarshalYAML() ([]byte, error) {
if len(d.Headers) == 0 {
dsn := d.URL
return yaml.Marshal(dsn)
}
return yaml.Marshal(d)
}
func (d *DSN) UnmarshalYAML(data []byte) error {
var dsn interface{}
if err := yaml.Unmarshal(data, &dsn); err != nil {
return err
}
switch raw := dsn.(type) {
case string:
d.URL = raw
case interface{}:
if err := yaml.Unmarshal(data, d); err != nil {
return err
}
}
return nil
}
func (f Format) MarshalYAML() ([]byte, error) {
if len(f.HideColumnsWithoutValues) == 0 {
s := struct {
Adjust bool `yaml:"adjust,omitempty"`
Sort bool `yaml:"sort,omitempty"`
Number bool `yaml:"number,omitempty"`
ShowOnlyFirstParagraph bool `yaml:"showOnlyFirstParagraph,omitempty"`
HideColumnsWithoutValues bool `yaml:"hideColumnsWithoutValues,omitempty"`
}{
Adjust: f.Adjust,
Sort: f.Sort,
Number: f.Number,
ShowOnlyFirstParagraph: f.ShowOnlyFirstParagraph,
HideColumnsWithoutValues: false,
}
return yaml.Marshal(s)
}
return yaml.Marshal(f)
}
func (f *Format) UnmarshalYAML(data []byte) error {
s := struct {
Adjust bool `yaml:"adjust,omitempty"`
Sort bool `yaml:"sort,omitempty"`
Number bool `yaml:"number,omitempty"`
ShowOnlyFirstParagraph bool `yaml:"showOnlyFirstParagraph,omitempty"`
HideColumnsWithoutValues interface{} `yaml:"hideColumnsWithoutValues,omitempty"`
}{}
if err := yaml.Unmarshal(data, &s); err != nil {
return err
}
f.Adjust = s.Adjust
f.Sort = s.Sort
f.Number = s.Number
f.ShowOnlyFirstParagraph = s.ShowOnlyFirstParagraph
switch v := s.HideColumnsWithoutValues.(type) {
case bool:
if v {
f.HideColumnsWithoutValues = schema.HideableColumns
}
case []interface{}:
values := []string{}
for _, vv := range v {
values = append(values, vv.(string))
}
f.HideColumnsWithoutValues = values
}
return nil
}