-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoconfigenv.go
135 lines (115 loc) · 3.14 KB
/
goconfigenv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package goconfigenv
import (
"fmt"
"github.com/joho/godotenv"
"os"
"reflect"
"slices"
"strconv"
"strings"
)
// Tags : env
// Example : `env:"NAME"`
// Validations :-
// default - sets the default value if the environment variable if not present
// ParseEnv parses the environment variables and return after setting the values to the struct
func ParseEnv[T any]() (T, error) {
// Loading the .env file if exists
if envFileExist := checkFileExists(".env"); envFileExist {
if err := godotenv.Overload(".env"); err != nil {
return reflect.Zero(reflect.TypeFor[T]()).Interface().(T), err
}
}
// Creating a zero instance of the struct
zero := reflect.Zero(reflect.TypeFor[T]()).Interface().(T)
// Creating a new instance of the struct
instance := reflect.New(reflect.TypeFor[T]()).Elem()
// Checking if the type is a struct
if err := checkForTypeStruct[T](); err != nil {
return zero, err
}
// Setting the struct fields
err := setStructFields(&instance)
if err != nil {
return zero, err
}
return instance.Interface().(T), nil
}
func setStructFields(instance *reflect.Value) error {
typ := instance.Type()
for i := 0; i < typ.NumField(); i++ {
tag := typ.Field(i).Tag.Get("env")
fieldType := typ.Field(i).Type
params := getAllParameters(tag)
envFieldName := params[0]
envValue := os.Getenv(envFieldName)
// Checking for default value
defaultIdx := slices.IndexFunc(params, func(s string) bool {
return strings.Contains(s, "default=")
})
if envValue == "" || !checkEnvFound(envFieldName) {
if defaultIdx != -1 {
defaultVal := strings.Trim(strings.SplitN(params[defaultIdx], "default=", 2)[1], " ")
if err := setFieldValue(instance, i, fieldType, defaultVal); err != nil {
return err
}
continue
}
}
// Set all the field values
if err := setFieldValue(instance, i, fieldType, envValue); err != nil {
return err
}
}
return nil
}
func checkForTypeStruct[T any]() error {
if reflect.TypeFor[T]().Kind() != reflect.Struct {
return fmt.Errorf("type is not a struct")
}
return nil
}
func getAllParameters(str string) []string {
return strings.Split(str, ",")
}
func checkEnvFound(env string) bool {
if _, ok := os.LookupEnv(env); !ok {
return false
}
return true
}
func setFieldValue(instance *reflect.Value, i int, fieldType reflect.Type, envValue string) error {
switch fieldType.Kind() {
case reflect.String:
instance.Field(i).SetString(envValue)
case reflect.Int:
intVal, err := strconv.Atoi(envValue)
if err != nil {
return err
}
instance.Field(i).SetInt(int64(intVal))
case reflect.Bool:
boolVal, err := strconv.ParseBool(envValue)
if err != nil {
return err
}
instance.Field(i).SetBool(boolVal)
case reflect.Struct:
nestedVal := instance.Field(i)
err := setStructFields(&nestedVal)
if err != nil {
return err
}
default:
return fmt.Errorf("unsupported type %s", fieldType.Kind())
}
return nil
}
// checkFileExists checks if a file exists and is not a directory before we try using it to prevent further errors.
func checkFileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}