forked from gookit/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
186 lines (159 loc) · 4.18 KB
/
export.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package config
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"github.com/gookit/goutil/structs"
"github.com/mitchellh/mapstructure"
)
// Decode all config data to the dst ptr
//
// Usage:
//
// myConf := &MyConf{}
// config.Decode(myConf)
func Decode(dst any) error { return dc.Decode(dst) }
// Decode all config data to the dst ptr.
//
// It's equals:
//
// c.Structure("", dst)
func (c *Config) Decode(dst any) error {
return c.Structure("", dst)
}
// MapStruct alias method of the 'Structure'
//
// Usage:
//
// dbInfo := &Db{}
// config.MapStruct("db", dbInfo)
func MapStruct(key string, dst any) error { return dc.MapStruct(key, dst) }
// MapStruct alias method of the 'Structure'
func (c *Config) MapStruct(key string, dst any) error {
return c.Structure(key, dst)
}
// BindStruct alias method of the 'Structure'
func BindStruct(key string, dst any) error { return dc.BindStruct(key, dst) }
// BindStruct alias method of the 'Structure'
func (c *Config) BindStruct(key string, dst any) error {
return c.Structure(key, dst)
}
// MapOnExists mapping data to the dst structure only on key exists.
func MapOnExists(key string, dst any) error {
return dc.MapOnExists(key, dst)
}
// MapOnExists mapping data to the dst structure only on key exists.
//
// - Support ParseEnv on mapping
// - Support ParseDefault on mapping
func (c *Config) MapOnExists(key string, dst any) error {
err := c.Structure(key, dst)
if err != nil && err == ErrNotFound {
return nil
}
return err
}
// Structure get config data and binding to the dst structure.
//
// - Support ParseEnv on mapping
// - Support ParseDefault on mapping
//
// Usage:
//
// dbInfo := Db{}
// config.Structure("db", &dbInfo)
func (c *Config) Structure(key string, dst any) (err error) {
var data any
// binding all data on key is empty.
if key == "" {
// fix: if c.data is nil, don't need to apply map structure
if len(c.data) == 0 {
// init default value by tag: default
if c.opts.ParseDefault {
err = structs.InitDefaults(dst, func(opt *structs.InitOptions) {
opt.ParseEnv = c.opts.ParseEnv
})
}
return
}
data = c.data
} else {
// binding sub-data of the config
var ok bool
data, ok = c.GetValue(key)
if !ok {
return ErrNotFound
}
}
// map structure from data
bindConf := c.opts.makeDecoderConfig()
// set result struct ptr
bindConf.Result = dst
decoder, err := mapstructure.NewDecoder(bindConf)
if err == nil {
if err = decoder.Decode(data); err != nil {
return err
}
}
// init default value by tag: default
if c.opts.ParseDefault {
err = structs.InitDefaults(dst, func(opt *structs.InitOptions) {
opt.ParseEnv = c.opts.ParseEnv
})
}
return err
}
// ToJSON string, will ignore error
func (c *Config) ToJSON() string {
buf := &bytes.Buffer{}
_, err := c.DumpTo(buf, JSON)
if err != nil {
return ""
}
return buf.String()
}
// WriteTo a writer
func WriteTo(out io.Writer) (int64, error) { return dc.WriteTo(out) }
// WriteTo Write out config data representing the current state to a writer.
func (c *Config) WriteTo(out io.Writer) (n int64, err error) {
return c.DumpTo(out, c.opts.DumpFormat)
}
// DumpTo a writer and use format
func DumpTo(out io.Writer, format string) (int64, error) { return dc.DumpTo(out, format) }
// DumpTo use the format(json,yaml,toml) dump config data to a writer
func (c *Config) DumpTo(out io.Writer, format string) (n int64, err error) {
var ok bool
var encoder Encoder
format = c.resolveFormat(format)
if encoder, ok = c.encoders[format]; !ok {
err = errors.New("not exists/register encoder for the format: " + format)
return
}
// is empty
if len(c.data) == 0 {
return
}
// encode data to string
encoded, err := encoder(c.data)
if err != nil {
return
}
// write content to out
num, _ := fmt.Fprintln(out, string(encoded))
return int64(num), nil
}
// DumpToFile use the format(json,yaml,toml) dump config data to a writer
func (c *Config) DumpToFile(fileName string, format string) (err error) {
fsFlags := os.O_CREATE | os.O_WRONLY | os.O_TRUNC
f, err := os.OpenFile(fileName, fsFlags, os.ModePerm)
if err != nil {
return err
}
_, err = c.DumpTo(f, format)
if err1 := f.Close(); err1 != nil && err == nil {
err = err1
}
return err
}