2017-12-12 15:00:34 +07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 eschao <esc.chao@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2017-12-08 20:02:26 +07:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2017-12-11 16:14:53 +07:00
|
|
|
|
2024-10-19 22:53:00 +07:00
|
|
|
"git.mousesoft.ru/ms/config/utils"
|
2017-12-08 20:02:26 +07:00
|
|
|
)
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// Parse parses given structure interface, extracts environment definitions
|
2024-10-19 22:48:50 +07:00
|
|
|
// from its tag and sets structure with defined environment variables
|
2017-12-11 16:14:53 +07:00
|
|
|
func Parse(i interface{}) error {
|
|
|
|
return ParseWith(i, "")
|
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// ParseWith parses with given structure interface and environment name prefix
|
|
|
|
// It is normally used in nested structure.
|
|
|
|
// For example: we have such structure
|
|
|
|
// type Database struct {
|
|
|
|
// Host string `env:"HOST"`
|
|
|
|
// }
|
|
|
|
|
2024-10-19 22:48:50 +07:00
|
|
|
// type Server struct {
|
|
|
|
// Server string `env:"SERVER"`
|
|
|
|
// DB Database `env:"DB_"`
|
|
|
|
// }
|
|
|
|
//
|
2017-12-12 15:00:34 +07:00
|
|
|
// The Server.DB.Host will be mapped to environment variable: DB_HOST which is
|
|
|
|
// concatenated from DB tag in Server struct and Host tag in Database struct
|
2017-12-11 16:14:53 +07:00
|
|
|
func ParseWith(i interface{}, prefix string) error {
|
2017-12-08 20:02:26 +07:00
|
|
|
ptrRef := reflect.ValueOf(i)
|
|
|
|
|
|
|
|
if ptrRef.IsNil() || ptrRef.Kind() != reflect.Ptr {
|
2024-10-19 22:48:50 +07:00
|
|
|
return fmt.Errorf("expect a structure pointer type instead of %s",
|
2017-12-08 20:02:26 +07:00
|
|
|
ptrRef.Kind().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
valueOfStruct := ptrRef.Elem()
|
|
|
|
if valueOfStruct.Kind() != reflect.Struct {
|
2024-10-19 22:48:50 +07:00
|
|
|
return fmt.Errorf("expect a structure pointer type instead of %s",
|
2017-12-08 20:02:26 +07:00
|
|
|
valueOfStruct.Kind().String())
|
|
|
|
}
|
|
|
|
|
2017-12-11 16:14:53 +07:00
|
|
|
return parseValue(valueOfStruct, prefix)
|
2017-12-08 20:02:26 +07:00
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// parseValue parses a reflect.Value object
|
2017-12-11 16:14:53 +07:00
|
|
|
func parseValue(v reflect.Value, prefix string) error {
|
2017-12-08 20:02:26 +07:00
|
|
|
typeOfStruct := v.Type()
|
2017-12-11 16:14:53 +07:00
|
|
|
var err error
|
|
|
|
for i := 0; i < v.NumField() && err == nil; i++ {
|
2017-12-08 20:02:26 +07:00
|
|
|
valueOfField := v.Field(i)
|
|
|
|
kindOfField := valueOfField.Kind()
|
|
|
|
structOfField := typeOfStruct.Field(i)
|
|
|
|
|
|
|
|
// recursively unmarshal if value is ptr type
|
|
|
|
if kindOfField == reflect.Ptr {
|
|
|
|
if !valueOfField.IsNil() && valueOfField.CanSet() {
|
2017-12-11 16:14:53 +07:00
|
|
|
err = ParseWith(valueOfField.Interface(),
|
|
|
|
prefix+structOfField.Tag.Get("env"))
|
2017-12-08 20:02:26 +07:00
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if kindOfField == reflect.Struct {
|
2017-12-11 16:14:53 +07:00
|
|
|
err = parseValue(valueOfField, prefix+structOfField.Tag.Get("env"))
|
2017-12-08 20:02:26 +07:00
|
|
|
}
|
|
|
|
|
2024-10-19 22:48:50 +07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-11 16:14:53 +07:00
|
|
|
err = setFieldValue(valueOfField, structOfField, prefix)
|
2017-12-08 20:02:26 +07:00
|
|
|
}
|
|
|
|
|
2017-12-11 16:14:53 +07:00
|
|
|
return err
|
2017-12-08 20:02:26 +07:00
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// setFieldValue sets a reflect.Value with environment value
|
2017-12-11 16:14:53 +07:00
|
|
|
func setFieldValue(v reflect.Value, f reflect.StructField, prefix string) error {
|
2017-12-08 20:02:26 +07:00
|
|
|
envName := f.Tag.Get("env")
|
|
|
|
if envName == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-15 12:57:51 +07:00
|
|
|
envValue, ok := os.LookupEnv(prefix + envName)
|
2017-12-08 20:02:26 +07:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !v.CanSet() {
|
|
|
|
return fmt.Errorf("%s: can't be set", f.Name)
|
|
|
|
}
|
|
|
|
|
2017-12-11 16:14:53 +07:00
|
|
|
var err error
|
2017-12-08 20:02:26 +07:00
|
|
|
kind := v.Kind()
|
|
|
|
switch kind {
|
|
|
|
case reflect.Bool:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithBool(v, envValue)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.String:
|
|
|
|
v.SetString(envValue)
|
|
|
|
case reflect.Int8:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithIntX(v, envValue, 8)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Int16:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithIntX(v, envValue, 16)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Int, reflect.Int32:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithIntX(v, envValue, 32)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Int64:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithIntX(v, envValue, 64)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Uint8:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithUintX(v, envValue, 8)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Uint16:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithUintX(v, envValue, 16)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Uint, reflect.Uint32:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithUintX(v, envValue, 32)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Uint64:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithUintX(v, envValue, 64)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Float32:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithFloatX(v, envValue, 32)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Float64:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithFloatX(v, envValue, 64)
|
2017-12-08 20:02:26 +07:00
|
|
|
|
|
|
|
case reflect.Slice:
|
|
|
|
sp, ok := f.Tag.Lookup("separator")
|
|
|
|
if !ok {
|
|
|
|
sp = ":"
|
|
|
|
}
|
2017-12-11 16:14:53 +07:00
|
|
|
err = utils.SetValueWithSlice(v, envValue, sp)
|
2017-12-08 20:02:26 +07:00
|
|
|
|
|
|
|
default:
|
2024-10-19 22:48:50 +07:00
|
|
|
return fmt.Errorf("unsupported type: %s", kind.String())
|
2017-12-08 20:02:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-12-11 16:14:53 +07:00
|
|
|
return fmt.Errorf("%s: %s", f.Name, err.Error())
|
2017-12-08 20:02:26 +07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|