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.
|
|
|
|
*/
|
2024-10-20 13:50:44 +07:00
|
|
|
package config
|
2017-12-08 20:02:26 +07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-21 00:15:51 +07:00
|
|
|
"io"
|
2017-12-08 20:02:26 +07:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
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
|
2024-10-20 13:50:44 +07:00
|
|
|
func ParseEnv(i interface{}) error {
|
|
|
|
return ParseEnvWith(i, "")
|
2017-12-11 16:14:53 +07:00
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
// UsageEnv prints usage description of config i to out
|
|
|
|
func UsageEnv(out io.Writer, i interface{}) {
|
|
|
|
// STub
|
|
|
|
}
|
|
|
|
|
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
|
2024-10-20 13:50:44 +07:00
|
|
|
func ParseEnvWith(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())
|
|
|
|
}
|
|
|
|
|
2024-10-20 13:50:44 +07:00
|
|
|
return parseValueEnv(valueOfStruct, prefix)
|
2017-12-08 20:02:26 +07:00
|
|
|
}
|
|
|
|
|
2017-12-12 15:00:34 +07:00
|
|
|
// parseValue parses a reflect.Value object
|
2024-10-21 00:15:51 +07:00
|
|
|
func parseValueEnv(value reflect.Value, prefix string) error {
|
2017-12-11 16:14:53 +07:00
|
|
|
var err error
|
2024-10-21 00:15:51 +07:00
|
|
|
|
|
|
|
typeOfStruct := value.Type()
|
|
|
|
|
|
|
|
for i := 0; i < value.NumField() && err == nil; i++ {
|
|
|
|
valueOfField := value.Field(i)
|
2017-12-08 20:02:26 +07:00
|
|
|
kindOfField := valueOfField.Kind()
|
|
|
|
structOfField := typeOfStruct.Field(i)
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
// Recursively unmarshal if value is ptr type
|
2017-12-08 20:02:26 +07:00
|
|
|
if kindOfField == reflect.Ptr {
|
|
|
|
if !valueOfField.IsNil() && valueOfField.CanSet() {
|
2024-10-20 13:50:44 +07:00
|
|
|
err = ParseEnvWith(valueOfField.Interface(),
|
2017-12-11 16:14:53 +07:00
|
|
|
prefix+structOfField.Tag.Get("env"))
|
2017-12-08 20:02:26 +07:00
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if kindOfField == reflect.Struct {
|
2024-10-20 13:50:44 +07:00
|
|
|
err = parseValueEnv(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
|
|
|
|
}
|
|
|
|
|
2024-10-20 13:50:44 +07:00
|
|
|
err = setFieldValueEnv(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
|
2024-10-21 00:15:51 +07:00
|
|
|
func setFieldValueEnv(value reflect.Value, field reflect.StructField, prefix string) error {
|
|
|
|
envName := field.Tag.Get("env")
|
2017-12-08 20:02:26 +07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
if !value.CanSet() {
|
|
|
|
return fmt.Errorf("%s: can't be set", field.Name)
|
2017-12-08 20:02:26 +07:00
|
|
|
}
|
|
|
|
|
2017-12-11 16:14:53 +07:00
|
|
|
var err error
|
2024-10-21 00:15:51 +07:00
|
|
|
|
|
|
|
kind := value.Kind()
|
2017-12-08 20:02:26 +07:00
|
|
|
switch kind {
|
|
|
|
case reflect.Bool:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithBool(value, envValue)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.String:
|
2024-10-21 00:15:51 +07:00
|
|
|
value.SetString(envValue)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Int8:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithIntX(value, envValue, Int8Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Int16:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithIntX(value, envValue, Int16Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Int, reflect.Int32:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithIntX(value, envValue, Int32Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Int64:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithIntX(value, envValue, Int64Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Uint8:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithUintX(value, envValue, Uint8Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Uint16:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithUintX(value, envValue, Uint16Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Uint, reflect.Uint32:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithUintX(value, envValue, Uint32Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Uint64:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithUintX(value, envValue, Uint64Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Float32:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithFloatX(value, envValue, Float32Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
case reflect.Float64:
|
2024-10-21 00:15:51 +07:00
|
|
|
err = SetValueWithFloatX(value, envValue, Float64Size)
|
2017-12-08 20:02:26 +07:00
|
|
|
|
|
|
|
case reflect.Slice:
|
2024-10-21 00:15:51 +07:00
|
|
|
sp, ok := field.Tag.Lookup("separator")
|
2017-12-08 20:02:26 +07:00
|
|
|
if !ok {
|
|
|
|
sp = ":"
|
|
|
|
}
|
2024-10-21 00:15:51 +07:00
|
|
|
|
|
|
|
err = SetValueWithSlice(value, 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 {
|
2024-10-21 00:15:51 +07:00
|
|
|
return fmt.Errorf("%s: %s", field.Name, err.Error())
|
2017-12-08 20:02:26 +07:00
|
|
|
}
|
2024-10-21 00:15:51 +07:00
|
|
|
|
2017-12-08 20:02:26 +07:00
|
|
|
return nil
|
|
|
|
}
|