-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinput.go
More file actions
108 lines (97 loc) · 2.66 KB
/
input.go
File metadata and controls
108 lines (97 loc) · 2.66 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package trial
import (
"fmt"
"reflect"
"strconv"
)
// Input the input value given to the trial test function
type Input struct { // TODO: try type Input interface{}
value reflect.Value
}
func newInput(i interface{}) Input {
return Input{value: reflect.ValueOf(i)}
}
// String value of input, panics on on non string value
func (in Input) String() string {
switch in.value.Kind() {
case reflect.Struct, reflect.Ptr, reflect.Slice, reflect.Map, reflect.Array, reflect.Chan:
panic("unsupported string conversion " + in.value.Kind().String())
default:
return fmt.Sprintf("%v", in.Interface())
}
}
// Bool value of input, panics on non bool value
func (in Input) Bool() bool {
if in.value.Kind() == reflect.String {
b, err := strconv.ParseBool(in.value.String())
if err != nil {
panic("invalid bool " + in.value.Interface().(string))
}
return b
}
return in.value.Bool()
}
// Int value of input, panics on non int value
func (in Input) Int() int {
switch in.value.Kind() {
case reflect.String:
i, err := strconv.Atoi(in.value.String())
if err != nil {
panic("invalid int " + in.value.Interface().(string))
}
return i
}
return int(in.value.Int())
}
// Uint value of input, panics on non uint value
func (in Input) Uint() uint {
switch in.value.Kind() {
case reflect.Int:
return uint(in.value.Int())
case reflect.String:
u, err := strconv.ParseUint(in.value.String(), 10, 64)
if err != nil {
panic("invalid uint " + in.value.Interface().(string))
}
return uint(u)
default:
return uint(in.value.Uint())
}
}
// Interface returns the current value of input
func (in Input) Interface() interface{} {
//TODO: check for nil
if in.value.Kind() == reflect.Invalid {
return nil
}
return in.value.Interface()
}
// Float64 value of input, panics on non float64 value
func (in Input) Float64() float64 {
switch in.value.Kind() {
case reflect.Int:
return float64(in.value.Int())
case reflect.String:
f, err := strconv.ParseFloat(in.value.String(), 64)
if err != nil {
panic("invalid float64 " + in.value.Interface().(string))
}
return f
default:
return in.value.Float()
}
}
// Slice returns the input value of the index of a slice/array. panics if non slice value
func (in Input) Slice(i int) Input {
// use reflect to access any slice type []int, etc
v := in.value.Index(i)
if v.Kind() == reflect.Interface {
return Input{value: reflect.ValueOf(v.Interface())}
}
return Input{value: v}
}
// Map returns the value for the provided key, panics on non map value
func (in Input) Map(key interface{}) Input {
// use reflection to access any map type map[string]string, etc
return Input{value: in.value.MapIndex(reflect.ValueOf(key))}
}