/*
UnmarshalENV accepts a struct with the `green` field tag defined
for its fields and assigns values from the environment or the defaults
to `env`.
Since os.Getenv() always returns a string we leave conversion to other
data types up to the caller. Any field whose type isn't a string
will be skipped as will any field that doesn't have a `green` struct tag.
*/
UnmarshalENV accepts a struct with the `green` field tag defined
for its fields and assigns values from the environment or the defaults
to `env`.
Since os.Getenv() always returns a string we leave conversion to other
data types up to the caller. Any field whose type isn't a string
will be skipped as will any field that doesn't have a `green` struct tag.
/ * UnmarshalENV accepts a struct with the ` green ` field tag defined for its fields and assigns values from the environment or the defaults to ` env ` . Since os . Getenv () always returns a string we leave conversion to other data types up to the caller . Any field whose type isn ' t a string will be skipped as will any field that doesn ' t have a ` green ` struct tag . * /
func UnmarshalENV(env Environment) (err error) {
	defer func() {
		if r := recover(); r != nil {
			envType := reflect.TypeOf(env).String()
			err = fmt.Errorf("must provide pointer to struct but given %s", envType)
		}
	}()
	envStruct := reflect.ValueOf(env).Elem()
	if !envStruct.CanAddr() {
		return errors.New("argument not addressable")
	}
	values := LoadEnv(env)
	fields := getStructFields(env)
	for _, field := range fields {
		if field.Type.String() != "string" {
			continue
		}
		ft := parseTag(field)
		val, found := values[ft.EnvVarName]
		if !found {
			continue
		}
		fieldVal := envStruct.FieldByName(ft.FieldName)
		fieldVal.Set(reflect.ValueOf(val))
	}
	return
}
func UnmarshalENV ( env Environment ) ( err error ) { defer func ( ) { if r := recover ( ) ; r != nil { envType := reflect . TypeOf ( env ) . String ( ) err = fmt . Errorf ( " " , envType ) } } ( ) envStruct := reflect . ValueOf ( env ) . Elem ( ) if ! envStruct . CanAddr ( ) { return errors . New ( " " ) } values := LoadEnv ( env ) fields := getStructFields ( env ) for _ , field := range fields { if field . Type . String ( ) != " " { continue } ft := parseTag ( field ) val , found := values [ ft . EnvVarName ] if ! found { continue } fieldVal := envStruct . FieldByName ( ft . FieldName ) fieldVal . Set ( reflect . ValueOf ( val ) ) } return }
UnmarshalENV accepts a struct with the `green` field tag defined
for its fields and assigns values from the environment or the defaults
to `env`.
UnmarshalENV accepts a struct with the ` green ` field tag defined for its fields and assigns values from the environment or the defaults to ` env ` .