From 10e2b7a20e5eebec3167f536be2efd3ea61845e9 Mon Sep 17 00:00:00 2001 From: Patrick <dreamlike.sky@foxmail.com> Date: Tue, 10 Mar 2020 12:37:07 +0800 Subject: [PATCH] change yaml unmarshal function to common package --- common/yaml/yaml.go | 25 ++++++++++++++++--------- config/base_config.go | 25 ------------------------- 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/common/yaml/yaml.go b/common/yaml/yaml.go index 79e5f67d8..b4c135c7a 100644 --- a/common/yaml/yaml.go +++ b/common/yaml/yaml.go @@ -10,17 +10,24 @@ import ( "gopkg.in/yaml.v2" ) -func UnmarshalYMLConfig(yamlFile string, out interface{}) error { - if path.Ext(yamlFile) != ".yml" { - return perrors.Errorf("yamlFile name{%v} suffix must be .yml", yamlFile) +// loadYMLConfig Load yml config byte from file +func loadYMLConfig(confProFile string) ([]byte, error) { + if len(confProFile) == 0 { + return nil, perrors.Errorf("application configure(provider) file name is nil") } - confFileStream, err := ioutil.ReadFile(yamlFile) - if err != nil { - return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", yamlFile, perrors.WithStack(err)) + + if path.Ext(confProFile) != ".yml" { + return nil, perrors.Errorf("application configure file name{%v} suffix must be .yml", confProFile) } - err = yaml.Unmarshal(confFileStream, out) + + return ioutil.ReadFile(confProFile) +} + +// unmarshalYMLConfig Load yml config byte from file , then unmarshal to object +func UnmarshalYMLConfig(confProFile string, out interface{}) error { + confFileStream, err := loadYMLConfig(confProFile) if err != nil { - return perrors.Errorf("yaml.Unmarshal() = error:%v", perrors.WithStack(err)) + return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confProFile, perrors.WithStack(err)) } - return nil + return yaml.Unmarshal(confFileStream, out) } diff --git a/config/base_config.go b/config/base_config.go index 6d5ec7e24..787297c18 100644 --- a/config/base_config.go +++ b/config/base_config.go @@ -18,8 +18,6 @@ package config import ( - "io/ioutil" - "path" "reflect" "strconv" "strings" @@ -27,7 +25,6 @@ import ( import ( perrors "github.com/pkg/errors" - "gopkg.in/yaml.v2" ) import ( @@ -366,25 +363,3 @@ func initializeStruct(t reflect.Type, v reflect.Value) { } } - -// loadYMLConfig Load yml config byte from file -func loadYMLConfig(confProFile string) ([]byte, error) { - if len(confProFile) == 0 { - return nil, perrors.Errorf("application configure(provider) file name is nil") - } - - if path.Ext(confProFile) != ".yml" { - return nil, perrors.Errorf("application configure file name{%v} suffix must be .yml", confProFile) - } - - return ioutil.ReadFile(confProFile) -} - -// unmarshalYMLConfig Load yml config byte from file , then unmarshal to object -func unmarshalYMLConfig(confProFile string, out interface{}) error { - confFileStream, err := loadYMLConfig(confProFile) - if err != nil { - return perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confProFile, perrors.WithStack(err)) - } - return yaml.Unmarshal(confFileStream, out) -} -- GitLab