object parsing added

This commit is contained in:
Timon Ringwald 2022-08-03 20:08:23 +02:00
parent fd8dc229ee
commit 277904865a
3 changed files with 34 additions and 15 deletions

2
go.mod
View File

@ -1,3 +1,3 @@
module git.tordarus.net/Tordarus/envvars module git.tordarus.net/Tordarus/envvars
go 1.17 go 1.18

View File

@ -24,7 +24,7 @@ func RuneSlice(key, sep string) []rune {
func IntSlice(key, sep string) []int { func IntSlice(key, sep string) []int {
res := make([]int, 0) res := make([]int, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil { if v, err := strconv.ParseInt(s, 10, 64); err == nil {
res = append(res, int(v)) res = append(res, int(v))
} }
} }
@ -34,7 +34,7 @@ func IntSlice(key, sep string) []int {
func Int8Slice(key, sep string) []int8 { func Int8Slice(key, sep string) []int8 {
res := make([]int8, 0) res := make([]int8, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil { if v, err := strconv.ParseInt(s, 10, 64); err == nil {
res = append(res, int8(v)) res = append(res, int8(v))
} }
} }
@ -44,7 +44,7 @@ func Int8Slice(key, sep string) []int8 {
func Int16Slice(key, sep string) []int16 { func Int16Slice(key, sep string) []int16 {
res := make([]int16, 0) res := make([]int16, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil { if v, err := strconv.ParseInt(s, 10, 64); err == nil {
res = append(res, int16(v)) res = append(res, int16(v))
} }
} }
@ -54,7 +54,7 @@ func Int16Slice(key, sep string) []int16 {
func Int32Slice(key, sep string) []int32 { func Int32Slice(key, sep string) []int32 {
res := make([]int32, 0) res := make([]int32, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil { if v, err := strconv.ParseInt(s, 10, 64); err == nil {
res = append(res, int32(v)) res = append(res, int32(v))
} }
} }
@ -64,7 +64,7 @@ func Int32Slice(key, sep string) []int32 {
func Int64Slice(key, sep string) []int64 { func Int64Slice(key, sep string) []int64 {
res := make([]int64, 0) res := make([]int64, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil { if v, err := strconv.ParseInt(s, 10, 64); err == nil {
res = append(res, v) res = append(res, v)
} }
} }
@ -74,7 +74,7 @@ func Int64Slice(key, sep string) []int64 {
func Uint8Slice(key, sep string) []uint8 { func Uint8Slice(key, sep string) []uint8 {
res := make([]uint8, 0) res := make([]uint8, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseUint(s, 10, 64); err != nil { if v, err := strconv.ParseUint(s, 10, 64); err == nil {
res = append(res, uint8(v)) res = append(res, uint8(v))
} }
} }
@ -84,7 +84,7 @@ func Uint8Slice(key, sep string) []uint8 {
func Uint16Slice(key, sep string) []uint16 { func Uint16Slice(key, sep string) []uint16 {
res := make([]uint16, 0) res := make([]uint16, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseUint(s, 10, 64); err != nil { if v, err := strconv.ParseUint(s, 10, 64); err == nil {
res = append(res, uint16(v)) res = append(res, uint16(v))
} }
} }
@ -94,7 +94,7 @@ func Uint16Slice(key, sep string) []uint16 {
func Uint32Slice(key, sep string) []uint32 { func Uint32Slice(key, sep string) []uint32 {
res := make([]uint32, 0) res := make([]uint32, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseUint(s, 10, 64); err != nil { if v, err := strconv.ParseUint(s, 10, 64); err == nil {
res = append(res, uint32(v)) res = append(res, uint32(v))
} }
} }
@ -104,7 +104,7 @@ func Uint32Slice(key, sep string) []uint32 {
func Uint64Slice(key, sep string) []uint64 { func Uint64Slice(key, sep string) []uint64 {
res := make([]uint64, 0) res := make([]uint64, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseUint(s, 10, 64); err != nil { if v, err := strconv.ParseUint(s, 10, 64); err == nil {
res = append(res, v) res = append(res, v)
} }
} }
@ -114,7 +114,7 @@ func Uint64Slice(key, sep string) []uint64 {
func Float32Slice(key, sep string) []float32 { func Float32Slice(key, sep string) []float32 {
res := make([]float32, 0) res := make([]float32, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseFloat(s, 64); err != nil { if v, err := strconv.ParseFloat(s, 64); err == nil {
res = append(res, float32(v)) res = append(res, float32(v))
} }
} }
@ -124,7 +124,7 @@ func Float32Slice(key, sep string) []float32 {
func Float64Slice(key, sep string) []float64 { func Float64Slice(key, sep string) []float64 {
res := make([]float64, 0) res := make([]float64, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseFloat(s, 64); err != nil { if v, err := strconv.ParseFloat(s, 64); err == nil {
res = append(res, v) res = append(res, v)
} }
} }
@ -134,7 +134,7 @@ func Float64Slice(key, sep string) []float64 {
func Complex64Slice(key, sep string) []complex64 { func Complex64Slice(key, sep string) []complex64 {
res := make([]complex64, 0) res := make([]complex64, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseComplex(s, 64); err != nil { if v, err := strconv.ParseComplex(s, 64); err == nil {
res = append(res, complex64(v)) res = append(res, complex64(v))
} }
} }
@ -144,7 +144,7 @@ func Complex64Slice(key, sep string) []complex64 {
func Complex128Slice(key, sep string) []complex128 { func Complex128Slice(key, sep string) []complex128 {
res := make([]complex128, 0) res := make([]complex128, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseComplex(s, 64); err != nil { if v, err := strconv.ParseComplex(s, 64); err == nil {
res = append(res, v) res = append(res, v)
} }
} }
@ -154,7 +154,17 @@ func Complex128Slice(key, sep string) []complex128 {
func BoolSlice(key, sep string) []bool { func BoolSlice(key, sep string) []bool {
res := make([]bool, 0) res := make([]bool, 0)
for _, s := range StringSlice(key, sep) { for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseBool(s); err != nil { if v, err := strconv.ParseBool(s); err == nil {
res = append(res, v)
}
}
return res
}
func ObjectSlice[T any](key, sep string, converter func(string) (T, error)) []T {
res := make([]T, 0)
for _, s := range StringSlice(key, sep) {
if v, err := converter(s); err == nil {
res = append(res, v) res = append(res, v)
} }
} }

View File

@ -148,3 +148,12 @@ func Bool(key string, defaultValue bool) bool {
} }
return defaultValue return defaultValue
} }
func Object[T any](key string, defaultValue T, converter func(string) (T, error)) T {
if v, ok := os.LookupEnv(key); ok {
if v2, err := converter(v); err == nil {
return v2
}
}
return defaultValue
}