selective_string.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2018 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 flags
  15. import (
  16. "errors"
  17. "fmt"
  18. "sort"
  19. "strings"
  20. )
  21. // SelectiveStringValue implements the flag.Value interface.
  22. type SelectiveStringValue struct {
  23. v string
  24. valids map[string]struct{}
  25. }
  26. // Set verifies the argument to be a valid member of the allowed values
  27. // before setting the underlying flag value.
  28. func (ss *SelectiveStringValue) Set(s string) error {
  29. if _, ok := ss.valids[s]; ok {
  30. ss.v = s
  31. return nil
  32. }
  33. return errors.New("invalid value")
  34. }
  35. // String returns the set value (if any) of the SelectiveStringValue
  36. func (ss *SelectiveStringValue) String() string {
  37. return ss.v
  38. }
  39. // Valids returns the list of valid strings.
  40. func (ss *SelectiveStringValue) Valids() []string {
  41. s := make([]string, 0, len(ss.valids))
  42. for k := range ss.valids {
  43. s = append(s, k)
  44. }
  45. sort.Strings(s)
  46. return s
  47. }
  48. // NewSelectiveStringValue creates a new string flag
  49. // for which any one of the given strings is a valid value,
  50. // and any other value is an error.
  51. //
  52. // valids[0] will be default value. Caller must be sure
  53. // len(valids) != 0 or it will panic.
  54. func NewSelectiveStringValue(valids ...string) *SelectiveStringValue {
  55. vm := make(map[string]struct{})
  56. for _, v := range valids {
  57. vm[v] = struct{}{}
  58. }
  59. return &SelectiveStringValue{valids: vm, v: valids[0]}
  60. }
  61. // SelectiveStringsValue implements the flag.Value interface.
  62. type SelectiveStringsValue struct {
  63. vs []string
  64. valids map[string]struct{}
  65. }
  66. // Set verifies the argument to be a valid member of the allowed values
  67. // before setting the underlying flag value.
  68. func (ss *SelectiveStringsValue) Set(s string) error {
  69. vs := strings.Split(s, ",")
  70. for i := range vs {
  71. if _, ok := ss.valids[vs[i]]; ok {
  72. ss.vs = append(ss.vs, vs[i])
  73. } else {
  74. return fmt.Errorf("invalid value %q", vs[i])
  75. }
  76. }
  77. sort.Strings(ss.vs)
  78. return nil
  79. }
  80. // String returns the set value (if any) of the SelectiveStringsValue.
  81. func (ss *SelectiveStringsValue) String() string {
  82. return strings.Join(ss.vs, ",")
  83. }
  84. // Valids returns the list of valid strings.
  85. func (ss *SelectiveStringsValue) Valids() []string {
  86. s := make([]string, 0, len(ss.valids))
  87. for k := range ss.valids {
  88. s = append(s, k)
  89. }
  90. sort.Strings(s)
  91. return s
  92. }
  93. // NewSelectiveStringsValue creates a new string slice flag
  94. // for which any one of the given strings is a valid value,
  95. // and any other value is an error.
  96. func NewSelectiveStringsValue(valids ...string) *SelectiveStringsValue {
  97. vm := make(map[string]struct{})
  98. for _, v := range valids {
  99. vm[v] = struct{}{}
  100. }
  101. return &SelectiveStringsValue{valids: vm, vs: []string{}}
  102. }