Compare commits

...

2 Commits

Author SHA1 Message Date
3493eee919 fix 2023-06-01 22:46:59 +08:00
e4fa93e893 优化 yaml本身就兼容json.... 2023-06-01 22:46:46 +08:00

View File

@ -1,15 +1,12 @@
package config
import (
"encoding/json"
"errors"
"fmt"
"github.com/fthvgb1/wp-go/safety"
"gopkg.in/yaml.v2"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
@ -106,23 +103,7 @@ func InitConfig(conf string) error {
return err
}
var c Config
switch strings.ToLower(filepath.Ext(conf)) {
case ".yaml":
err = yaml.Unmarshal(file, &c)
case ".json":
err = jsonToYaml(file, &c)
default:
err = yaml.Unmarshal(file, &c)
if err == nil {
break
}
err = jsonToYaml(file, &c)
if err == nil {
break
}
return errors.Join(errors.New("can't parse the config"), err)
}
err = yaml.Unmarshal(file, &c)
if err != nil {
return err
}
@ -130,27 +111,13 @@ 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"`
Db string `yaml:"db" json:"db,omitempty"`
User string `yaml:"user" json:"user,omitempty"`
Password string `yaml:"password" json:"password,omitempty"`
Charset string `yaml:"charset" json:"charset,omitempty"`
Host string `yaml:"host" json:"host,omitempty"`
Port string `yaml:"port" json:"port,omitempty"`
Db string `yaml:"db" json:"db,omitempty"`
User string `yaml:"user" json:"user,omitempty"`
Password string `yaml:"password" json:"password,omitempty"`
Charset string `yaml:"charset" json:"charset,omitempty"`
}
func (m Dsn) GetDsn() string {