utils.go 2.6 KB

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