utils.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "fmt"
  9. "net/url"
  10. "reflect"
  11. "strconv"
  12. "strings"
  13. )
  14. type defaultParser interface {
  15. ParseDefault(string) error
  16. }
  17. // SetDefaults sets default values on a struct, based on the default annotation.
  18. func SetDefaults(data interface{}) {
  19. s := reflect.ValueOf(data).Elem()
  20. t := s.Type()
  21. for i := 0; i < s.NumField(); i++ {
  22. f := s.Field(i)
  23. tag := t.Field(i).Tag
  24. v := tag.Get("default")
  25. if len(v) > 0 {
  26. if f.CanInterface() {
  27. if parser, ok := f.Interface().(defaultParser); ok {
  28. if err := parser.ParseDefault(v); err != nil {
  29. panic(err)
  30. }
  31. continue
  32. }
  33. }
  34. if f.CanAddr() && f.Addr().CanInterface() {
  35. if parser, ok := f.Addr().Interface().(defaultParser); ok {
  36. if err := parser.ParseDefault(v); err != nil {
  37. panic(err)
  38. }
  39. continue
  40. }
  41. }
  42. switch f.Interface().(type) {
  43. case string:
  44. f.SetString(v)
  45. case int:
  46. i, err := strconv.ParseInt(v, 10, 64)
  47. if err != nil {
  48. panic(err)
  49. }
  50. f.SetInt(i)
  51. case float64:
  52. i, err := strconv.ParseFloat(v, 64)
  53. if err != nil {
  54. panic(err)
  55. }
  56. f.SetFloat(i)
  57. case bool:
  58. f.SetBool(v == "true")
  59. case []string:
  60. // We don't do anything with string slices here. Any default
  61. // we set will be appended to by the XML decoder, so we fill
  62. // those after decoding.
  63. default:
  64. panic(f.Type())
  65. }
  66. }
  67. }
  68. }
  69. // CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
  70. func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
  71. fromStruct := reflect.ValueOf(from).Elem()
  72. fromType := fromStruct.Type()
  73. toStruct := reflect.ValueOf(to).Elem()
  74. toType := toStruct.Type()
  75. if fromType != toType {
  76. panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
  77. }
  78. for i := 0; i < toStruct.NumField(); i++ {
  79. fromField := fromStruct.Field(i)
  80. toField := toStruct.Field(i)
  81. if !toField.CanSet() {
  82. // Unexported fields
  83. continue
  84. }
  85. structTag := toType.Field(i).Tag
  86. v := structTag.Get(tag)
  87. if shouldCopy(v) {
  88. toField.Set(fromField)
  89. }
  90. }
  91. }
  92. // UniqueTrimmedStrings returns a list on unique strings, trimming at the same time.
  93. func UniqueTrimmedStrings(ss []string) []string {
  94. // Trim all first
  95. for i, v := range ss {
  96. ss[i] = strings.Trim(v, " ")
  97. }
  98. var m = make(map[string]struct{}, len(ss))
  99. var us = make([]string, 0, len(ss))
  100. for _, v := range ss {
  101. if _, ok := m[v]; ok {
  102. continue
  103. }
  104. m[v] = struct{}{}
  105. us = append(us, v)
  106. }
  107. return us
  108. }
  109. // FillNilSlices sets default value on slices that are still nil.
  110. func FillNilSlices(data interface{}) error {
  111. s := reflect.ValueOf(data).Elem()
  112. t := s.Type()
  113. for i := 0; i < s.NumField(); i++ {
  114. f := s.Field(i)
  115. tag := t.Field(i).Tag
  116. v := tag.Get("default")
  117. if len(v) > 0 {
  118. switch f.Interface().(type) {
  119. case []string:
  120. if f.IsNil() {
  121. // Treat the default as a comma separated slice
  122. vs := strings.Split(v, ",")
  123. for i := range vs {
  124. vs[i] = strings.TrimSpace(vs[i])
  125. }
  126. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
  127. for i, v := range vs {
  128. rv.Index(i).SetString(v)
  129. }
  130. f.Set(rv)
  131. }
  132. }
  133. }
  134. }
  135. return nil
  136. }
  137. // Address constructs a URL from the given network and hostname.
  138. func Address(network, host string) string {
  139. u := url.URL{
  140. Scheme: network,
  141. Host: host,
  142. }
  143. return u.String()
  144. }