Skip to content

Commit

Permalink
adding the slog
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacwein committed Sep 9, 2024
1 parent 760f7ed commit 927e677
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 28 deletions.
18 changes: 2 additions & 16 deletions cmd/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,9 @@ type State struct {
EnvFile string `json:"envFile" flag:"env-file, default:.env,usage:to change the default env file to load" env:"-"`
}

func setUpFlagsTest(log *slog.Logger) {
log = log.With("test", "setUpFlagsTest")
state := State{}
err := envparser.SetUpFlags(".", &state)
if err != nil {
log.Error("SetUpFlags returned an error", "error", err)
}

log.Debug("state", "state", state)
}

func setUpEnvTest(log *slog.Logger) {

log = log.With("test", "setUpEnvTest")
log = log.With("test", "SetUpFlagEnv")
os.Setenv("CUSTOMER_ID", "1")
os.Setenv("START", "2")
os.Setenv("END", "3")
Expand All @@ -46,13 +35,11 @@ func setUpEnvTest(log *slog.Logger) {
os.Setenv("LOG_DB.CONN_MAX_LIFE_TIME", "4")

state := State{}
err := envparser.SetUpEnv(".", &state)
err := envparser.SetUpFlagEnv(".", &state)
if err != nil {
log.Error("SetUpEnv returned an error", "error", err)
}

log.Debug("state", "state", state)

}

func main() {
Expand All @@ -61,6 +48,5 @@ func main() {
slog.SetDefault(log)
envparser.SetLogger(log)
defer log.Info("exiting")
setUpFlagsTest(log)
setUpEnvTest(log)
}
68 changes: 56 additions & 12 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func setUpFlags(prefix, env, splitter string, p any) error {
return fmt.Errorf("error: unsupported tag %q for field %q\n", s, field.Name)
}
}
suppertedTypes := []string{"string", "int", "int64", "uint", "uint64", "bool", "struct"}

supportedTypes := []string{"string", "int", "int64", "uint", "uint64", "bool", "struct"}
log = log.With("flag-usage", flagUsage, "flag-default", flagDefaultValue)

if fieldsEnv != "" && fieldsEnv != "-" {
Expand All @@ -101,8 +102,13 @@ func setUpFlags(prefix, env, splitter string, p any) error {
filedType := field.Type.Kind()
var err error
switch filedType {

case reflect.String:
flag.StringVar(value.(*string), flagKey, flagDefaultValue, flagUsage)
tempValue := ""
flag.StringVar(&tempValue, flagKey, flagDefaultValue, flagUsage)
if tempValue != "" {
fieldInfo.SetString(tempValue)
}
case reflect.Int:
var i int
if flagDefaultValue != "" {
Expand All @@ -112,7 +118,11 @@ func setUpFlags(prefix, env, splitter string, p any) error {
return fmt.Errorf("error parsing default to type %q value for field %q: %s", filedType, field.Name, err)
}
}
flag.IntVar(value.(*int), flagKey, i, flagUsage)
var tempValue int
flag.IntVar(&tempValue, flagKey, i, flagUsage)
if tempValue != 0 {
fieldInfo.Set(reflect.ValueOf(tempValue))
}
case reflect.Int64:
var i int64

Expand All @@ -123,7 +133,11 @@ func setUpFlags(prefix, env, splitter string, p any) error {
return fmt.Errorf("error parsing default to type %q value for field %q: %s", filedType, field.Name, err)
}
}
flag.Int64Var(value.(*int64), flagKey, i, flagUsage)
var tempValue int64
flag.Int64Var(&tempValue, flagKey, i, flagUsage)
if tempValue != 0 {
fieldInfo.SetInt(tempValue)
}
case reflect.Uint:
var i uint
if flagDefaultValue != "" {
Expand All @@ -134,7 +148,11 @@ func setUpFlags(prefix, env, splitter string, p any) error {
}
i = uint(ii)
}
flag.UintVar(value.(*uint), flagKey, i, flagUsage)
var tempValue uint
flag.UintVar(&tempValue, flagKey, i, flagUsage)
if tempValue != 0 {
fieldInfo.Set(reflect.ValueOf(tempValue))
}
case reflect.Uint64:
var i uint64

Expand All @@ -145,7 +163,12 @@ func setUpFlags(prefix, env, splitter string, p any) error {
return fmt.Errorf("error parsing default to type %q value for field %q: %s", filedType, field.Name, err)
}
}
flag.Uint64Var(value.(*uint64), flagKey, i, flagUsage)
var tempValue uint64
flag.Uint64Var(&tempValue, flagKey, i, flagUsage)
if tempValue != 0 {
fieldInfo.SetUint(tempValue)
}

case reflect.Bool:
var b bool
if flagDefaultValue != "" {
Expand All @@ -155,16 +178,19 @@ func setUpFlags(prefix, env, splitter string, p any) error {
return fmt.Errorf("error parsing default to type %q value for field %q: %s", filedType, field.Name, err)
}
}

flag.BoolVar(value.(*bool), flagKey, b, flagUsage)
var tempValue bool
flag.BoolVar(&tempValue, flagKey, b, flagUsage)
if tempValue {
fieldInfo.SetBool(tempValue)
}
case reflect.Struct:
err = setUpFlags(flagKey, fieldsEnv, splitter, value)
if err != nil {
return err
}

default:
log.Warn("unsupported type", "supported types", suppertedTypes, "suggest", `"you can ignore it by flag:"-"`)
log.Warn("unsupported type", "supported types", supportedTypes, "suggest", `"you can ignore it by flag:"-"`)
continue
}

Expand Down Expand Up @@ -270,7 +296,7 @@ func setUpEnv(prefix, splitter string, p any) error {
log.Error("error parsing env to type", "error", err)
return fmt.Errorf("%s: %w", errorMessage, err)
}
fieldInfo.Set(reflect.ValueOf(i))
fieldInfo.SetInt(i)
case reflect.Uint:
i, err := strconv.ParseUint(envValue, 10, 64)
if err != nil {
Expand Down Expand Up @@ -298,14 +324,14 @@ func setUpEnv(prefix, splitter string, p any) error {
log.Error("error parsing env to type", "error", err)
return fmt.Errorf("%s: %w", errorMessage, err)
}
fieldInfo.Set(reflect.ValueOf(i))
fieldInfo.SetUint(i)
case reflect.Bool:
b, err := strconv.ParseBool(envValue)
if err != nil {
log.Error("error parsing env to type", "error", err)
return fmt.Errorf("%s: %w", errorMessage, err)
}
fieldInfo.Set(reflect.ValueOf(b))
fieldInfo.SetBool(b)
case reflect.Struct:

err := setUpEnv(fieldsEnv, splitter, value)
Expand All @@ -323,3 +349,21 @@ func setUpEnv(prefix, splitter string, p any) error {

return nil
}

// SetUpFlagEnv set up the flag and env for the given struct
// first it will set up the flag, and then it will set up the env
func SetUpFlagEnv(splitter string, p any) error {
err := SetUpEnv(splitter, p)
if err != nil {
log.Error("error setting up env", "error", err)
return err
}

err = SetUpFlags(splitter, p)
if err != nil {
log.Error("error setting up flag", "error", err)
return err
}

return err
}

0 comments on commit 927e677

Please sign in to comment.