feat:完善入驻信息管理
This commit is contained in:
42
utils/load.go
Normal file
42
utils/load.go
Normal file
@ -0,0 +1,42 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
32
utils/load_test.go
Normal file
32
utils/load_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type ManagePatent struct {
|
||||
Kind string `json:"kind"`
|
||||
Title string `json:"title"`
|
||||
FileUrl string `json:"file_url"`
|
||||
ApplyCode string `json:"apply_code"`
|
||||
ApplyAt string `json:"apply_at"`
|
||||
OpenCode string `json:"open_code"`
|
||||
OpenAt string `json:"open_at"`
|
||||
ApplyName string `json:"apply_name"`
|
||||
ApplyAddress string `json:"apply_address"`
|
||||
Inventor string `json:"inventor"`
|
||||
Description string `json:"description"`
|
||||
PrincipalClaim string `json:"principal_claim"`
|
||||
IpcCode string `json:"ipc_code"`
|
||||
}
|
||||
|
||||
func TestLoadConfig(t *testing.T) {
|
||||
file := "../file/manage_patent.json"
|
||||
|
||||
out := make([]*ManagePatent, 0)
|
||||
|
||||
LoadConfig(file, &out)
|
||||
|
||||
fmt.Println(AnyToJSON(out))
|
||||
}
|
Reference in New Issue
Block a user