utils.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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"
  11. "net/url"
  12. "reflect"
  13. "strconv"
  14. "strings"
  15. "time"
  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, uint32, int32, int64, uint64:
  49. i, err := strconv.ParseInt(v, 10, 64)
  50. if err != nil {
  51. panic(err)
  52. }
  53. f.SetInt(i)
  54. case float64, float32:
  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. } else if f.CanSet() && f.Kind() == reflect.Struct && f.CanAddr() {
  70. if addr := f.Addr(); addr.CanInterface() {
  71. SetDefaults(addr.Interface())
  72. }
  73. }
  74. }
  75. }
  76. // CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
  77. func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
  78. fromStruct := reflect.ValueOf(from).Elem()
  79. fromType := fromStruct.Type()
  80. toStruct := reflect.ValueOf(to).Elem()
  81. toType := toStruct.Type()
  82. if fromType != toType {
  83. panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
  84. }
  85. for i := 0; i < toStruct.NumField(); i++ {
  86. fromField := fromStruct.Field(i)
  87. toField := toStruct.Field(i)
  88. if !toField.CanSet() {
  89. // Unexported fields
  90. continue
  91. }
  92. structTag := toType.Field(i).Tag
  93. v := structTag.Get(tag)
  94. if shouldCopy(v) {
  95. toField.Set(fromField)
  96. }
  97. }
  98. }
  99. // UniqueTrimmedStrings returns a list of all unique strings in ss,
  100. // in the order in which they first appear in ss, after trimming away
  101. // leading and trailing spaces.
  102. func UniqueTrimmedStrings(ss []string) []string {
  103. var m = make(map[string]struct{}, len(ss))
  104. var us = make([]string, 0, len(ss))
  105. for _, v := range ss {
  106. v = strings.Trim(v, " ")
  107. if _, ok := m[v]; ok {
  108. continue
  109. }
  110. m[v] = struct{}{}
  111. us = append(us, v)
  112. }
  113. return us
  114. }
  115. func FillNilExceptDeprecated(data interface{}) {
  116. fillNil(data, true)
  117. }
  118. func FillNil(data interface{}) {
  119. fillNil(data, false)
  120. }
  121. func fillNil(data interface{}, skipDeprecated bool) {
  122. s := reflect.ValueOf(data).Elem()
  123. t := s.Type()
  124. for i := 0; i < s.NumField(); i++ {
  125. if skipDeprecated && strings.HasPrefix(t.Field(i).Name, "Deprecated") {
  126. continue
  127. }
  128. f := s.Field(i)
  129. for f.Kind() == reflect.Ptr && f.IsZero() && f.CanSet() {
  130. newValue := reflect.New(f.Type().Elem())
  131. f.Set(newValue)
  132. f = f.Elem()
  133. }
  134. if f.CanSet() {
  135. if f.IsZero() {
  136. switch f.Kind() {
  137. case reflect.Map:
  138. f.Set(reflect.MakeMap(f.Type()))
  139. case reflect.Slice:
  140. f.Set(reflect.MakeSlice(f.Type(), 0, 0))
  141. case reflect.Chan:
  142. f.Set(reflect.MakeChan(f.Type(), 0))
  143. }
  144. }
  145. switch f.Kind() {
  146. case reflect.Slice:
  147. if f.Type().Elem().Kind() != reflect.Struct {
  148. continue
  149. }
  150. for i := 0; i < f.Len(); i++ {
  151. fillNil(f.Index(i).Addr().Interface(), skipDeprecated)
  152. }
  153. case reflect.Struct:
  154. if f.CanAddr() {
  155. if addr := f.Addr(); addr.CanInterface() {
  156. fillNil(addr.Interface(), skipDeprecated)
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. // FillNilSlices sets default value on slices that are still nil.
  164. func FillNilSlices(data interface{}) error {
  165. s := reflect.ValueOf(data).Elem()
  166. t := s.Type()
  167. for i := 0; i < s.NumField(); i++ {
  168. f := s.Field(i)
  169. tag := t.Field(i).Tag
  170. v := tag.Get("default")
  171. if len(v) > 0 {
  172. switch f.Interface().(type) {
  173. case []string:
  174. if f.IsNil() {
  175. // Treat the default as a comma separated slice
  176. vs := strings.Split(v, ",")
  177. for i := range vs {
  178. vs[i] = strings.TrimSpace(vs[i])
  179. }
  180. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
  181. for i, v := range vs {
  182. rv.Index(i).SetString(v)
  183. }
  184. f.Set(rv)
  185. }
  186. }
  187. }
  188. }
  189. return nil
  190. }
  191. // Address constructs a URL from the given network and hostname.
  192. func Address(network, host string) string {
  193. u := url.URL{
  194. Scheme: network,
  195. Host: host,
  196. }
  197. return u.String()
  198. }
  199. // AddressUnspecifiedLess is a comparator function preferring least specific network address (most widely listening,
  200. // namely preferring 0.0.0.0 over some IP), if both IPs are equal, it prefers the less restrictive network (prefers tcp
  201. // over tcp4)
  202. func AddressUnspecifiedLess(a, b net.Addr) bool {
  203. aIsUnspecified := false
  204. bIsUnspecified := false
  205. if host, _, err := net.SplitHostPort(a.String()); err == nil {
  206. aIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  207. }
  208. if host, _, err := net.SplitHostPort(b.String()); err == nil {
  209. bIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  210. }
  211. if aIsUnspecified == bIsUnspecified {
  212. return len(a.Network()) < len(b.Network())
  213. }
  214. return aIsUnspecified
  215. }
  216. type ExitStatus int
  217. const (
  218. ExitSuccess ExitStatus = 0
  219. ExitError ExitStatus = 1
  220. ExitNoUpgradeAvailable ExitStatus = 2
  221. ExitRestart ExitStatus = 3
  222. ExitUpgrade ExitStatus = 4
  223. )
  224. func (s ExitStatus) AsInt() int {
  225. return int(s)
  226. }
  227. // OnDone calls fn when ctx is cancelled.
  228. func OnDone(ctx context.Context, fn func()) {
  229. go func() {
  230. <-ctx.Done()
  231. fn()
  232. }()
  233. }
  234. func CallWithContext(ctx context.Context, fn func() error) error {
  235. var err error
  236. done := make(chan struct{})
  237. go func() {
  238. err = fn()
  239. close(done)
  240. }()
  241. select {
  242. case <-done:
  243. return err
  244. case <-ctx.Done():
  245. return ctx.Err()
  246. }
  247. }
  248. func NiceDurationString(d time.Duration) string {
  249. switch {
  250. case d > 24*time.Hour:
  251. d = d.Round(time.Hour)
  252. case d > time.Hour:
  253. d = d.Round(time.Minute)
  254. case d > time.Minute:
  255. d = d.Round(time.Second)
  256. case d > time.Second:
  257. d = d.Round(time.Millisecond)
  258. case d > time.Millisecond:
  259. d = d.Round(time.Microsecond)
  260. }
  261. return d.String()
  262. }