utils.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package util
  7. import (
  8. "context"
  9. "fmt"
  10. "net/url"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "github.com/syncthing/syncthing/lib/sync"
  15. "github.com/thejerf/suture"
  16. )
  17. type defaultParser interface {
  18. ParseDefault(string) error
  19. }
  20. // SetDefaults sets default values on a struct, based on the default annotation.
  21. func SetDefaults(data interface{}) {
  22. s := reflect.ValueOf(data).Elem()
  23. t := s.Type()
  24. for i := 0; i < s.NumField(); i++ {
  25. f := s.Field(i)
  26. tag := t.Field(i).Tag
  27. v := tag.Get("default")
  28. if len(v) > 0 {
  29. if f.CanInterface() {
  30. if parser, ok := f.Interface().(defaultParser); ok {
  31. if err := parser.ParseDefault(v); err != nil {
  32. panic(err)
  33. }
  34. continue
  35. }
  36. }
  37. if f.CanAddr() && f.Addr().CanInterface() {
  38. if parser, ok := f.Addr().Interface().(defaultParser); ok {
  39. if err := parser.ParseDefault(v); err != nil {
  40. panic(err)
  41. }
  42. continue
  43. }
  44. }
  45. switch f.Interface().(type) {
  46. case string:
  47. f.SetString(v)
  48. case int:
  49. i, err := strconv.ParseInt(v, 10, 64)
  50. if err != nil {
  51. panic(err)
  52. }
  53. f.SetInt(i)
  54. case float64:
  55. i, err := strconv.ParseFloat(v, 64)
  56. if err != nil {
  57. panic(err)
  58. }
  59. f.SetFloat(i)
  60. case bool:
  61. f.SetBool(v == "true")
  62. case []string:
  63. // We don't do anything with string slices here. Any default
  64. // we set will be appended to by the XML decoder, so we fill
  65. // those after decoding.
  66. default:
  67. panic(f.Type())
  68. }
  69. }
  70. }
  71. }
  72. // CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
  73. func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
  74. fromStruct := reflect.ValueOf(from).Elem()
  75. fromType := fromStruct.Type()
  76. toStruct := reflect.ValueOf(to).Elem()
  77. toType := toStruct.Type()
  78. if fromType != toType {
  79. panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
  80. }
  81. for i := 0; i < toStruct.NumField(); i++ {
  82. fromField := fromStruct.Field(i)
  83. toField := toStruct.Field(i)
  84. if !toField.CanSet() {
  85. // Unexported fields
  86. continue
  87. }
  88. structTag := toType.Field(i).Tag
  89. v := structTag.Get(tag)
  90. if shouldCopy(v) {
  91. toField.Set(fromField)
  92. }
  93. }
  94. }
  95. // UniqueTrimmedStrings returns a list on unique strings, trimming at the same time.
  96. func UniqueTrimmedStrings(ss []string) []string {
  97. // Trim all first
  98. for i, v := range ss {
  99. ss[i] = strings.Trim(v, " ")
  100. }
  101. var m = make(map[string]struct{}, len(ss))
  102. var us = make([]string, 0, len(ss))
  103. for _, v := range ss {
  104. if _, ok := m[v]; ok {
  105. continue
  106. }
  107. m[v] = struct{}{}
  108. us = append(us, v)
  109. }
  110. return us
  111. }
  112. // FillNilSlices sets default value on slices that are still nil.
  113. func FillNilSlices(data interface{}) error {
  114. s := reflect.ValueOf(data).Elem()
  115. t := s.Type()
  116. for i := 0; i < s.NumField(); i++ {
  117. f := s.Field(i)
  118. tag := t.Field(i).Tag
  119. v := tag.Get("default")
  120. if len(v) > 0 {
  121. switch f.Interface().(type) {
  122. case []string:
  123. if f.IsNil() {
  124. // Treat the default as a comma separated slice
  125. vs := strings.Split(v, ",")
  126. for i := range vs {
  127. vs[i] = strings.TrimSpace(vs[i])
  128. }
  129. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
  130. for i, v := range vs {
  131. rv.Index(i).SetString(v)
  132. }
  133. f.Set(rv)
  134. }
  135. }
  136. }
  137. }
  138. return nil
  139. }
  140. // Address constructs a URL from the given network and hostname.
  141. func Address(network, host string) string {
  142. u := url.URL{
  143. Scheme: network,
  144. Host: host,
  145. }
  146. return u.String()
  147. }
  148. // AsService wraps the given function to implement suture.Service by calling
  149. // that function on serve and closing the passed channel when Stop is called.
  150. func AsService(fn func(ctx context.Context), creator string) suture.Service {
  151. return asServiceWithError(func(ctx context.Context) error {
  152. fn(ctx)
  153. return nil
  154. }, creator)
  155. }
  156. type ServiceWithError interface {
  157. suture.Service
  158. fmt.Stringer
  159. Error() error
  160. SetError(error)
  161. }
  162. // AsServiceWithError does the same as AsService, except that it keeps track
  163. // of an error returned by the given function.
  164. func AsServiceWithError(fn func(ctx context.Context) error, creator string) ServiceWithError {
  165. return asServiceWithError(fn, creator)
  166. }
  167. func asServiceWithError(fn func(ctx context.Context) error, creator string) ServiceWithError {
  168. ctx, cancel := context.WithCancel(context.Background())
  169. s := &service{
  170. serve: fn,
  171. ctx: ctx,
  172. cancel: cancel,
  173. stopped: make(chan struct{}),
  174. creator: creator,
  175. mut: sync.NewMutex(),
  176. }
  177. close(s.stopped) // not yet started, don't block on Stop()
  178. return s
  179. }
  180. type service struct {
  181. creator string
  182. serve func(ctx context.Context) error
  183. ctx context.Context
  184. cancel context.CancelFunc
  185. stopped chan struct{}
  186. err error
  187. mut sync.Mutex
  188. }
  189. func (s *service) Serve() {
  190. s.mut.Lock()
  191. select {
  192. case <-s.ctx.Done():
  193. s.mut.Unlock()
  194. return
  195. default:
  196. }
  197. s.err = nil
  198. s.stopped = make(chan struct{})
  199. s.mut.Unlock()
  200. var err error
  201. defer func() {
  202. if err == context.Canceled {
  203. err = nil
  204. }
  205. s.mut.Lock()
  206. s.err = err
  207. close(s.stopped)
  208. s.mut.Unlock()
  209. }()
  210. err = s.serve(s.ctx)
  211. }
  212. func (s *service) Stop() {
  213. s.mut.Lock()
  214. select {
  215. case <-s.ctx.Done():
  216. panic(fmt.Sprintf("Stop called more than once on %v", s))
  217. default:
  218. s.cancel()
  219. }
  220. s.mut.Unlock()
  221. <-s.stopped
  222. }
  223. func (s *service) Error() error {
  224. s.mut.Lock()
  225. defer s.mut.Unlock()
  226. return s.err
  227. }
  228. func (s *service) SetError(err error) {
  229. s.mut.Lock()
  230. s.err = err
  231. s.mut.Unlock()
  232. }
  233. func (s *service) String() string {
  234. return fmt.Sprintf("Service@%p created by %v", s, s.creator)
  235. }