This commit is contained in:
xing 2023-06-01 21:30:14 +08:00
parent 7431823ac7
commit a08b9538ae

View File

@ -2,6 +2,7 @@ package config
import (
"encoding/json"
"errors"
"fmt"
"github.com/fthvgb1/wp-go/safety"
"gopkg.in/yaml.v2"
@ -109,24 +110,17 @@ func InitConfig(conf string) error {
case ".yaml":
err = yaml.Unmarshal(file, &c)
case ".json":
var v map[string]any
err = json.Unmarshal(file, &v)
if err == nil {
marshal, er := yaml.Marshal(v)
if er != nil {
return er
}
err = yaml.Unmarshal(marshal, &c)
}
err = jsonToYaml(file, &c)
default:
err = yaml.Unmarshal(file, &c)
if err == nil {
break
}
err = json.Unmarshal(file, &c)
err = jsonToYaml(file, &c)
if err == nil {
break
}
return errors.Join(errors.New("can't parse the config"), err)
}
if err != nil {
@ -136,6 +130,20 @@ func InitConfig(conf string) error {
return nil
}
func jsonToYaml[T any](b []byte, c T) error {
var v map[string]any
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
marshal, er := yaml.Marshal(v)
if er != nil {
return er
}
err = yaml.Unmarshal(marshal, c)
return err
}
type Dsn struct {
Host string `yaml:"host" json:"host,omitempty"`
Port json.Number `yaml:"port" json:"port,omitempty"`