utils.go 2.4 KB

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