@@ -1000,36 +1000,57 @@ func ToStringE(i interface{}) (string, error) {
1000
1000
}
1001
1001
}
1002
1002
1003
- // ToStringMapStringE casts an interface to a map[string]string type.
1004
- func ToStringMapStringE (i interface {}) (map [string ]string , error ) {
1005
- m := map [string ]string {}
1003
+ func toMapE [K comparable , V any ](i any , keyFn func (any ) K , valFn func (any ) V ) (map [K ]V , error ) {
1004
+ m := map [K ]V {}
1005
+
1006
+ if i == nil {
1007
+ return m , fmt .Errorf ("unable to cast %#v of type %T to %T" , i , i , m )
1008
+ }
1006
1009
1007
1010
switch v := i .(type ) {
1008
- case map [string ] string :
1011
+ case map [K ] V :
1009
1012
return v , nil
1010
- case map [string ]interface {}:
1013
+
1014
+ case map [K ]any :
1011
1015
for k , val := range v {
1012
- m [ToString ( k ) ] = ToString (val )
1016
+ m [k ] = valFn (val )
1013
1017
}
1018
+
1014
1019
return m , nil
1015
- case map [interface {}]string :
1020
+
1021
+ case map [any ]V :
1016
1022
for k , val := range v {
1017
- m [ToString (k )] = ToString ( val )
1023
+ m [keyFn (k )] = val
1018
1024
}
1025
+
1019
1026
return m , nil
1020
- case map [interface {}]interface {}:
1027
+
1028
+ case map [any ]any :
1021
1029
for k , val := range v {
1022
- m [ToString (k )] = ToString (val )
1030
+ m [keyFn (k )] = valFn (val )
1023
1031
}
1032
+
1024
1033
return m , nil
1034
+
1025
1035
case string :
1026
1036
err := jsonStringToObject (v , & m )
1037
+
1027
1038
return m , err
1039
+
1028
1040
default :
1029
- return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]string " , i , i )
1041
+ return m , fmt .Errorf ("unable to cast %#v of type %T to %T " , i , i , m )
1030
1042
}
1031
1043
}
1032
1044
1045
+ func toStringMapE [T any ](i any , fn func (any ) T ) (map [string ]T , error ) {
1046
+ return toMapE (i , ToString , fn )
1047
+ }
1048
+
1049
+ // ToStringMapStringE casts an interface to a map[string]string type.
1050
+ func ToStringMapStringE (i any ) (map [string ]string , error ) {
1051
+ return toStringMapE (i , ToString )
1052
+ }
1053
+
1033
1054
// ToStringMapStringSliceE casts an interface to a map[string][]string type.
1034
1055
func ToStringMapStringSliceE (i interface {}) (map [string ][]string , error ) {
1035
1056
m := map [string ][]string {}
@@ -1096,130 +1117,83 @@ func ToStringMapStringSliceE(i interface{}) (map[string][]string, error) {
1096
1117
1097
1118
// ToStringMapBoolE casts an interface to a map[string]bool type.
1098
1119
func ToStringMapBoolE (i interface {}) (map [string ]bool , error ) {
1099
- m := map [string ]bool {}
1100
-
1101
- switch v := i .(type ) {
1102
- case map [interface {}]interface {}:
1103
- for k , val := range v {
1104
- m [ToString (k )] = ToBool (val )
1105
- }
1106
- return m , nil
1107
- case map [string ]interface {}:
1108
- for k , val := range v {
1109
- m [ToString (k )] = ToBool (val )
1110
- }
1111
- return m , nil
1112
- case map [string ]bool :
1113
- return v , nil
1114
- case string :
1115
- err := jsonStringToObject (v , & m )
1116
- return m , err
1117
- default :
1118
- return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]bool" , i , i )
1119
- }
1120
+ return toStringMapE (i , ToBool )
1120
1121
}
1121
1122
1122
1123
// ToStringMapE casts an interface to a map[string]interface{} type.
1123
1124
func ToStringMapE (i interface {}) (map [string ]interface {}, error ) {
1124
- m := map [ string ] interface {}{ }
1125
+ fn := func ( i any ) any { return i }
1125
1126
1126
- switch v := i .(type ) {
1127
- case map [interface {}]interface {}:
1128
- for k , val := range v {
1129
- m [ToString (k )] = val
1130
- }
1131
- return m , nil
1132
- case map [string ]interface {}:
1133
- return v , nil
1134
- case string :
1135
- err := jsonStringToObject (v , & m )
1136
- return m , err
1137
- default :
1138
- return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]interface{}" , i , i )
1139
- }
1127
+ return toStringMapE (i , fn )
1140
1128
}
1141
1129
1142
- // ToStringMapIntE casts an interface to a map[string]int{} type.
1143
- func ToStringMapIntE ( i interface {}) ( map [string ]int , error ) {
1144
- m := map [ string ] int {}
1130
+ func toStringMapIntE [ T int | int64 ]( i any , fn func ( any ) T , fnE func ( any ) ( T , error )) ( map [string ]T , error ) {
1131
+ m := map [string ]T {}
1132
+
1145
1133
if i == nil {
1146
- return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]int " , i , i )
1134
+ return m , fmt .Errorf ("unable to cast %#v of type %T to %T " , i , i , m )
1147
1135
}
1148
1136
1149
1137
switch v := i .(type ) {
1150
- case map [interface {}]interface {}:
1151
- for k , val := range v {
1152
- m [ToString (k )] = ToInt (val )
1153
- }
1154
- return m , nil
1155
- case map [string ]interface {}:
1156
- for k , val := range v {
1157
- m [k ] = ToInt (val )
1158
- }
1159
- return m , nil
1160
- case map [string ]int :
1138
+ case map [string ]T :
1161
1139
return v , nil
1162
- case string :
1163
- err := jsonStringToObject (v , & m )
1164
- return m , err
1165
- }
1166
1140
1167
- if reflect .TypeOf (i ).Kind () != reflect .Map {
1168
- return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]int" , i , i )
1169
- }
1170
-
1171
- mVal := reflect .ValueOf (m )
1172
- v := reflect .ValueOf (i )
1173
- for _ , keyVal := range v .MapKeys () {
1174
- val , err := ToIntE (v .MapIndex (keyVal ).Interface ())
1175
- if err != nil {
1176
- return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]int" , i , i )
1141
+ case map [string ]any :
1142
+ for k , val := range v {
1143
+ m [k ] = fn (val )
1177
1144
}
1178
- mVal .SetMapIndex (keyVal , reflect .ValueOf (val ))
1179
- }
1180
- return m , nil
1181
- }
1182
1145
1183
- // ToStringMapInt64E casts an interface to a map[string]int64{} type.
1184
- func ToStringMapInt64E (i interface {}) (map [string ]int64 , error ) {
1185
- m := map [string ]int64 {}
1186
- if i == nil {
1187
- return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]int64" , i , i )
1188
- }
1146
+ return m , nil
1189
1147
1190
- switch v := i .(type ) {
1191
- case map [interface {}]interface {}:
1148
+ case map [any ]T :
1192
1149
for k , val := range v {
1193
- m [ToString (k )] = ToInt64 ( val )
1150
+ m [ToString (k )] = val
1194
1151
}
1152
+
1195
1153
return m , nil
1196
- case map [string ]interface {}:
1154
+
1155
+ case map [any ]any :
1197
1156
for k , val := range v {
1198
- m [k ] = ToInt64 (val )
1157
+ m [ToString ( k ) ] = fn (val )
1199
1158
}
1159
+
1200
1160
return m , nil
1201
- case map [string ]int64 :
1202
- return v , nil
1161
+
1203
1162
case string :
1204
1163
err := jsonStringToObject (v , & m )
1164
+
1205
1165
return m , err
1206
1166
}
1207
1167
1208
1168
if reflect .TypeOf (i ).Kind () != reflect .Map {
1209
- return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]int64 " , i , i )
1169
+ return m , fmt .Errorf ("unable to cast %#v of type %T to %T " , i , i , m )
1210
1170
}
1171
+
1211
1172
mVal := reflect .ValueOf (m )
1212
1173
v := reflect .ValueOf (i )
1174
+
1213
1175
for _ , keyVal := range v .MapKeys () {
1214
- val , err := ToInt64E (v .MapIndex (keyVal ).Interface ())
1176
+ val , err := fnE (v .MapIndex (keyVal ).Interface ())
1215
1177
if err != nil {
1216
- return m , fmt .Errorf ("unable to cast %#v of type %T to map[string]int64 " , i , i )
1178
+ return m , fmt .Errorf ("unable to cast %#v of type %T to %T " , i , i , m )
1217
1179
}
1180
+
1218
1181
mVal .SetMapIndex (keyVal , reflect .ValueOf (val ))
1219
1182
}
1183
+
1220
1184
return m , nil
1221
1185
}
1222
1186
1187
+ // ToStringMapIntE casts an interface to a map[string]int{} type.
1188
+ func ToStringMapIntE (i any ) (map [string ]int , error ) {
1189
+ return toStringMapIntE (i , ToInt , ToIntE )
1190
+ }
1191
+
1192
+ // ToStringMapInt64E casts an interface to a map[string]int64{} type.
1193
+ func ToStringMapInt64E (i interface {}) (map [string ]int64 , error ) {
1194
+ return toStringMapIntE (i , ToInt64 , ToInt64E )
1195
+ }
1196
+
1223
1197
// ToSliceE casts an interface to a []interface{} type.
1224
1198
func ToSliceE (i interface {}) ([]interface {}, error ) {
1225
1199
var s []interface {}
@@ -1237,35 +1211,39 @@ func ToSliceE(i interface{}) ([]interface{}, error) {
1237
1211
}
1238
1212
}
1239
1213
1240
- // ToBoolSliceE casts an interface to a []bool type.
1241
- func ToBoolSliceE (i interface {}) ([]bool , error ) {
1214
+ func toSliceE [T any ](i any , fn func (any ) (T , error )) ([]T , error ) {
1242
1215
if i == nil {
1243
- return []bool {}, fmt .Errorf ("unable to cast %#v of type %T to []bool " , i , i )
1216
+ return []T {}, fmt .Errorf ("unable to cast %#v of type %T to %T " , i , i , [] T {} )
1244
1217
}
1245
1218
1246
1219
switch v := i .(type ) {
1247
- case []bool :
1220
+ case []T :
1248
1221
return v , nil
1249
1222
}
1250
1223
1251
1224
kind := reflect .TypeOf (i ).Kind ()
1252
1225
switch kind {
1253
1226
case reflect .Slice , reflect .Array :
1254
1227
s := reflect .ValueOf (i )
1255
- a := make ([]bool , s .Len ())
1228
+ a := make ([]T , s .Len ())
1256
1229
for j := 0 ; j < s .Len (); j ++ {
1257
- val , err := ToBoolE (s .Index (j ).Interface ())
1230
+ val , err := fn (s .Index (j ).Interface ())
1258
1231
if err != nil {
1259
- return []bool {}, fmt .Errorf ("unable to cast %#v of type %T to []bool " , i , i )
1232
+ return []T {}, fmt .Errorf ("unable to cast %#v of type %T to %T " , i , i , [] T {} )
1260
1233
}
1261
1234
a [j ] = val
1262
1235
}
1263
1236
return a , nil
1264
1237
default :
1265
- return []bool {}, fmt .Errorf ("unable to cast %#v of type %T to []bool " , i , i )
1238
+ return []T {}, fmt .Errorf ("unable to cast %#v of type %T to %T " , i , i , [] T {} )
1266
1239
}
1267
1240
}
1268
1241
1242
+ // ToBoolSliceE casts an interface to a []bool type.
1243
+ func ToBoolSliceE (i interface {}) ([]bool , error ) {
1244
+ return toSliceE (i , ToBoolE )
1245
+ }
1246
+
1269
1247
// ToStringSliceE casts an interface to a []string type.
1270
1248
func ToStringSliceE (i interface {}) ([]string , error ) {
1271
1249
var a []string
@@ -1328,60 +1306,12 @@ func ToStringSliceE(i interface{}) ([]string, error) {
1328
1306
1329
1307
// ToIntSliceE casts an interface to a []int type.
1330
1308
func ToIntSliceE (i interface {}) ([]int , error ) {
1331
- if i == nil {
1332
- return []int {}, fmt .Errorf ("unable to cast %#v of type %T to []int" , i , i )
1333
- }
1334
-
1335
- switch v := i .(type ) {
1336
- case []int :
1337
- return v , nil
1338
- }
1339
-
1340
- kind := reflect .TypeOf (i ).Kind ()
1341
- switch kind {
1342
- case reflect .Slice , reflect .Array :
1343
- s := reflect .ValueOf (i )
1344
- a := make ([]int , s .Len ())
1345
- for j := 0 ; j < s .Len (); j ++ {
1346
- val , err := ToIntE (s .Index (j ).Interface ())
1347
- if err != nil {
1348
- return []int {}, fmt .Errorf ("unable to cast %#v of type %T to []int" , i , i )
1349
- }
1350
- a [j ] = val
1351
- }
1352
- return a , nil
1353
- default :
1354
- return []int {}, fmt .Errorf ("unable to cast %#v of type %T to []int" , i , i )
1355
- }
1309
+ return toSliceE (i , ToIntE )
1356
1310
}
1357
1311
1358
1312
// ToDurationSliceE casts an interface to a []time.Duration type.
1359
1313
func ToDurationSliceE (i interface {}) ([]time.Duration , error ) {
1360
- if i == nil {
1361
- return []time.Duration {}, fmt .Errorf ("unable to cast %#v of type %T to []time.Duration" , i , i )
1362
- }
1363
-
1364
- switch v := i .(type ) {
1365
- case []time.Duration :
1366
- return v , nil
1367
- }
1368
-
1369
- kind := reflect .TypeOf (i ).Kind ()
1370
- switch kind {
1371
- case reflect .Slice , reflect .Array :
1372
- s := reflect .ValueOf (i )
1373
- a := make ([]time.Duration , s .Len ())
1374
- for j := 0 ; j < s .Len (); j ++ {
1375
- val , err := ToDurationE (s .Index (j ).Interface ())
1376
- if err != nil {
1377
- return []time.Duration {}, fmt .Errorf ("unable to cast %#v of type %T to []time.Duration" , i , i )
1378
- }
1379
- a [j ] = val
1380
- }
1381
- return a , nil
1382
- default :
1383
- return []time.Duration {}, fmt .Errorf ("unable to cast %#v of type %T to []time.Duration" , i , i )
1384
- }
1314
+ return toSliceE (i , ToDurationE )
1385
1315
}
1386
1316
1387
1317
// StringToDate attempts to parse a string into a time.Time type using a
0 commit comments