Skip to content

Commit 4e59e3b

Browse files
add additional int types to type conversion (#215)
1 parent 542b7e3 commit 4e59e3b

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

pkg/typeconverters/typeconverters.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,65 @@ func ConvertStringToGivenType(value string, goType string) (output interface{},
169169
value = "0"
170170
}
171171
return strconv.Atoi(value)
172+
case "int8":
173+
if value == "" {
174+
value = "0"
175+
}
176+
valueInt64, err := strconv.ParseInt(value, 10, 8)
177+
if err != nil {
178+
return nil, err
179+
}
180+
return int8(valueInt64), nil
181+
case "int16":
182+
if value == "" {
183+
value = "0"
184+
}
185+
valueInt64, err := strconv.ParseInt(value, 10, 16)
186+
if err != nil {
187+
return nil, err
188+
}
189+
return int16(valueInt64), nil
190+
case "int32":
191+
if value == "" {
192+
value = "0"
193+
}
194+
valueInt64, err := strconv.ParseInt(value, 10, 32)
195+
if err != nil {
196+
return nil, err
197+
}
198+
return int32(valueInt64), nil
199+
case "int64":
200+
if value == "" {
201+
value = "0"
202+
}
203+
return strconv.ParseInt(value, 10, 64)
204+
case "uint":
205+
if value == "" {
206+
value = "0"
207+
}
208+
valueUint64, err := strconv.ParseUint(value, 10, 64)
209+
if err != nil {
210+
return nil, err
211+
}
212+
return uint(valueUint64), nil
213+
case "uint8":
214+
if value == "" {
215+
value = "0"
216+
}
217+
valueUint64, err := strconv.ParseUint(value, 10, 8)
218+
if err != nil {
219+
return nil, err
220+
}
221+
return uint8(valueUint64), nil
222+
case "uint16":
223+
if value == "" {
224+
value = "0"
225+
}
226+
valueUint64, err := strconv.ParseUint(value, 10, 16)
227+
if err != nil {
228+
return nil, err
229+
}
230+
return uint16(valueUint64), nil
172231
case "uint32":
173232
if value == "" {
174233
value = "0"
@@ -685,6 +744,20 @@ func ConvertGivenTypeToString(value interface{}, goType string) (output string,
685744
return strconv.FormatFloat(value.(float64), 'f', -1, 64), nil
686745
case "int":
687746
return strconv.Itoa(value.(int)), nil
747+
case "int8":
748+
return strconv.FormatInt(int64(value.(int8)), 10), nil
749+
case "int16":
750+
return strconv.FormatInt(int64(value.(int16)), 10), nil
751+
case "int32":
752+
return strconv.FormatInt(int64(value.(int32)), 10), nil
753+
case "int64":
754+
return strconv.FormatInt(value.(int64), 10), nil
755+
case "uint":
756+
return strconv.FormatUint(uint64(value.(uint)), 10), nil
757+
case "uint8":
758+
return strconv.FormatUint(uint64(value.(uint8)), 10), nil
759+
case "uint16":
760+
return strconv.FormatUint(uint64(value.(uint16)), 10), nil
688761
case "uint32":
689762
return strconv.FormatUint(uint64(value.(uint32)), 10), nil
690763
case "uint64":

0 commit comments

Comments
 (0)