lib/upnp: Refactor out methods to util with tests, refactor IGD
This commit is contained in:
committed by
Jakob Borg
parent
6a3f3f5577
commit
1d17891286
119
lib/util/utils.go
Normal file
119
lib/util/utils.go
Normal file
@@ -0,0 +1,119 @@
|
||||
// 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 http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SetDefaults sets default values on a struct, based on the default annotation.
|
||||
func SetDefaults(data interface{}) 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:
|
||||
f.SetString(v)
|
||||
|
||||
case int:
|
||||
i, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.SetInt(i)
|
||||
|
||||
case float64:
|
||||
i, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
return 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())
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UniqueStrings returns a list on unique strings, trimming and sorting them
|
||||
// at the same time.
|
||||
func UniqueStrings(ss []string) []string {
|
||||
var m = make(map[string]bool, len(ss))
|
||||
for _, s := range ss {
|
||||
m[strings.Trim(s, " ")] = true
|
||||
}
|
||||
|
||||
var us = make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
us = append(us, k)
|
||||
}
|
||||
|
||||
sort.Strings(us)
|
||||
|
||||
return us
|
||||
}
|
||||
|
||||
// FillNilSlices sets default value on slices that are still nil.
|
||||
func FillNilSlices(data interface{}) 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
|
||||
}
|
||||
|
||||
// Address constructs a URL from the given network and hostname.
|
||||
func Address(network, host string) string {
|
||||
u := url.URL{
|
||||
Scheme: network,
|
||||
Host: host,
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
158
lib/util/utils_test.go
Normal file
158
lib/util/utils_test.go
Normal file
@@ -0,0 +1,158 @@
|
||||
// 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 http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package util
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSetDefaults(t *testing.T) {
|
||||
x := &struct {
|
||||
A string `default:"string"`
|
||||
B int `default:"2"`
|
||||
C float64 `default:"2.2"`
|
||||
D bool `default:"true"`
|
||||
}{}
|
||||
|
||||
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 != false {
|
||||
t.Errorf("bool failed")
|
||||
}
|
||||
|
||||
if err := SetDefaults(x); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
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 != true {
|
||||
t.Errorf("bool failed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUniqueStrings(t *testing.T) {
|
||||
tests := []struct {
|
||||
input []string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
[]string{"a", "b"},
|
||||
[]string{"a", "b"},
|
||||
},
|
||||
{
|
||||
[]string{"a", "a"},
|
||||
[]string{"a"},
|
||||
},
|
||||
{
|
||||
[]string{"a", "a", "a", "a"},
|
||||
[]string{"a"},
|
||||
},
|
||||
{
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
[]string{"b", "a"},
|
||||
[]string{"a", "b"},
|
||||
},
|
||||
{
|
||||
[]string{" a ", " a ", "b ", " b"},
|
||||
[]string{"a", "b"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := UniqueStrings(test.input)
|
||||
if len(result) != len(test.expected) {
|
||||
t.Errorf("%s != %s", result, test.expected)
|
||||
}
|
||||
for i := range result {
|
||||
if test.expected[i] != result[i] {
|
||||
t.Errorf("%s != %s", result, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 TestAddress(t *testing.T) {
|
||||
tests := []struct {
|
||||
network string
|
||||
host string
|
||||
result string
|
||||
}{
|
||||
{"tcp", "google.com", "tcp://google.com"},
|
||||
{"foo", "google", "foo://google"},
|
||||
{"123", "456", "123://456"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result := Address(test.network, test.host)
|
||||
if result != test.result {
|
||||
t.Errorf("%s != %s", result, test.result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user