urlsmap.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package types
  15. import (
  16. "fmt"
  17. "sort"
  18. "strings"
  19. )
  20. // URLsMap is a map from a name to its URLs.
  21. type URLsMap map[string]URLs
  22. // NewURLsMap returns a URLsMap instantiated from the given string,
  23. // which consists of discovery-formatted names-to-URLs, like:
  24. // mach0=http://1.1.1.1:2380,mach0=http://2.2.2.2::2380,mach1=http://3.3.3.3:2380,mach2=http://4.4.4.4:2380
  25. func NewURLsMap(s string) (URLsMap, error) {
  26. m := parse(s)
  27. cl := URLsMap{}
  28. for name, urls := range m {
  29. us, err := NewURLs(urls)
  30. if err != nil {
  31. return nil, err
  32. }
  33. cl[name] = us
  34. }
  35. return cl, nil
  36. }
  37. // NewURLsMapFromStringMap takes a map of strings and returns a URLsMap. The
  38. // string values in the map can be multiple values separated by the sep string.
  39. func NewURLsMapFromStringMap(m map[string]string, sep string) (URLsMap, error) {
  40. var err error
  41. um := URLsMap{}
  42. for k, v := range m {
  43. um[k], err = NewURLs(strings.Split(v, sep))
  44. if err != nil {
  45. return nil, err
  46. }
  47. }
  48. return um, nil
  49. }
  50. // String turns URLsMap into discovery-formatted name-to-URLs sorted by name.
  51. func (c URLsMap) String() string {
  52. var pairs []string
  53. for name, urls := range c {
  54. for _, url := range urls {
  55. pairs = append(pairs, fmt.Sprintf("%s=%s", name, url.String()))
  56. }
  57. }
  58. sort.Strings(pairs)
  59. return strings.Join(pairs, ",")
  60. }
  61. // URLs returns a list of all URLs.
  62. // The returned list is sorted in ascending lexicographical order.
  63. func (c URLsMap) URLs() []string {
  64. var urls []string
  65. for _, us := range c {
  66. for _, u := range us {
  67. urls = append(urls, u.String())
  68. }
  69. }
  70. sort.Strings(urls)
  71. return urls
  72. }
  73. // Len returns the size of URLsMap.
  74. func (c URLsMap) Len() int {
  75. return len(c)
  76. }
  77. // parse parses the given string and returns a map listing the values specified for each key.
  78. func parse(s string) map[string][]string {
  79. m := make(map[string][]string)
  80. for s != "" {
  81. key := s
  82. if i := strings.IndexAny(key, ","); i >= 0 {
  83. key, s = key[:i], key[i+1:]
  84. } else {
  85. s = ""
  86. }
  87. if key == "" {
  88. continue
  89. }
  90. value := ""
  91. if i := strings.Index(key, "="); i >= 0 {
  92. key, value = key[:i], key[i+1:]
  93. }
  94. m[key] = append(m[key], value)
  95. }
  96. return m
  97. }