-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathz_string.go
More file actions
52 lines (41 loc) · 1.18 KB
/
Copy pathz_string.go
File metadata and controls
52 lines (41 loc) · 1.18 KB
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
47
48
49
50
51
52
package zerocfg
import "encoding/json"
type stringValue string
func newStringValue(val string, p *string) Value {
*p = val
return (*stringValue)(p)
}
func (s *stringValue) Set(val string) error {
*s = stringValue(val)
return nil
}
func (s *stringValue) Type() string {
return "string"
}
// Str registers a string configuration option and returns a pointer to its value.
//
// Usage:
//
// username := zerocfg.Str("db.user", "guest", "user of database")
func Str(name string, defVal string, desc string, opts ...OptNode) *string {
return Any(name, defVal, desc, newStringValue, opts...)
}
type stringSliceValue []string
func newStringSlice(val []string, p *[]string) Value {
*p = val
return (*stringSliceValue)(p)
}
func (s *stringSliceValue) Set(val string) error {
return json.Unmarshal([]byte(val), s)
}
func (s *stringSliceValue) Type() string {
return "strings"
}
// Strs registers a slice of string configuration options and returns a pointer to its value.
//
// Usage:
//
// hosts := zerocfg.Strs("hosts", []string{"a", "b"}, "list of hosts")
func Strs(name string, defVal []string, desc string, opts ...OptNode) *[]string {
return Any(name, defVal, desc, newStringSlice, opts...)
}