Skip to content
Snippets Groups Projects
Commit 10e2b7a2 authored by Patrick's avatar Patrick
Browse files

change yaml unmarshal function to common package

parent a32c98eb
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
......@@ -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)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment