-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption-types.go
134 lines (105 loc) · 3.05 KB
/
option-types.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cmdline
import (
"fmt"
"path/filepath"
"strconv"
)
type OptionTypes interface {
StringToAttributes(typeName string, spec string) *OptionTypeAttributes
MakeValue(typeIndex int, inputValue string) (any, error)
NewList(typeIndex int) (any, error)
AppendList(typeIndex int, list any, inputValue string) (any, error)
}
type OptionTypeAttributes struct {
Index int
DefaultValue any
}
type argType int
const (
argTypeBool argType = iota
argTypeInt
argTypeFloat64
argTypeString
argTypePath
)
type DefaultOptionTypes struct {
}
// Returns the OptionTypes interface for bool, int, float64, string and path. The lastIndex
// helps the caller know what the type index range is (0..lastIndex), to extend with
// custom types in a wrapper interface.
func NewDefaultOptionTypes() (dot *DefaultOptionTypes, lastIndex int) {
dot = &DefaultOptionTypes{}
lastIndex = int(argTypePath) + 1
return
}
func (dot *DefaultOptionTypes) StringToAttributes(typeName string, spec string) *OptionTypeAttributes {
switch typeName {
case "bool":
return &OptionTypeAttributes{Index: int(argTypeBool), DefaultValue: bool(false)}
case "int":
return &OptionTypeAttributes{Index: int(argTypeInt), DefaultValue: int(0)}
case "float64":
return &OptionTypeAttributes{Index: int(argTypeFloat64), DefaultValue: float64(0)}
case "string":
return &OptionTypeAttributes{Index: int(argTypeString), DefaultValue: ""}
case "path":
return &OptionTypeAttributes{Index: int(argTypePath), DefaultValue: ""}
default:
panic(fmt.Errorf("%svalid arg type %s in %s", basePanic, typeName, spec))
}
}
func (dot *DefaultOptionTypes) MakeValue(typeIndex int, inputValue string) (any, error) {
var result any
var err error
switch argType(typeIndex) {
case argTypeBool:
result, err = strconv.ParseBool(inputValue)
case argTypeInt:
result, err = strconv.Atoi(inputValue)
case argTypeFloat64:
result, err = strconv.ParseFloat(inputValue, 64)
case argTypeString:
result = inputValue
err = nil
case argTypePath:
result, err = filepath.Abs(inputValue)
default:
panic(fmt.Errorf("invalid arg type index"))
}
return result, err
}
func (dot *DefaultOptionTypes) NewList(typeIndex int) (any, error) {
switch argType(typeIndex) {
case argTypeBool:
return []bool{}, nil
case argTypeInt:
return []int{}, nil
case argTypeFloat64:
return []float64{}, nil
case argTypeString:
return []string{}, nil
case argTypePath:
return []string{}, nil
default:
panic(fmt.Errorf("invalid arg type index"))
}
}
func (dot *DefaultOptionTypes) AppendList(typeIndex int, list any, inputValue string) (any, error) {
value, err := dot.MakeValue(typeIndex, inputValue)
if err != nil {
return nil, err
}
switch argType(typeIndex) {
case argTypeBool:
list = append(list.([]bool), value.(bool))
case argTypeInt:
list = append(list.([]int), value.(int))
case argTypeFloat64:
list = append(list.([]float64), value.(float64))
case argTypeString:
list = append(list.([]string), value.(string))
case argTypePath:
list = append(list.([]string), value.(string))
}
return list, nil
}