Skip to content

Commit

Permalink
feat: add int convert
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Aug 20, 2024
1 parent ce95f07 commit 62fb0d6
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,134 @@ func Bool(val any) bool {
panic("unknown type for Bool()")
}
}

// Int returns the integer representation of the value.
func Int(val any) int {

Check failure on line 63 in conv/conv.go

View workflow job for this annotation

GitHub Actions / lint

cyclomatic complexity 45 of func `Int` is high (> 10) (gocyclo)
switch val := val.(type) {
case bool:
if val {
return 1
}

return 0
case int:
return val
case int8:
return int(val)
case int16:
return int(val)
case int32:
return int(val)
case int64:
return int(val)
case uint:
return int(val)
case uint8:
return int(val)
case uint16:
return int(val)
case uint32:
return int(val)
case uint64:
return int(val)
case float32:
return int(val)
case float64:
return int(val)
case *int:
if val == nil {
return 0
}

return *val
case *int8:
if val == nil {
return 0
}

return int(*val)
case *int16:
if val == nil {
return 0
}

return int(*val)
case *int32:
if val == nil {
return 0
}

return int(*val)
case *int64:
if val == nil {
return 0
}

return int(*val)
case *uint:
if val == nil {
return 0
}

return int(*val)
case *uint8:
if val == nil {
return 0
}

return int(*val)
case *uint16:
if val == nil {
return 0
}

return int(*val)
case *uint32:
if val == nil {
return 0
}

return int(*val)
case *uint64:
if val == nil {
return 0
}

return int(*val)

case *float32:
if val == nil {
return 0
}

return int(*val)
case *float64:
if val == nil {
return 0
}

return int(*val)
case string:
i, err := strconv.Atoi(val)
if err != nil {
panic(err)
}

return i
case *string:
if val == nil {
return 0
}

i, err := strconv.Atoi(*val)
if err != nil {
panic(err)
}

return i
case nil:
return 0
default:
panic("unknown type for Int()")
}
}

0 comments on commit 62fb0d6

Please sign in to comment.