From 47aa2e30f8c4e95afa21edb34d855495f68e1e08 Mon Sep 17 00:00:00 2001 From: Ilya Kaznacheev Date: Sun, 14 Jun 2020 21:00:02 +0300 Subject: [PATCH] Update docs --- README.md | 7 ++++--- cleanenv.go | 32 +++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2670882..d0de12d 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ Here remote host and port may change in a distributed system architecture. Field ### Description -You can get descriptions of all environment variables to use them in help documentation. +You can get descriptions of all environment variables to use them in the help documentation. ```go import github.com/ilyakaznacheev/cleanenv @@ -169,10 +169,11 @@ Environment variables: ## Model Format -Library uses tags to configure model of configuration structure. There are following tags: +Library uses tags to configure the model of configuration structure. There are the following tags: - `env=""` - environment variable name (e.g. `env="PORT"`); - `env-upd` - flag to mark a field as updatable. Run `UpdateEnv(&cfg)` to refresh updatable variables from environment; +- `env-required` - flag to mark a field as required. If set will return an error during environment parsing when the flagged as required field is empty (default Go value). Tag `env-default` is ignored in this case; - `env-default=""` - default value. If the field wasn't filled from the environment variable default value will be used instead; - `env-separator=""` - custom list and map separator. If not set, the default separator `,` will be used; - `env-description=""` - environment variable description; @@ -249,7 +250,7 @@ There are several most popular config file formats supported: ## Integration -Package can be used with many other solutions. To make it more useful, we made some helpers. +The package can be used with many other solutions. To make it more useful, we made some helpers. ### Flag diff --git a/cleanenv.go b/cleanenv.go index e494b02..6a05a2f 100644 --- a/cleanenv.go +++ b/cleanenv.go @@ -38,6 +38,24 @@ const ( DefaultSeparator = "," ) +// Supported tags +const ( + // Name of the environment variable or a list of names + TagEnv = "env" + // Value parsing layout (for types like time.Time) + TagEnvLayout = "env-layout" + // Default value + TagEnvDefault = "env-default" + // Custom list and map separator + TagEnvSeparator = "env-separator" + // Environment variable description + TagEnvDescription = "env-description" + // Flag to mark a field as updatable + TagEnvUpd = "env-upd" + // Flag to mark a field as required + TagEnvRequired = "env-required" +) + // Setter is an interface for a custom value setter. // // To implement a custom value setter you need to add a SetValue function to your type that will receive a string raw value: @@ -232,7 +250,7 @@ func readStructMetadata(cfgRoot interface{}) ([]structMeta, error) { continue } // process time.Time - if l, ok := fType.Tag.Lookup("env-layout"); ok { + if l, ok := fType.Tag.Lookup(TagEnvLayout); ok { layout = &l } } @@ -242,23 +260,23 @@ func readStructMetadata(cfgRoot interface{}) ([]structMeta, error) { continue } - if def, ok := fType.Tag.Lookup("env-default"); ok { + if def, ok := fType.Tag.Lookup(TagEnvDefault); ok { defValue = &def } - if sep, ok := fType.Tag.Lookup("env-separator"); ok { + if sep, ok := fType.Tag.Lookup(TagEnvSeparator); ok { separator = sep } else { separator = DefaultSeparator } - _, upd := fType.Tag.Lookup("env-upd") + _, upd := fType.Tag.Lookup(TagEnvUpd) - _, required := fType.Tag.Lookup("env-required") + _, required := fType.Tag.Lookup(TagEnvRequired) envList := make([]string, 0) - if envs, ok := fType.Tag.Lookup("env"); ok && len(envs) != 0 { + if envs, ok := fType.Tag.Lookup(TagEnv); ok && len(envs) != 0 { envList = strings.Split(envs, DefaultSeparator) } @@ -269,7 +287,7 @@ func readStructMetadata(cfgRoot interface{}) ([]structMeta, error) { defValue: defValue, layout: layout, separator: separator, - description: fType.Tag.Get("env-description"), + description: fType.Tag.Get(TagEnvDescription), updatable: upd, required: required, })