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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
// Copyright (C) 2023 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package structutil
import (
"reflect"
"strconv"
"strings"
)
type defaultParser interface {
ParseDefault(string) error
}
// SetDefaults sets default values on a struct, based on the default annotation.
func SetDefaults(data any) {
s := reflect.ValueOf(data).Elem()
t := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
tag := t.Field(i).Tag
v := tag.Get("default")
if len(v) > 0 {
if f.CanInterface() {
if parser, ok := f.Interface().(defaultParser); ok {
if err := parser.ParseDefault(v); err != nil {
panic(err)
}
continue
}
}
if f.CanAddr() && f.Addr().CanInterface() {
if parser, ok := f.Addr().Interface().(defaultParser); ok {
if err := parser.ParseDefault(v); err != nil {
panic(err)
}
continue
}
}
switch f.Interface().(type) {
case string:
f.SetString(v)
case int, uint32, int32, int64, uint64:
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
panic(err)
}
f.SetInt(i)
case float64, float32:
i, err := strconv.ParseFloat(v, 64)
if err != nil {
panic(err)
}
f.SetFloat(i)
case bool:
f.SetBool(v == "true")
case []string:
// We don't do anything with string slices here. Any default
// we set will be appended to by the XML decoder, so we fill
// those after decoding.
default:
panic(f.Type())
}
} else if f.CanSet() && f.Kind() == reflect.Struct && f.CanAddr() {
if addr := f.Addr(); addr.CanInterface() {
SetDefaults(addr.Interface())
}
}
}
}
func FillNilExceptDeprecated(data any) {
fillNil(data, true)
}
func FillNil(data any) {
fillNil(data, false)
}
func fillNil(data any, skipDeprecated bool) {
s := reflect.ValueOf(data).Elem()
t := s.Type()
for i := 0; i < s.NumField(); i++ {
if skipDeprecated && strings.HasPrefix(t.Field(i).Name, "Deprecated") {
continue
}
f := s.Field(i)
for f.Kind() == reflect.Ptr && f.IsZero() && f.CanSet() {
newValue := reflect.New(f.Type().Elem())
f.Set(newValue)
f = f.Elem()
}
if f.CanSet() {
if f.IsZero() {
switch f.Kind() {
case reflect.Map:
f.Set(reflect.MakeMap(f.Type()))
case reflect.Slice:
f.Set(reflect.MakeSlice(f.Type(), 0, 0))
case reflect.Chan:
f.Set(reflect.MakeChan(f.Type(), 0))
}
}
switch f.Kind() {
case reflect.Slice:
if f.Type().Elem().Kind() != reflect.Struct {
continue
}
for i := 0; i < f.Len(); i++ {
fillNil(f.Index(i).Addr().Interface(), skipDeprecated)
}
case reflect.Struct:
if f.CanAddr() {
if addr := f.Addr(); addr.CanInterface() {
fillNil(addr.Interface(), skipDeprecated)
}
}
}
}
}
}
// FillNilSlices sets default value on slices that are still nil.
func FillNilSlices(data any) error {
s := reflect.ValueOf(data).Elem()
t := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
tag := t.Field(i).Tag
v := tag.Get("default")
if len(v) > 0 {
switch f.Interface().(type) {
case []string:
if f.IsNil() {
// Treat the default as a comma separated slice
vs := strings.Split(v, ",")
for i := range vs {
vs[i] = strings.TrimSpace(vs[i])
}
rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
for i, v := range vs {
rv.Index(i).SetString(v)
}
f.Set(rv)
}
}
}
}
return nil
}
|