feat:优化信息

This commit is contained in:
henry
2021-11-24 09:08:07 +08:00
parent bcfe119972
commit d3f24450fb
9 changed files with 451 additions and 351 deletions

View File

@ -1,42 +0,0 @@
package lib
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)
}
}
}

View File

@ -1,37 +0,0 @@
package lib
import (
"ArmedPolice/utils"
"testing"
)
type (
structForSysConfig struct {
ID uint64 `json:"id"`
Kind int `json:"kind"`
Name string `json:"name"`
Key string `json:"key"`
Value string `json:"value"`
IsDeleted int `json:"is_deleted"`
}
structForSysMenu struct {
ID int `json:"id"`
ParentId int `json:"parent_id"`
Name string `json:"name"`
Kind int `json:"kind"`
Link string `json:"link"`
Component string `json:"component"`
Icon string `json:"icon"`
Auth int `json:"auth"`
Sort int `json:"sort"`
Remark string `json:"remark"`
Status int `json:"status"`
IsDeleted int `json:"is_deleted"`
}
)
func TestLoadConfig(t *testing.T) {
values := make([]*structForSysMenu, 0)
LoadConfig("../json/sys_menu.json", &values)
t.Log(utils.AnyToJSON(values))
}