Skip to content

Commit fb9973e

Browse files
committed
add flush config
1 parent 178abc4 commit fb9973e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

config.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@ func New(list ...Resolver) *Config {
3737
return &Config{
3838
data: &codec.BlobEncoder{
3939
Blob: make([]byte, 0),
40-
Ext: ".yaml",
40+
Ext: "",
4141
},
4242
list: list,
4343
}
4444
}
4545

46+
func (v *Config) Flush() {
47+
v.data.Blob = make([]byte, 0)
48+
v.data.Ext = ""
49+
}
50+
4651
func (v *Config) OpenBlob(b, ext string) {
4752
v.data.Blob = []byte(b)
4853
v.data.Ext = ext
@@ -59,12 +64,20 @@ func (v *Config) OpenFile(filename string) error {
5964
}
6065

6166
func (v *Config) Decode(configs ...interface{}) error {
67+
if len(v.data.Blob) == 0 || len(v.data.Ext) == 0 {
68+
return fmt.Errorf("config is empty")
69+
}
70+
6271
return v.data.Decode(configs...)
6372
}
6473

6574
var rexName = regexp.MustCompile(`(?m)^[a-z][a-z0-9]+$`)
6675

6776
func (v *Config) Build() error {
77+
if len(v.data.Blob) == 0 || len(v.data.Ext) == 0 {
78+
return fmt.Errorf("config is empty")
79+
}
80+
6881
for _, r := range v.list {
6982
if !rexName.MatchString(r.Name()) {
7083
return fmt.Errorf("resolver '%s' has invalid name, must like regexp [a-z][a-z0-9]+", r.Name())

config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ envs:
4545
casecheck.NotEqual(t, "fail", tc.Envs.Home)
4646
casecheck.NotEqual(t, "fail", tc.Envs.Path)
4747
casecheck.NotEqual(t, "123", tc.Envs.Tmp)
48+
49+
res.Flush()
50+
casecheck.Error(t, res.Build())
51+
casecheck.Error(t, res.Decode(&tc))
4852
}

0 commit comments

Comments
 (0)