-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynstringset.go
46 lines (37 loc) · 1.29 KB
/
dynstringset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2015 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package dflag
import (
"flag"
"fmt"
"fortio.org/sets"
)
// DynStringSet creates a `Flag` that represents `map[string]struct{}` which is safe to change dynamically at runtime.
// Unlike `pflag.StringSlice`, consecutive sets don't append to the slice, but override it.
func DynStringSet(flagSet *flag.FlagSet, name string, value []string, usage string) *DynStringSetValue {
d := Dyn(flagSet, name, sets.FromSlice(value), usage)
return &DynStringSetValue{d}
}
// In order to have methods unique to this subtype... we extend/have the generic instantiated type:
// DynStringSetValue implements a dynamic set of strings.
type DynStringSetValue struct {
*DynValue[sets.Set[string]]
}
// Contains returns whether the specified string is in the flag.
func (d *DynStringSetValue) Contains(val string) bool {
v := d.Get()
_, ok := v[val]
return ok
}
// String represents the canonical representation of the type.
func (d *DynStringSetValue) String() string {
v := d.Get()
arr := make([]string, 0, len(v))
for k := range v {
arr = append(arr, k)
}
return fmt.Sprintf("%v", arr)
}
func ValidateDynStringSetMinElements(count int) func(sets.Set[string]) error {
return ValidateDynSetMinElements[string](count)
}