lib/weakhash, lib/model, cmd/syncthing: Decide if to use weakhash on startup (fixes #3938)

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3949
This commit is contained in:
Audrius Butkevicius
2017-02-06 10:27:11 +00:00
parent 237893ead3
commit 67acef1794
12 changed files with 232 additions and 77 deletions

View File

@@ -25,6 +25,17 @@ func SetDefaults(data interface{}) error {
v := tag.Get("default")
if len(v) > 0 {
if parser, ok := f.Interface().(interface {
ParseDefault(string) (interface{}, error)
}); ok {
val, err := parser.ParseDefault(v)
if err != nil {
panic(err)
}
f.Set(reflect.ValueOf(val))
continue
}
switch f.Interface().(type) {
case string:
f.SetString(v)

View File

@@ -8,12 +8,21 @@ package util
import "testing"
type Defaulter struct {
Value string
}
func (Defaulter) ParseDefault(v string) (interface{}, error) {
return Defaulter{Value: v}, 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"`
A string `default:"string"`
B int `default:"2"`
C float64 `default:"2.2"`
D bool `default:"true"`
E Defaulter `default:"defaulter"`
}{}
if x.A != "" {
@@ -24,6 +33,8 @@ func TestSetDefaults(t *testing.T) {
t.Errorf("float failed")
} else if x.D {
t.Errorf("bool failed")
} else if x.E.Value != "" {
t.Errorf("defaulter failed")
}
if err := SetDefaults(x); err != nil {
@@ -38,6 +49,8 @@ func TestSetDefaults(t *testing.T) {
t.Errorf("float failed")
} else if !x.D {
t.Errorf("bool failed")
} else if x.E.Value != "defaulter" {
t.Errorf("defaulter failed")
}
}