2023-08-21 17:44:33 +00:00
|
|
|
// Copyright (C) 2023 The Syncthing Authors.
|
2016-03-25 20:22:29 +00:00
|
|
|
//
|
|
|
|
// 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-03-25 20:22:29 +00:00
|
|
|
|
2023-08-21 17:44:33 +00:00
|
|
|
package structutil
|
2016-03-25 20:22:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-02-12 06:58:24 +00:00
|
|
|
type defaultParser interface {
|
|
|
|
ParseDefault(string) error
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:22:29 +00:00
|
|
|
// SetDefaults sets default values on a struct, based on the default annotation.
|
2023-08-21 17:44:33 +00:00
|
|
|
func SetDefaults(data any) {
|
2016-03-25 20:22:29 +00:00
|
|
|
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 {
|
2019-02-12 06:58:24 +00:00
|
|
|
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
|
2017-02-06 10:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:22:29 +00:00
|
|
|
switch f.Interface().(type) {
|
|
|
|
case string:
|
|
|
|
f.SetString(v)
|
|
|
|
|
2020-08-25 06:11:14 +00:00
|
|
|
case int, uint32, int32, int64, uint64:
|
2016-03-25 20:22:29 +00:00
|
|
|
i, err := strconv.ParseInt(v, 10, 64)
|
|
|
|
if err != nil {
|
2019-02-12 06:58:24 +00:00
|
|
|
panic(err)
|
2016-03-25 20:22:29 +00:00
|
|
|
}
|
|
|
|
f.SetInt(i)
|
|
|
|
|
2020-08-25 06:11:14 +00:00
|
|
|
case float64, float32:
|
2016-03-25 20:22:29 +00:00
|
|
|
i, err := strconv.ParseFloat(v, 64)
|
|
|
|
if err != nil {
|
2019-02-12 06:58:24 +00:00
|
|
|
panic(err)
|
2016-03-25 20:22:29 +00:00
|
|
|
}
|
|
|
|
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())
|
|
|
|
}
|
2020-11-20 13:21:54 +00:00
|
|
|
} else if f.CanSet() && f.Kind() == reflect.Struct && f.CanAddr() {
|
|
|
|
if addr := f.Addr(); addr.CanInterface() {
|
|
|
|
SetDefaults(addr.Interface())
|
|
|
|
}
|
2016-03-25 20:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:44:33 +00:00
|
|
|
func FillNilExceptDeprecated(data any) {
|
2020-11-20 13:21:54 +00:00
|
|
|
fillNil(data, true)
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:44:33 +00:00
|
|
|
func FillNil(data any) {
|
2020-11-20 13:21:54 +00:00
|
|
|
fillNil(data, false)
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:44:33 +00:00
|
|
|
func fillNil(data any, skipDeprecated bool) {
|
2020-06-28 18:35:22 +00:00
|
|
|
s := reflect.ValueOf(data).Elem()
|
2020-11-20 13:21:54 +00:00
|
|
|
t := s.Type()
|
2020-06-28 18:35:22 +00:00
|
|
|
for i := 0; i < s.NumField(); i++ {
|
2020-11-20 13:21:54 +00:00
|
|
|
if skipDeprecated && strings.HasPrefix(t.Field(i).Name, "Deprecated") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-28 18:35:22 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-20 13:21:54 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-06-28 18:35:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:22:29 +00:00
|
|
|
// FillNilSlices sets default value on slices that are still nil.
|
2023-08-21 17:44:33 +00:00
|
|
|
func FillNilSlices(data any) error {
|
2016-03-25 20:22:29 +00:00
|
|
|
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
|
|
|
|
}
|