feat:完善入驻信息管理

This commit is contained in:
henry
2021-12-03 15:22:23 +08:00
parent 851a2c1784
commit 911fcf9b1c
16 changed files with 100 additions and 27 deletions

42
utils/load.go Normal file
View 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
View 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))
}