Skip to content
Permalink
Browse files

Removed duplicated code

  • Loading branch information...
everettcaleb committed Feb 9, 2019
1 parent 183f316 commit 567436245a43102bc9d597983ff4656e7a6826a2
Showing with 16 additions and 40 deletions.
  1. +16 −40 getenv.go
@@ -10,30 +10,17 @@ import (

var env = os.Getenv

func setStringFromEnv(v reflect.Value, name string) bool {
s := env(name)
if len(s) == 0 {
return false
}
func setStringFromEnv(v reflect.Value, s string) bool {
v.SetString(s)
return true
}

func setStringSliceFromEnv(v reflect.Value, name string) bool {
s := env(name)
if len(s) == 0 {
return false
}
func setStringSliceFromEnv(v reflect.Value, s string) bool {
v.Set(reflect.ValueOf(strings.Split(s, ":")))
return true
}

func setIntFromEnv(v reflect.Value, name string) (bool, error) {
s := env(name)
if len(s) == 0 {
return false, nil
}

func setIntFromEnv(v reflect.Value, s string) (bool, error) {
val, err := strconv.ParseInt(s, 10, bitsOf(v.Kind()))
if err != nil {
return false, err
@@ -42,12 +29,7 @@ func setIntFromEnv(v reflect.Value, name string) (bool, error) {
return true, nil
}

func setUintFromEnv(v reflect.Value, name string) (bool, error) {
s := env(name)
if len(s) == 0 {
return false, nil
}

func setUintFromEnv(v reflect.Value, s string) (bool, error) {
val, err := strconv.ParseUint(s, 10, bitsOf(v.Kind()))
if err != nil {
return false, err
@@ -56,12 +38,7 @@ func setUintFromEnv(v reflect.Value, name string) (bool, error) {
return true, nil
}

func setFloatFromEnv(v reflect.Value, name string) (bool, error) {
s := env(name)
if len(s) == 0 {
return false, nil
}

func setFloatFromEnv(v reflect.Value, s string) (bool, error) {
val, err := strconv.ParseFloat(s, bitsOf(v.Kind()))
if err != nil {
return false, err
@@ -70,12 +47,7 @@ func setFloatFromEnv(v reflect.Value, name string) (bool, error) {
return true, nil
}

func setBoolFromEnv(v reflect.Value, name string) (bool, error) {
s := env(name)
if len(s) == 0 {
return false, nil
}

func setBoolFromEnv(v reflect.Value, s string) (bool, error) {
b, err := parseFriendlyBool(s)
if err != nil {
return false, err
@@ -89,34 +61,38 @@ func setBoolFromEnv(v reflect.Value, name string) (bool, error) {
// Second return value is an error for if something went wrong (invalid format for parsing, etc)
func setValueFromEnv(v reflect.Value, name string) (bool, error) {
k := v.Kind()
s := env(name)
if len(s) == 0 {
return false, nil
}

switch k {
case reflect.Bool:
return setBoolFromEnv(v, name)
return setBoolFromEnv(v, s)

case reflect.Float32, reflect.Float64:
return setFloatFromEnv(v, name)
return setFloatFromEnv(v, s)

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return setIntFromEnv(v, name)
return setIntFromEnv(v, s)

case reflect.Ptr:
return false, fmt.Errorf("invalid kind, pointer-to-pointer is not supported and single pointer is resolved by unmarshalStructValue")

case reflect.Slice:
if v.Type().Elem().Kind() == reflect.String {
return setStringSliceFromEnv(v, name), nil
return setStringSliceFromEnv(v, s), nil
}
return false, fmt.Errorf("invalid kind, for slices only string slices are currently supported")

case reflect.String:
return setStringFromEnv(v, name), nil
return setStringFromEnv(v, s), nil

case reflect.Struct:
return false, fmt.Errorf("invalid kind, struct must be processed by unmarshalStructValue")

case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return setUintFromEnv(v, name)
return setUintFromEnv(v, s)

default:
return false, fmt.Errorf("invalid kind")

0 comments on commit 5674362

Please sign in to comment.
You can’t perform that action at this time.