Utility function to initialize struct based on default tags

This commit is contained in:
Kovid Goyal
2023-07-02 09:30:01 +05:30
parent 471263b778
commit dee6210de5

View File

@@ -5,6 +5,8 @@ package utils
import (
"fmt"
"os"
"reflect"
"strconv"
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
@@ -216,3 +218,33 @@ func Concat[T any](slices ...[]T) []T {
}
return result
}
func SetStructDefaults(v reflect.Value) (err error) {
for _, field := range reflect.VisibleFields(v.Type()) {
if defval := field.Tag.Get("default"); defval != "" {
val := v.FieldByIndex(field.Index)
if val.CanSet() {
switch field.Type.Kind() {
case reflect.String:
if val.String() != "" {
val.SetString(defval)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if d, err := strconv.ParseInt(defval, 10, 64); err == nil {
val.SetInt(d)
} else {
return fmt.Errorf("Could not parse default value for struct field: %#v with error: %s", field.Name, err)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if d, err := strconv.ParseUint(defval, 10, 64); err == nil {
val.SetUint(d)
} else {
return fmt.Errorf("Could not parse default value for struct field: %#v with error: %s", field.Name, err)
}
}
}
}
}
return
}