-
Notifications
You must be signed in to change notification settings - Fork 3
/
export.go
141 lines (114 loc) · 3.16 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
package config
import (
"bytes"
"fmt"
"io"
"github.com/pkg/errors"
"github.com/mitchellh/mapstructure"
)
// MapStruct alias method of the 'Structure'
//
// Usage:
//
// dbInfo := &Db{}
// config.MapStruct("db", dbInfo)
func MapStruct(key string, dst interface{}) error { return dc.MapStruct(key, dst) }
// MapStruct alias method of the 'Structure'
func (c *Config) MapStruct(key string, dst interface{}) error {
return c.Structure(key, dst)
}
// BindStruct alias method of the 'Structure'
func BindStruct(key string, dst interface{}) error { return dc.BindStruct(key, dst) }
// BindStruct alias method of the 'Structure'
func (c *Config) BindStruct(key string, dst interface{}) error {
return c.Structure(key, dst)
}
// MapOnExists mapping data to the dst structure only on key exists.
func MapOnExists(key string, dst interface{}) error {
return dc.MapOnExists(key, dst)
}
// MapOnExists mapping data to the dst structure only on key exists.
func (c *Config) MapOnExists(key string, dst interface{}) 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.
//
// Usage:
//
// dbInfo := Db{}
// config.Structure("db", &dbInfo)
func (c *Config) Structure(key string, dst interface{}) error {
var data interface{}
if key == "" { // binding all data
data = c.data
} else { // some data of the config
var ok bool
data, ok = c.GetValue(key)
if !ok {
return errNotFound
}
}
var bindConf *mapstructure.DecoderConfig
if c.opts.DecoderConfig == nil {
bindConf = newDefaultDecoderConfig()
} else {
bindConf = c.opts.DecoderConfig
// Compatible with previous settings opts.TagName
if bindConf.TagName == "" {
bindConf.TagName = c.opts.TagName
}
}
// parse env var
if c.opts.ParseEnv && bindConf.DecodeHook == nil {
bindConf.DecodeHook = ParseEnvVarStringHookFunc()
}
bindConf.Result = dst // set result struct ptr
decoder, err := mapstructure.NewDecoder(bindConf)
if err != nil {
return err
}
return decoder.Decode(data)
}
// ToJSON string
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 = fixFormat(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
}