Github.com/flyleft/gpr…

A simple go language configuration file library that automatically maps YAML configurations to entities, similar to spring-Boot configuration files. Struts from the root entity, if bool, string, integer, float, map[string]interface{}, []interface{}, YAML or Flag or environment variables will be parsed and assigned according to the profile value in the tag (if not set, the first letter of the current attribute name will be written smaller); If YAML or flag or environment variables do not exist, use profileDefault in tag to set the default value. If the default value is not set, error is returned. Default variable priority: Environment variables > Flag Parameters > YAML > profileDefault Default value. You can set envHigher not to false to set flag priority > environment variables. Note: Both the receive and return types need to be pointer types.

Usage under single 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
Copy the code
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"`
}

/ / use
func main(a)  {
	env, err := Profile(&SingleEnv{}, "test-single-profile.yml".true)
	iferr ! =nil {
		t.Error("Profile execute error", err)
	}
	trueEnv := env.(*SingleEnv)
}
/ / cover configuration by environment variables, such as setting EUREKA_INSTANCE_LEASERENEWALINTERVALINSECONDS environment variable value cover eureka instance. LeaseRenewalIntervalInSeconds
Copy the code

Use under multiple profiles:

profiles:
  active: dev # Set the profile to take effect

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

Copy the code
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(a)  {
	env, err := Profile(&MultiEnv{}, "test-multi-profile.yml".true)
	iferr ! =nil {
		t.Error("Profile execute error", err)
	}
	trueEnv := env.(*MultiEnv)
}
// The value of eureka. Zone can be overridden by the environment variable DEV_EUREKA_ZONE
Copy the code