2016-03-25 20:22:29 +00:00
|
|
|
// 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,
|
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
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2017-12-07 07:08:24 +00:00
|
|
|
"fmt"
|
2016-03-25 20:22:29 +00:00
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-07-09 09:40:30 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
|
|
|
|
|
|
|
"github.com/thejerf/suture"
|
2016-03-25 20:22:29 +00:00
|
|
|
)
|
|
|
|
|
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.
|
2019-02-12 06:58:24 +00:00
|
|
|
func SetDefaults(data interface{}) {
|
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)
|
|
|
|
|
|
|
|
case int:
|
|
|
|
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)
|
|
|
|
|
|
|
|
case float64:
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 07:08:24 +00:00
|
|
|
// CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
|
|
|
|
func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
|
|
|
|
fromStruct := reflect.ValueOf(from).Elem()
|
|
|
|
fromType := fromStruct.Type()
|
|
|
|
|
|
|
|
toStruct := reflect.ValueOf(to).Elem()
|
|
|
|
toType := toStruct.Type()
|
|
|
|
|
|
|
|
if fromType != toType {
|
|
|
|
panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < toStruct.NumField(); i++ {
|
|
|
|
fromField := fromStruct.Field(i)
|
|
|
|
toField := toStruct.Field(i)
|
|
|
|
|
2017-12-07 08:33:32 +00:00
|
|
|
if !toField.CanSet() {
|
|
|
|
// Unexported fields
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-12-07 07:08:24 +00:00
|
|
|
structTag := toType.Field(i).Tag
|
|
|
|
|
|
|
|
v := structTag.Get(tag)
|
|
|
|
if shouldCopy(v) {
|
|
|
|
toField.Set(fromField)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 07:56:40 +00:00
|
|
|
// UniqueTrimmedStrings returns a list on unique strings, trimming at the same time.
|
|
|
|
func UniqueTrimmedStrings(ss []string) []string {
|
|
|
|
// Trim all first
|
|
|
|
for i, v := range ss {
|
|
|
|
ss[i] = strings.Trim(v, " ")
|
2016-03-25 20:22:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 07:56:40 +00:00
|
|
|
var m = make(map[string]struct{}, len(ss))
|
|
|
|
var us = make([]string, 0, len(ss))
|
|
|
|
for _, v := range ss {
|
|
|
|
if _, ok := m[v]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m[v] = struct{}{}
|
|
|
|
us = append(us, v)
|
2016-03-25 20:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2019-07-09 09:40:30 +00:00
|
|
|
|
|
|
|
// AsService wraps the given function to implement suture.Service by calling
|
|
|
|
// that function on serve and closing the passed channel when Stop is called.
|
|
|
|
func AsService(fn func(stop chan struct{})) suture.Service {
|
|
|
|
return AsServiceWithError(func(stop chan struct{}) error {
|
|
|
|
fn(stop)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServiceWithError interface {
|
|
|
|
suture.Service
|
|
|
|
Error() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsServiceWithError does the same as AsService, except that it keeps track
|
|
|
|
// of an error returned by the given function.
|
|
|
|
func AsServiceWithError(fn func(stop chan struct{}) error) ServiceWithError {
|
|
|
|
s := &service{
|
|
|
|
serve: fn,
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
stopped: make(chan struct{}),
|
|
|
|
mut: sync.NewMutex(),
|
|
|
|
}
|
|
|
|
close(s.stopped) // not yet started, don't block on Stop()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
serve func(stop chan struct{}) error
|
|
|
|
stop chan struct{}
|
|
|
|
stopped chan struct{}
|
|
|
|
err error
|
|
|
|
mut sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Serve() {
|
|
|
|
s.mut.Lock()
|
|
|
|
select {
|
|
|
|
case <-s.stop:
|
|
|
|
s.mut.Unlock()
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
s.err = nil
|
|
|
|
s.stopped = make(chan struct{})
|
|
|
|
s.mut.Unlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
s.mut.Lock()
|
|
|
|
s.err = err
|
|
|
|
close(s.stopped)
|
|
|
|
s.mut.Unlock()
|
|
|
|
}()
|
|
|
|
err = s.serve(s.stop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Stop() {
|
|
|
|
s.mut.Lock()
|
|
|
|
close(s.stop)
|
|
|
|
s.mut.Unlock()
|
|
|
|
<-s.stopped
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Error() error {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
return s.err
|
|
|
|
}
|