all: Remove lib/util package (#9049)
Grab-bag packages are nasty, this cleans it up a little by splitting it into topical packages sempahore, netutil, stringutil, structutil.
This commit is contained in:
@@ -0,0 +1,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
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
// Copyright (C) 2016 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 (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Defaulter struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func (d *Defaulter) ParseDefault(v string) error {
|
||||
*d = Defaulter{Value: v}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestSetDefaults(t *testing.T) {
|
||||
x := &struct {
|
||||
A string `default:"string"`
|
||||
B int `default:"2"`
|
||||
C float64 `default:"2.2"`
|
||||
D bool `default:"true"`
|
||||
E Defaulter `default:"defaulter"`
|
||||
}{}
|
||||
|
||||
if x.A != "" {
|
||||
t.Error("string failed")
|
||||
} else if x.B != 0 {
|
||||
t.Error("int failed")
|
||||
} else if x.C != 0 {
|
||||
t.Errorf("float failed")
|
||||
} else if x.D {
|
||||
t.Errorf("bool failed")
|
||||
} else if x.E.Value != "" {
|
||||
t.Errorf("defaulter failed")
|
||||
}
|
||||
|
||||
SetDefaults(x)
|
||||
|
||||
if x.A != "string" {
|
||||
t.Error("string failed")
|
||||
} else if x.B != 2 {
|
||||
t.Error("int failed")
|
||||
} else if x.C != 2.2 {
|
||||
t.Errorf("float failed")
|
||||
} else if !x.D {
|
||||
t.Errorf("bool failed")
|
||||
} else if x.E.Value != "defaulter" {
|
||||
t.Errorf("defaulter failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFillNillSlices(t *testing.T) {
|
||||
// Nil
|
||||
x := &struct {
|
||||
A []string `default:"a,b"`
|
||||
}{}
|
||||
|
||||
if x.A != nil {
|
||||
t.Error("not nil")
|
||||
}
|
||||
|
||||
if err := FillNilSlices(x); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(x.A) != 2 {
|
||||
t.Error("length")
|
||||
}
|
||||
|
||||
// Already provided
|
||||
y := &struct {
|
||||
A []string `default:"c,d,e"`
|
||||
}{[]string{"a", "b"}}
|
||||
|
||||
if len(y.A) != 2 {
|
||||
t.Error("length")
|
||||
}
|
||||
|
||||
if err := FillNilSlices(y); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(y.A) != 2 {
|
||||
t.Error("length")
|
||||
}
|
||||
|
||||
// Non-nil but empty
|
||||
z := &struct {
|
||||
A []string `default:"c,d,e"`
|
||||
}{[]string{}}
|
||||
|
||||
if len(z.A) != 0 {
|
||||
t.Error("length")
|
||||
}
|
||||
|
||||
if err := FillNilSlices(z); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if len(z.A) != 0 {
|
||||
t.Error("length")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFillNil(t *testing.T) {
|
||||
type A struct {
|
||||
Slice []int
|
||||
Map map[string]string
|
||||
Chan chan int
|
||||
}
|
||||
|
||||
type B struct {
|
||||
Slice *[]int
|
||||
Map *map[string]string
|
||||
Chan *chan int
|
||||
}
|
||||
|
||||
type C struct {
|
||||
A A
|
||||
B *B
|
||||
D *****[]int
|
||||
}
|
||||
|
||||
c := C{}
|
||||
FillNil(&c)
|
||||
|
||||
if c.A.Slice == nil {
|
||||
t.Error("c.A.Slice")
|
||||
}
|
||||
if c.A.Map == nil {
|
||||
t.Error("c.A.Slice")
|
||||
}
|
||||
if c.A.Chan == nil {
|
||||
t.Error("c.A.Chan")
|
||||
}
|
||||
if c.B == nil {
|
||||
t.Error("c.B")
|
||||
}
|
||||
if c.B.Slice == nil {
|
||||
t.Error("c.B.Slice")
|
||||
}
|
||||
if c.B.Map == nil {
|
||||
t.Error("c.B.Slice")
|
||||
}
|
||||
if c.B.Chan == nil {
|
||||
t.Error("c.B.Chan")
|
||||
}
|
||||
if *c.B.Slice == nil {
|
||||
t.Error("*c.B.Slice")
|
||||
}
|
||||
if *c.B.Map == nil {
|
||||
t.Error("*c.B.Slice")
|
||||
}
|
||||
if *c.B.Chan == nil {
|
||||
t.Error("*c.B.Chan")
|
||||
}
|
||||
if *****c.D == nil {
|
||||
t.Error("c.D")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFillNilDoesNotBulldozeSetFields(t *testing.T) {
|
||||
type A struct {
|
||||
Slice []int
|
||||
Map map[string]string
|
||||
Chan chan int
|
||||
}
|
||||
|
||||
type B struct {
|
||||
Slice *[]int
|
||||
Map *map[string]string
|
||||
Chan *chan int
|
||||
}
|
||||
|
||||
type C struct {
|
||||
A A
|
||||
B *B
|
||||
D **[]int
|
||||
}
|
||||
|
||||
ch := make(chan int, 10)
|
||||
d := make([]int, 10)
|
||||
dd := &d
|
||||
|
||||
c := C{
|
||||
A: A{
|
||||
Slice: []int{1},
|
||||
Map: map[string]string{
|
||||
"k": "v",
|
||||
},
|
||||
Chan: make(chan int, 10),
|
||||
},
|
||||
B: &B{
|
||||
Slice: &[]int{1},
|
||||
Map: &map[string]string{
|
||||
"k": "v",
|
||||
},
|
||||
Chan: &ch,
|
||||
},
|
||||
D: &dd,
|
||||
}
|
||||
FillNil(&c)
|
||||
|
||||
if len(c.A.Slice) != 1 {
|
||||
t.Error("c.A.Slice")
|
||||
}
|
||||
if len(c.A.Map) != 1 {
|
||||
t.Error("c.A.Slice")
|
||||
}
|
||||
if cap(c.A.Chan) != 10 {
|
||||
t.Error("c.A.Chan")
|
||||
}
|
||||
if c.B == nil {
|
||||
t.Error("c.B")
|
||||
}
|
||||
if len(*c.B.Slice) != 1 {
|
||||
t.Error("c.B.Slice")
|
||||
}
|
||||
if len(*c.B.Map) != 1 {
|
||||
t.Error("c.B.Slice")
|
||||
}
|
||||
if cap(*c.B.Chan) != 10 {
|
||||
t.Error("c.B.Chan")
|
||||
}
|
||||
if cap(**c.D) != 10 {
|
||||
t.Error("c.D")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user