Documentation
¶
Overview ¶
Yaml Config Loader
Index ¶
- Variables
- func Delims(left, right string)
- func Load(conf interface{}, configPaths ...string) error
- func LoadBytes(conf interface{}, src []byte) error
- func LoadJSON(conf interface{}, configPaths ...string) error
- func LoadJSONBytes(conf interface{}, src []byte) error
- func LoadTOML(conf interface{}, configPaths ...string) error
- func LoadTOMLBytes(conf interface{}, src []byte) error
- func LoadWithEnv(conf interface{}, configPaths ...string) error
- func LoadWithEnvBytes(conf interface{}, src []byte) error
- func LoadWithEnvJSON(conf interface{}, configPaths ...string) error
- func LoadWithEnvJSONBytes(conf interface{}, src []byte) error
- func LoadWithEnvTOML(conf interface{}, configPaths ...string) error
- func LoadWithEnvTOMLBytes(conf interface{}, src []byte) error
- func MarshalJSON(v interface{}) ([]byte, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Marshal = yaml.Marshal
Marshal serializes the value provided into a YAML document.
Functions ¶
func Delims ¶ added in v0.2.0
func Delims(left, right string)
Delims sets the action delimiters to the specified strings.
func Load ¶
Load loads YAML files from `configPaths`. and assigns decoded values into the `conf` value.
Example ¶
type DBConfig struct { Master string `yaml:"master"` Slave string `yaml:"slave"` Timeout time.Duration `yml:"timeout"` } type Conf struct { Domain string `yaml:"domain"` IsDev bool `yaml:"is_dev"` Timeout time.Duration `yml:"timeout"` DB DBConfig `yaml:"db"` } baseConfig := ` # config.yml domain: example.com db: master: rw@/example slave: ro@/example timeout: 0.5s ` localConfig := ` # config_local.yml domain: dev.example.com is_dev: true ` conf := &Conf{} baseConfigYaml, _ := genConfigFile("config.yml", baseConfig) // /path/to/config.yml localConfigYaml, _ := genConfigFile("config_local.yml", localConfig) // /path/to/config_local.yml err := config.Load(conf, baseConfigYaml, localConfigYaml) if err != nil { panic(err) } fmt.Printf("%+v", conf)
Output: &{Domain:dev.example.com IsDev:true Timeout:0s DB:{Master:rw@/example Slave:ro@/example Timeout:500ms}}
func LoadJSON ¶
Load loads JSON files from `configPaths`. and assigns decoded values into the `conf` value.
func LoadJSONBytes ¶ added in v0.1.0
LoadJSONBytes loads JSON bytes
func LoadTOML ¶
Load loads TOML files from `configPaths`. and assigns decoded values into the `conf` value.
func LoadTOMLBytes ¶ added in v0.1.0
LoadTOMLBytes loads TOML bytes
func LoadWithEnv ¶
LoadWithEnv loads YAML files with Env replace {{ env "ENV" }} to os.Getenv("ENV") if you set default value then {{ env "ENV" "default" }}
Example ¶
type DBConfig struct { Master string `yaml:"master"` Slave string `yaml:"slave"` Timeout time.Duration `yml:"timeout"` } type Conf struct { Domain string `yaml:"domain"` IsDev bool `yaml:"is_dev"` Timeout time.Duration `yml:"timeout"` DB DBConfig `yaml:"db"` } baseConfig := ` # config.yml domain: {{ env "DOMAIN"}} db: master: rw@/example slave: ro@/example timeout: 0.5s ` localConfig := ` # config_local.yml is_dev: true db: master: {{ env "RW_USER" "rw" }}@/{{ env "DB_NAME" "example_dev" }} slave: {{ env "RO_USER" "ro" }}@/{{ env "DB_NAME" "example_dev" }} ` os.Setenv("DOMAIN", "dev.example.com") os.Setenv("DB_NAME", "example_local") conf := &Conf{} baseConfigYaml, _ := genConfigFile("config.yml", baseConfig) // /path/to/config.yml localConfigYaml, _ := genConfigFile("config_local.yml", localConfig) // /path/to/config_local.yml err := config.LoadWithEnv(conf, baseConfigYaml, localConfigYaml) if err != nil { panic(err) } fmt.Printf("%+v", conf)
Output: &{Domain:dev.example.com IsDev:true Timeout:0s DB:{Master:rw@/example_local Slave:ro@/example_local Timeout:500ms}}
func LoadWithEnvBytes ¶ added in v0.1.0
LoadWithEnvBytes loads YAML bytes with Env
func LoadWithEnvJSON ¶
LoadWithEnvJSON loads JSON files with Env
func LoadWithEnvJSONBytes ¶ added in v0.1.0
LoadWithEnvJSONBytes loads JSON bytes with Env
func LoadWithEnvTOML ¶
LoadWithEnvTOML loads TOML files with Env
func LoadWithEnvTOMLBytes ¶ added in v0.1.0
LoadWithEnvTOMLBytes loads TOML bytes with Env
func MarshalJSON ¶
MarshalJSON returns the JSON encoding of v with indent by 2 white spaces.
Types ¶
This section is empty.