envvars/converters.go

90 lines
1.8 KiB
Go
Raw Permalink Normal View History

2022-08-25 10:18:22 +02:00
package envvars
import (
"errors"
"strconv"
"unicode/utf8"
)
func ConvertString(s string) (string, error) {
return s, nil
}
func ConvertRune(s string) (rune, error) {
rn, _ := utf8.DecodeRuneInString(s)
if rn == utf8.RuneError {
return 0, errors.New("rune decoding failed")
}
return rn, nil
}
func ConvertInt64(s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}
func ConvertInt32(s string) (int32, error) {
v, err := strconv.ParseInt(s, 10, 32)
return int32(v), err
}
func ConvertInt16(s string) (int16, error) {
v, err := strconv.ParseInt(s, 10, 16)
return int16(v), err
}
func ConvertInt8(s string) (int8, error) {
v, err := strconv.ParseInt(s, 10, 8)
return int8(v), err
}
func ConvertInt(s string) (int, error) {
v, err := strconv.ParseInt(s, 10, 64)
return int(v), err
}
func ConvertUint64(s string) (uint64, error) {
return strconv.ParseUint(s, 10, 64)
}
func ConvertUint32(s string) (uint32, error) {
v, err := strconv.ParseUint(s, 10, 32)
return uint32(v), err
}
func ConvertUint16(s string) (uint16, error) {
v, err := strconv.ParseUint(s, 10, 16)
return uint16(v), err
}
func ConvertUint8(s string) (uint8, error) {
v, err := strconv.ParseUint(s, 10, 8)
return uint8(v), err
}
func ConvertUint(s string) (uint, error) {
v, err := strconv.ParseUint(s, 10, 64)
return uint(v), err
}
func ConvertFloat64(s string) (float64, error) {
return strconv.ParseFloat(s, 64)
}
func ConvertFloat32(s string) (float32, error) {
v, err := strconv.ParseFloat(s, 32)
return float32(v), err
}
func ConvertComplex128(s string) (complex128, error) {
return strconv.ParseComplex(s, 128)
}
func ConvertComplex64(s string) (complex64, error) {
v, err := strconv.ParseComplex(s, 64)
return complex64(v), err
}
func ConvertBool(s string) (bool, error) {
return strconv.ParseBool(s)
}