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

github.com/flyleft/gpr…

一个简单的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的值
复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值