-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.go
More file actions
80 lines (69 loc) · 1.49 KB
/
result.go
File metadata and controls
80 lines (69 loc) · 1.49 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
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
package gofeat
import "fmt"
type Result struct {
values map[string]any
}
func newResult(values map[string]any) Result {
return Result{values: values}
}
func (r Result) Int(name string) (int, error) {
v, ok := r.values[name]
if !ok {
return 0, fmt.Errorf("feature %q not found", name)
}
i, ok := v.(int)
if !ok {
return 0, fmt.Errorf("feature %q: expected int, got %T", name, v)
}
return i, nil
}
func (r Result) IntOr(name string, defaultValue int) int {
v, err := r.Int(name)
if err != nil {
return defaultValue
}
return v
}
func (r Result) Float(name string) (float64, error) {
v, ok := r.values[name]
if !ok {
return 0, fmt.Errorf("feature %q not found", name)
}
f, ok := v.(float64)
if !ok {
return 0, fmt.Errorf("feature %q: expected float64, got %T", name, v)
}
return f, nil
}
func (r Result) FloatOr(name string, defaultValue float64) float64 {
v, err := r.Float(name)
if err != nil {
return defaultValue
}
return v
}
func (r Result) String(name string) (string, error) {
v, ok := r.values[name]
if !ok {
return "", fmt.Errorf("feature %q not found", name)
}
s, ok := v.(string)
if !ok {
return "", fmt.Errorf("feature %q: expected string, got %T", name, v)
}
return s, nil
}
func (r Result) StringOr(name, defaultValue string) string {
v, err := r.String(name)
if err != nil {
return defaultValue
}
return v
}
func (r Result) Any(name string) (any, bool) {
v, ok := r.values[name]
return v, ok
}
func (r Result) All() map[string]any {
return r.values
}