github.com/dubbogo/gost@v1.14.0/encoding/yaml/yaml.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package yaml 19 20 import ( 21 "io/ioutil" 22 "path" 23 ) 24 25 import ( 26 perrors "github.com/pkg/errors" 27 28 "gopkg.in/yaml.v2" 29 ) 30 31 // LoadYMLConfig Load yml config byte from file 32 func LoadYMLConfig(confProFile string) ([]byte, error) { 33 if len(confProFile) == 0 { 34 return nil, perrors.Errorf("application configure(provider) file name is nil") 35 } 36 37 if path.Ext(confProFile) != ".yml" { 38 return nil, perrors.Errorf("application configure file name{%v} suffix must be .yml", confProFile) 39 } 40 41 return ioutil.ReadFile(confProFile) 42 } 43 44 // UnmarshalYMLConfig Load yml config byte from file, then unmarshal to object 45 func UnmarshalYMLConfig(confProFile string, out interface{}) ([]byte, error) { 46 confFileStream, err := LoadYMLConfig(confProFile) 47 if err != nil { 48 return confFileStream, perrors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confProFile, perrors.WithStack(err)) 49 } 50 return confFileStream, yaml.Unmarshal(confFileStream, out) 51 } 52 53 // UnmarshalYML unmarshals decodes the first document found within the in byte slice and assigns decoded values into the out value. 54 func UnmarshalYML(data []byte, out interface{}) error { 55 return yaml.Unmarshal(data, out) 56 } 57 58 // MarshalYML serializes the value provided into a YAML document. 59 func MarshalYML(in interface{}) ([]byte, error) { 60 return yaml.Marshal(in) 61 }