Skip to content

Commit c7e6668

Browse files
committed
feat: better number conversion support
1 parent 74140e5 commit c7e6668

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

cli/param.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const (
2020
StyleForm
2121
)
2222

23+
func typeConvert(from, to interface{}) interface{} {
24+
return reflect.ValueOf(from).Convert(reflect.TypeOf(to)).Interface()
25+
}
26+
2327
// Param represents an API operation input parameter.
2428
type Param struct {
2529
Type string `json:"type"`
@@ -113,12 +117,12 @@ func (p Param) AddFlag(flags *pflag.FlagSet) interface{} {
113117
if def == nil {
114118
def = 0
115119
}
116-
return flags.Int(name, int(def.(float64)), p.Description)
120+
return flags.Int(name, typeConvert(def, 0).(int), p.Description)
117121
case "number":
118122
if def == nil {
119123
def = 0.0
120124
}
121-
return flags.Float64(name, def.(float64), p.Description)
125+
return flags.Float64(name, typeConvert(def, float64(0.0)).(float64), p.Description)
122126
case "string":
123127
if def == nil {
124128
def = ""

0 commit comments

Comments
 (0)