设为首页 加入收藏

TOP

Go 处理yaml类型的配置文件(一)
2018-10-19 15:52:06 】 浏览:183
Tags:处理 yaml 类型 配置 文件

 

先说一下,这里用到了很多关于反射类型的功能,可能刚开始看代码,如果对反射不熟悉的可能会不是非常清晰,但是同时也是为了更好的理解golang中的反射,同时如果后面想在代码中可以直接从我的git地址get:
go get github.com/pythonsite/config_yaml
直接上代码:

// 可以用于处理读yaml格式的配置文件,同时也可以用于理解golang中的反射
package config_yaml

import (
    "strings"
    "errors"
    "io/ioutil"
    "gopkg.in/yaml.v2"
    "reflect"
    "fmt"
    "strconv"
)

type ConfigEngine struct {
    data map[interface{}]interface{}
}


// 将ymal文件中的内容进行加载
func (c *ConfigEngine) Load (path string) error {
    ext := c.guessFileType(path)
    if ext == "" {
        return errors.New("cant not load" + path + " config")
    }
    return c.loadFromYaml(path)
}

//判断配置文件名是否为yaml格式
func (c *ConfigEngine) guessFileType(path string) string {
    s := strings.Split(path,".")
    ext := s[len(s) - 1]
    switch ext {
    case "yaml","yml":
        return "yaml"
    }
    return ""
}

// 将配置yaml文件中的进行加载
func (c *ConfigEngine) loadFromYaml(path string) error {
    yamlS,readErr := ioutil.ReadFile(path)
    if readErr != nil {
        return readErr
    }
    // yaml解析的时候c.data如果没有被初始化,会自动为你做初始化
    err := yaml.Unmarshal(yamlS, &c.data)
    if err != nil {
        return errors.New("can not parse "+ path + " config" )
    }
    return nil
}

// 从配置文件中获取值
func (c *ConfigEngine) Get(name string) interface{}{
    path := strings.Split(name,".")
    data := c.data
    for key, value := range path {
        v, ok := data[value]
        if !ok {
            break
        }
        if (key + 1) == len(path) {
            return v
        }
        if reflect.TypeOf(v).String() == "map[interface {}]interface {}"{
            data = v.(map[interface {}]interface {})
        }
    }
    return nil
}

// 从配置文件中获取string类型的值
func (c *ConfigEngine) GetString(name string) string {
    value := c.Get(name)
    switch value:=value.(type){
    case string:
        return value
    case bool,float64,int:
        return fmt.Sprint(value)
    default:
        return ""
    }
}

// 从配置文件中获取int类型的值
func (c *ConfigEngine) GetInt(name string) int {
    value := c.Get(name)
    switch value := value.(type){
    case string:
        i,_:= strconv.Atoi(value)
        return i
    case int:
        return value
    case bool:
        if value{
            return 1
        }
        return 0
    case float64:
        return int(value)
    default:
        return 0
    }
}

// 从配置文件中获取bool类型的值
func (c *ConfigEngine) GetBool(name string) bool {
    value := c.Get(name)
    switch value := value.(type){
    case string:
        str,_:= strconv.ParseBool(value)
        return str
    case int:
        if value != 0 {
            return true
        }
        return false
    case bool:
        return value
    case float64:
        if value != 0.0 {
            return true
        }
        return false
    default:
        return false
    }
}

// 从配置文件中获取Float64类型的值
func (c *ConfigEngine) GetFloat64(name string) float64 {
    value := c.Get(name)
    switch value := value.(type){
    case string:
        str,_ := strconv.ParseFloat(value,64)
        return str
    case int:
        return float64(value)
    case bool:
        if value {
            return float64(1)
        }
        return float64(0)
    case float64:
        return value
    default:
        return float64(0)
    }
}

// 从配置文件中获取Struct类型的值,这里的struct是你自己定义的根据配置文件
func (c *ConfigEngine) GetStruct(name string,s interface{}) interface{}{
    d := c.Get(name)
    switch d.(type){
    case string:
        c.setField(s,name,d)
    case map[interface{}]interface{}:
        c.mapToStruct(d.(map[interface
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Go实现海量日志收集系统(四) 下一篇[日常] Go语言圣经--接口约定习题

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目