diff --git a/internal/basic/config/config_test.go b/internal/basic/config/config_test.go index ea1adc1fe4f30afe0036553c87e9b9e17fc49555..dc0dc5b53645a36fe20119816775560e7167700a 100644 --- a/internal/basic/config/config_test.go +++ b/internal/basic/config/config_test.go @@ -39,3 +39,13 @@ func TestHttpConfs_GetConf(t *testing.T) { fmt.Printf("%#v\n", conf) } + +func TestYaml(t *testing.T) { + var dbConf DataBaseConf + + if err := NewYamlParse().Parse("configs/database.yaml", &dbConf); err != nil { + t.Error(err) + return + } + t.Log(dbConf) +} diff --git a/internal/basic/config/util.go b/internal/basic/config/util.go index 935dc39d485f4e36c0b5c04b88afa327417d925c..8de81d0c294778215aad1d20420b05b0da8ab77f 100644 --- a/internal/basic/config/util.go +++ b/internal/basic/config/util.go @@ -4,6 +4,7 @@ import ( "fmt" "gopkg.in/yaml.v3" "io/ioutil" + "os" ) // 解析配置文件 @@ -19,3 +20,29 @@ func YamlParse(filePath string, data interface{}) error { } return nil } + +// Parse 解析接口 +type Parse interface { + Parse(filePath string, data interface{}) error +} + +var _ Parse = &Yaml{} //实现解析接口 + +// Yaml 解析实现了(yaml) +type Yaml struct { +} + +func NewYamlParse() Parse { + return &Yaml{} +} + +func (conf *Yaml) Parse(filePath string, data interface{}) error { + b, err := os.ReadFile(filePath) + if err != nil { + return fmt.Errorf("文件读取错误 %w", err) + } + if err = yaml.Unmarshal(b, data); err != nil { + return fmt.Errorf("yaml 配置解析错误 %w", err) + } + return nil +}