Files
2021-12-03 15:22:23 +08:00

43 lines
851 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"encoding/json"
"io/ioutil"
"path"
"gopkg.in/yaml.v2"
)
type callback func(interface{})
var loaders = map[string]func([]byte, interface{}) error{
".json": LoadConfigFormJsonBytes,
".yaml": LoadConfigFromYamlBytes,
}
func LoadConfigFormJsonBytes(content []byte, obj interface{}) error {
return json.Unmarshal(content, obj)
}
func LoadConfigFromYamlBytes(content []byte, obj interface{}) error {
return yaml.Unmarshal(content, obj)
}
func LoadConfig(file string, v interface{}, callback ...callback) {
content, err := ioutil.ReadFile(file)
if err != nil {
panic("Load Config Error " + err.Error())
}
loader, ok := loaders[path.Ext(file)]
if !ok {
panic("Unknown File Type" + path.Ext(file))
}
if err = loader(content, v); err == nil {
for _, _callback := range callback {
_callback(v)
}
}
}