@@ -20,6 +20,7 @@ import (
20
20
"bytes"
21
21
"encoding/json"
22
22
"fmt"
23
+ "strconv"
23
24
"time"
24
25
)
25
26
@@ -82,12 +83,22 @@ func getInt(dict map[string]interface{}, key string, defaultValue int, defaultEr
82
83
return defaultValue , defaultError
83
84
}
84
85
86
+ intValue , ok := value .(int )
87
+ if ok {
88
+ return intValue , nil
89
+ }
85
90
// Types from interface{} create from json cannot be cast directly to int.
86
91
floatValue , ok := value .(float64 )
87
- if ! ok {
88
- return 0 , fmt .Errorf ("type assertion error: %v of is not an int" , value )
92
+ if ok {
93
+ return int (floatValue ), nil
94
+ }
95
+ stringValue , ok := value .(string )
96
+ if ok {
97
+ if i , err := strconv .Atoi (stringValue ); err != nil {
98
+ return i , nil
99
+ }
89
100
}
90
- return int ( floatValue ), nil
101
+ return 0 , fmt . Errorf ( "type assertion error: %v of is not an int" , value )
91
102
}
92
103
93
104
func getFloat64 (dict map [string ]interface {}, key string , defaultValue float64 , defaultError error ) (float64 , error ) {
@@ -97,10 +108,16 @@ func getFloat64(dict map[string]interface{}, key string, defaultValue float64, d
97
108
}
98
109
99
110
floatValue , ok := value .(float64 )
100
- if ! ok {
101
- return 0 , fmt .Errorf ("type assertion error: %v of is not an int" , value )
111
+ if ok {
112
+ return floatValue , nil
113
+ }
114
+ stringValue , ok := value .(string )
115
+ if ok {
116
+ if f , err := strconv .ParseFloat (stringValue , 64 ); err != nil {
117
+ return f , nil
118
+ }
102
119
}
103
- return floatValue , nil
120
+ return 0 , fmt . Errorf ( "type assertion error: %v of is not a float" , value )
104
121
}
105
122
106
123
func getDuration (dict map [string ]interface {}, key string , defaultValue time.Duration , defaultError error ) (time.Duration , error ) {
0 commit comments