feat: 重构配置层,添加 json 支持

This commit is contained in:
Mmx233
2023-08-28 22:03:03 +08:00
parent 2a098e00dd
commit 6f283a93f5
19 changed files with 318 additions and 272 deletions

45
internal/config/reader.go Normal file
View File

@@ -0,0 +1,45 @@
package config
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"path"
)
func newReaderFromPath(pathname string) Reader {
switch path.Ext(pathname) {
case ".json":
return Json{}
case ".yaml":
return Yaml{}
default:
log.Warnf("未知配置类型,使用 yaml 进行解析")
return Yaml{}
}
}
type Reader interface {
Marshal(v any) ([]byte, error)
Unmarshal(data []byte, v any) error
}
type Json struct {
}
func (Json) Marshal(v any) ([]byte, error) {
return json.MarshalIndent(v, "", " ")
}
func (Json) Unmarshal(data []byte, v any) error {
return json.Unmarshal(data, v)
}
type Yaml struct {
}
func (Yaml) Marshal(v any) ([]byte, error) {
return yaml.Marshal(v)
}
func (Yaml) Unmarshal(data []byte, v any) error {
return yaml.Unmarshal(data, v)
}