utils.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. "time"
  15. )
  16. type defaultParser interface {
  17. ParseDefault(string) error
  18. }
  19. // SetDefaults sets default values on a struct, based on the default annotation.
  20. func SetDefaults(data interface{}) {
  21. s := reflect.ValueOf(data).Elem()
  22. t := s.Type()
  23. for i := 0; i < s.NumField(); i++ {
  24. f := s.Field(i)
  25. tag := t.Field(i).Tag
  26. v := tag.Get("default")
  27. if len(v) > 0 {
  28. if f.CanInterface() {
  29. if parser, ok := f.Interface().(defaultParser); ok {
  30. if err := parser.ParseDefault(v); err != nil {
  31. panic(err)
  32. }
  33. continue
  34. }
  35. }
  36. if f.CanAddr() && f.Addr().CanInterface() {
  37. if parser, ok := f.Addr().Interface().(defaultParser); ok {
  38. if err := parser.ParseDefault(v); err != nil {
  39. panic(err)
  40. }
  41. continue
  42. }
  43. }
  44. switch f.Interface().(type) {
  45. case string:
  46. f.SetString(v)
  47. case int, uint32, int32, int64, uint64:
  48. i, err := strconv.ParseInt(v, 10, 64)
  49. if err != nil {
  50. panic(err)
  51. }
  52. f.SetInt(i)
  53. case float64, float32:
  54. i, err := strconv.ParseFloat(v, 64)
  55. if err != nil {
  56. panic(err)
  57. }
  58. f.SetFloat(i)
  59. case bool:
  60. f.SetBool(v == "true")
  61. case []string:
  62. // We don't do anything with string slices here. Any default
  63. // we set will be appended to by the XML decoder, so we fill
  64. // those after decoding.
  65. default:
  66. panic(f.Type())
  67. }
  68. } else if f.CanSet() && f.Kind() == reflect.Struct && f.CanAddr() {
  69. if addr := f.Addr(); addr.CanInterface() {
  70. SetDefaults(addr.Interface())
  71. }
  72. }
  73. }
  74. }
  75. // CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
  76. func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
  77. fromStruct := reflect.ValueOf(from).Elem()
  78. fromType := fromStruct.Type()
  79. toStruct := reflect.ValueOf(to).Elem()
  80. toType := toStruct.Type()
  81. if fromType != toType {
  82. panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
  83. }
  84. for i := 0; i < toStruct.NumField(); i++ {
  85. fromField := fromStruct.Field(i)
  86. toField := toStruct.Field(i)
  87. if !toField.CanSet() {
  88. // Unexported fields
  89. continue
  90. }
  91. structTag := toType.Field(i).Tag
  92. v := structTag.Get(tag)
  93. if shouldCopy(v) {
  94. toField.Set(fromField)
  95. }
  96. }
  97. }
  98. // UniqueTrimmedStrings returns a list of all unique strings in ss,
  99. // in the order in which they first appear in ss, after trimming away
  100. // leading and trailing spaces.
  101. func UniqueTrimmedStrings(ss []string) []string {
  102. var m = make(map[string]struct{}, len(ss))
  103. var us = make([]string, 0, len(ss))
  104. for _, v := range ss {
  105. v = strings.Trim(v, " ")
  106. if _, ok := m[v]; ok {
  107. continue
  108. }
  109. m[v] = struct{}{}
  110. us = append(us, v)
  111. }
  112. return us
  113. }
  114. func FillNilExceptDeprecated(data interface{}) {
  115. fillNil(data, true)
  116. }
  117. func FillNil(data interface{}) {
  118. fillNil(data, false)
  119. }
  120. func fillNil(data interface{}, skipDeprecated bool) {
  121. s := reflect.ValueOf(data).Elem()
  122. t := s.Type()
  123. for i := 0; i < s.NumField(); i++ {
  124. if skipDeprecated && strings.HasPrefix(t.Field(i).Name, "Deprecated") {
  125. continue
  126. }
  127. f := s.Field(i)
  128. for f.Kind() == reflect.Ptr && f.IsZero() && f.CanSet() {
  129. newValue := reflect.New(f.Type().Elem())
  130. f.Set(newValue)
  131. f = f.Elem()
  132. }
  133. if f.CanSet() {
  134. if f.IsZero() {
  135. switch f.Kind() {
  136. case reflect.Map:
  137. f.Set(reflect.MakeMap(f.Type()))
  138. case reflect.Slice:
  139. f.Set(reflect.MakeSlice(f.Type(), 0, 0))
  140. case reflect.Chan:
  141. f.Set(reflect.MakeChan(f.Type(), 0))
  142. }
  143. }
  144. switch f.Kind() {
  145. case reflect.Slice:
  146. if f.Type().Elem().Kind() != reflect.Struct {
  147. continue
  148. }
  149. for i := 0; i < f.Len(); i++ {
  150. fillNil(f.Index(i).Addr().Interface(), skipDeprecated)
  151. }
  152. case reflect.Struct:
  153. if f.CanAddr() {
  154. if addr := f.Addr(); addr.CanInterface() {
  155. fillNil(addr.Interface(), skipDeprecated)
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. // FillNilSlices sets default value on slices that are still nil.
  163. func FillNilSlices(data interface{}) error {
  164. s := reflect.ValueOf(data).Elem()
  165. t := s.Type()
  166. for i := 0; i < s.NumField(); i++ {
  167. f := s.Field(i)
  168. tag := t.Field(i).Tag
  169. v := tag.Get("default")
  170. if len(v) > 0 {
  171. switch f.Interface().(type) {
  172. case []string:
  173. if f.IsNil() {
  174. // Treat the default as a comma separated slice
  175. vs := strings.Split(v, ",")
  176. for i := range vs {
  177. vs[i] = strings.TrimSpace(vs[i])
  178. }
  179. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
  180. for i, v := range vs {
  181. rv.Index(i).SetString(v)
  182. }
  183. f.Set(rv)
  184. }
  185. }
  186. }
  187. }
  188. return nil
  189. }
  190. // Address constructs a URL from the given network and hostname.
  191. func Address(network, host string) string {
  192. u := url.URL{
  193. Scheme: network,
  194. Host: host,
  195. }
  196. return u.String()
  197. }
  198. func CallWithContext(ctx context.Context, fn func() error) error {
  199. var err error
  200. done := make(chan struct{})
  201. go func() {
  202. err = fn()
  203. close(done)
  204. }()
  205. select {
  206. case <-done:
  207. return err
  208. case <-ctx.Done():
  209. return ctx.Err()
  210. }
  211. }
  212. func NiceDurationString(d time.Duration) string {
  213. switch {
  214. case d > 24*time.Hour:
  215. d = d.Round(time.Hour)
  216. case d > time.Hour:
  217. d = d.Round(time.Minute)
  218. case d > time.Minute:
  219. d = d.Round(time.Second)
  220. case d > time.Second:
  221. d = d.Round(time.Millisecond)
  222. case d > time.Millisecond:
  223. d = d.Round(time.Microsecond)
  224. }
  225. return d.String()
  226. }
  227. func EqualStrings(a, b []string) bool {
  228. if len(a) != len(b) {
  229. return false
  230. }
  231. for i := range a {
  232. if a[i] != b[i] {
  233. return false
  234. }
  235. }
  236. return true
  237. }