Go语言YAML配置文件库,类似于spring-boot

栏目: Java · 发布时间: 6年前

内容简介:一个简单的go语言配置文件库,将YAML配置自动映射到实体中, 类似与spring-boot配置文件。 会从根实体strut遍历实体属性,如果类型为bool, string, 整型, 浮点型,map[string]interface{},[]interface{}, 则会根据tag中的定义实体

一个简单的 go 语言配置文件库,将YAML配置自动映射到实体中, 类似与spring-boot配置文件。 会从根实体strut遍历实体属性,如果类型为bool, string, 整型, 浮点型,map[string]interface{},[]interface{}, 则会根据tag中的 profile 值(不设置则为当前属性名的首字母变小写)解析YAML或flag或环境变量,并赋值; 若YAML或flag或环境变量都不存在,则使用tag中的 profileDefault 设置默认值; 若默认值也未设置,则返回error。默认变量优先级啊:环境变量 > flag参数 > YAML > profileDefault默认值, 可以通过设置envHigher未false,让flag优先级 > 环境变量。 注意: 接收类型和返回类型都需要为指针类型

单profile下的使用:

eureka:
  instance:
    preferIpAddress: true
    leaseRenewalIntervalInSeconds: 10
    leaseExpirationDurationInSeconds: 30
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/
    registryFetchIntervalSeconds: 10
logging:
  level:
    github.com/flyleft/consul-iris/pkg/config: info
    github.com/flyleft/consul-iris/pkg/route: debug
复制代码
type SingleEnv struct {
	Eureka  SingleEureka
	Logging map[string]interface{} `profile:"logging.level" profileDefault:"{\"github.com/flyleft/consul-iris\":\"debug\"}"`
}

type SingleEureka struct {
	PreferIpAddress                  bool   `profile:"instance.preferIpAddress"`
	LeaseRenewalIntervalInSeconds    int32  `profile:"instance.leaseRenewalIntervalInSeconds"`
	LeaseExpirationDurationInSeconds uint   `profile:"instance.leaseExpirationDurationInSeconds"`
	ServerDefaultZone                string `profile:"client.serviceUrl.defaultZone" profileDefault:"http://localhost:8000/eureka/"`
	RegistryFetchIntervalSeconds     byte   `profile:"client.registryFetchIntervalSeconds"`
}

//使用
func main()  {
	env, err := Profile(&SingleEnv{}, "test-single-profile.yml", true)
	if err != nil {
		t.Error("Profile execute error", err)
	}
	trueEnv := env.(*SingleEnv)
}
//通过环境变量覆盖配置,比如设置EUREKA_INSTANCE_LEASERENEWALINTERVALINSECONDS环境变量值覆盖eureka.instance.leaseRenewalIntervalInSeconds
复制代码

多profile下的使用:

profiles:
  active: dev # 设置生效的profile

dev:
  database:
    username: root
    password: root
  eureka:
    zone: http://localhost:8000/eureka/
    fetchInterval: 10
  logging:
    level:
      github.com/flyleft/consul-iris/pkg/config: info
      github.com/flyleft/consul-iris/pkg/route: debug


production:
  database:
    username: production
    password: production
  eureka:
    zone: http://localhost:8000/eureka/
    fetchInterval: 10
  logging:
    level:
      github.com/flyleft/consul-iris/pkg/config: info
      github.com/flyleft/consul-iris/pkg/route: debug

复制代码
type Eureka struct {
	Zone          string `profile:"zone"`
	FetchInterval int    `profile:"fetchInterval"`
}

type DataSource struct {
	Host     string `profile:"host" profileDefault:"localhost"`
	Username string `profile:"username"`
	Password string `profile:"password"`
}

type MultiEnv struct {
	DataSource DataSource `profile:"database"`
	Eureka     Eureka
	Logging    map[string]interface{} `profile:"logging.level" profileDefault:"{\"github.com/flyleft/consul-iris\":\"debug\"}"`
	Users      []interface{}          `profile:"users" profileDefault:"[\"admin\",\"test\",\"root\"]"`
}
func main()  {
	env, err := Profile(&MultiEnv{}, "test-multi-profile.yml", true)
	if err != nil {
		t.Error("Profile execute error", err)
	}
	trueEnv := env.(*MultiEnv)
}
//可通过环境变量DEV_EUREKA_ZONE覆盖eureka.zone的值
复制代码

定义实体


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

自品牌

自品牌

[美] 丹·斯柯伯尔(Dan Schawbel) / 佘卓桓 / 湖南文艺出版社 / 2016-1-1 / 39.80元

什么是自品牌?如何利用新媒体推广自己?如何放大自己的职业优势? 细化到如何巩固“弱联系”人脉?如何在团队里合作与生存?如何开创自己的事业?这些都是职场人不得不面临的问题,但少有人告诉你答案,你需要利用书里分享的高效方法独辟蹊径,把自己变成职场里高性价比的人才。这是一本教你利用新型社交媒体开发职业潜能的自我管理读本,不管你是新人还是老鸟,都可以通过打造自品牌在职场中脱颖而出。如果不甘平庸,就亮......一起来看看 《自品牌》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具