choices.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2019 Yunion
  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 choices
  15. import (
  16. "strings"
  17. )
  18. type Empty struct{}
  19. type Choices map[string]Empty
  20. func NewChoices(choices ...string) Choices {
  21. cs := Choices{}
  22. for _, choice := range choices {
  23. cs[choice] = Empty{}
  24. }
  25. return cs
  26. }
  27. func (cs Choices) Has(choice string) bool {
  28. _, ok := cs[choice]
  29. return ok
  30. }
  31. func (cs Choices) String() string {
  32. choices := make([]string, len(cs))
  33. i := 0
  34. for choice := range cs {
  35. choices[i] = choice
  36. i++
  37. }
  38. return strings.Join(choices, "|")
  39. }