utils.go 3.5 KB

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