This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdefault_source.go
92 lines (81 loc) · 2.21 KB
/
default_source.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package configo
import (
"net/url"
"strconv"
"time"
)
// DefaultSource is allows registration of specified default values of various types.
type DefaultSource struct {
settings map[string][]string
}
// NewDefaultSource initializes a new DefaultSource.
func NewDefaultSource(pairs ...DefaultPair) *DefaultSource {
source := &DefaultSource{settings: make(map[string][]string)}
for _, config := range pairs {
config(source)
}
return source
}
type DefaultPair func(*DefaultSource)
// Default registers the provided values (which will be converted to strings) to the given key.
// It does NOT overwrite existing values, it appends.
func Default(key string, values ...interface{}) DefaultPair {
return func(source *DefaultSource) {
contents := source.settings[key]
for _, value := range values {
contents = append(contents, convertToString(value))
}
source.settings[key] = contents
}
}
func convertToString(value interface{}) string {
switch typed := value.(type) {
case string:
return typed
case bool:
return strconv.FormatBool(typed)
case int:
return strconv.FormatInt(int64(typed), 10)
case int8:
return strconv.FormatInt(int64(typed), 10)
case int16:
return strconv.FormatInt(int64(typed), 10)
case int32:
return strconv.FormatInt(int64(typed), 10)
case int64:
return strconv.FormatInt(typed, 10)
case uint:
return strconv.FormatUint(uint64(typed), 10)
case uint8:
return strconv.FormatUint(uint64(typed), 10)
case uint16:
return strconv.FormatUint(uint64(typed), 10)
case uint32:
return strconv.FormatUint(uint64(typed), 10)
case uint64:
return strconv.FormatUint(typed, 10)
case float32:
return strconv.FormatFloat(float64(typed), 'f', -1, 32)
case float64:
return strconv.FormatFloat(typed, 'f', -1, 64)
case *url.URL:
return typed.String()
case url.URL:
return typed.String()
case time.Duration:
return typed.String()
case time.Time:
return typed.String()
default:
return ""
}
}
// Strings returns all values associated with the given key, or ErrKeyNotFound.
func (this *DefaultSource) Strings(key string) ([]string, error) {
values, found := this.settings[key]
if !found {
return nil, ErrKeyNotFound
}
return values, nil
}
func (this *DefaultSource) Initialize() {}