envvars/slices.go

163 lines
3.4 KiB
Go
Raw Permalink Normal View History

2022-01-20 12:23:20 +01:00
package envvars
import (
"os"
"strconv"
"strings"
)
func StringSlice(key, sep string) []string {
if v, ok := os.LookupEnv(key); ok {
return strings.Split(v, sep)
}
return []string{}
}
func ByteSlice(key, sep string) []byte {
return []byte(String(key, ""))
}
func RuneSlice(key, sep string) []rune {
return []rune(String(key, ""))
}
func IntSlice(key, sep string) []int {
res := make([]int, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil {
res = append(res, int(v))
}
}
return res
}
func Int8Slice(key, sep string) []int8 {
res := make([]int8, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil {
res = append(res, int8(v))
}
}
return res
}
func Int16Slice(key, sep string) []int16 {
res := make([]int16, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil {
res = append(res, int16(v))
}
}
return res
}
func Int32Slice(key, sep string) []int32 {
res := make([]int32, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil {
res = append(res, int32(v))
}
}
return res
}
func Int64Slice(key, sep string) []int64 {
res := make([]int64, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseInt(s, 10, 64); err != nil {
res = append(res, v)
}
}
return res
}
func Uint8Slice(key, sep string) []uint8 {
res := make([]uint8, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseUint(s, 10, 64); err != nil {
res = append(res, uint8(v))
}
}
return res
}
func Uint16Slice(key, sep string) []uint16 {
res := make([]uint16, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseUint(s, 10, 64); err != nil {
res = append(res, uint16(v))
}
}
return res
}
func Uint32Slice(key, sep string) []uint32 {
res := make([]uint32, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseUint(s, 10, 64); err != nil {
res = append(res, uint32(v))
}
}
return res
}
func Uint64Slice(key, sep string) []uint64 {
res := make([]uint64, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseUint(s, 10, 64); err != nil {
res = append(res, v)
}
}
return res
}
func Float32Slice(key, sep string) []float32 {
res := make([]float32, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseFloat(s, 64); err != nil {
res = append(res, float32(v))
}
}
return res
}
func Float64Slice(key, sep string) []float64 {
res := make([]float64, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseFloat(s, 64); err != nil {
res = append(res, v)
}
}
return res
}
func Complex64Slice(key, sep string) []complex64 {
res := make([]complex64, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseComplex(s, 64); err != nil {
res = append(res, complex64(v))
}
}
return res
}
func Complex128Slice(key, sep string) []complex128 {
res := make([]complex128, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseComplex(s, 64); err != nil {
res = append(res, v)
}
}
return res
}
func BoolSlice(key, sep string) []bool {
res := make([]bool, 0)
for _, s := range StringSlice(key, sep) {
if v, err := strconv.ParseBool(s); err != nil {
res = append(res, v)
}
}
return res
}