Files
conf/value.go

128 lines
3.0 KiB
Go
Raw Normal View History

2025-04-09 04:57:40 +03:00
package conf
import (
"errors"
2025-04-13 22:46:22 +03:00
"net/url"
2025-04-09 04:57:40 +03:00
"strconv"
"strings"
)
var (
ErrCouldNotConvert = errors.New("conf: could not cast one or more values to required type")
)
const (
separatorSlice = ","
separatorMap = ":"
)
type Value string
2025-04-13 22:46:22 +03:00
// Map tries to interpret value as "key: value" pairs
// separated by comma like in the following string:
// "key1: value1, key2: value2, key3: value3".
func (v Value) Map() (Map, error) {
2025-04-09 04:57:40 +03:00
split := tidySplit(v, separatorSlice)
2025-04-13 22:46:22 +03:00
m := make(Map, len(split))
2025-04-09 04:57:40 +03:00
for i := range split {
k, v, ok := toKV(split[i], separatorMap)
if !ok {
2025-04-13 22:46:22 +03:00
return nil, ErrCouldNotConvert
2025-04-09 04:57:40 +03:00
}
m[k] = Value(v)
}
2025-04-13 22:46:22 +03:00
2025-04-09 04:57:40 +03:00
return m, nil
}
// String returns Value as string
func (v Value) String() string {
return string(v)
}
// Int converts Value to int. Returned error
// will be non nil if convesion failed.
func (v Value) Int() (int, error) {
2025-04-13 22:46:22 +03:00
return parseValue(v, strconv.Atoi) // Atoi in fact checks int size, no need to use ParseInt
2025-04-09 04:57:40 +03:00
}
// IntSlice splits Value (separator is ",") and adds
// each of resulting values to []int if possible.
2025-04-13 22:46:22 +03:00
// Returns nil and non-nil error on first failure to convert.
2025-04-09 04:57:40 +03:00
func (v Value) IntSlice() ([]int, error) {
return parseSlice(v, strconv.Atoi)
}
2025-04-13 22:46:22 +03:00
// Float64 converts Value to float64. Returned error
2025-04-09 04:57:40 +03:00
// will be non nil if convesion failed.
func (v Value) Float64() (float64, error) {
return parseValue(v, parseFloat64)
}
2025-04-13 22:46:22 +03:00
// Float64Slice splits Value (separator is ",") and adds
2025-04-09 04:57:40 +03:00
// each of resulting values to []float64 if possible.
2025-04-13 22:46:22 +03:00
// Returns nil and non-nil error on first failure to convert.
2025-04-09 04:57:40 +03:00
func (v Value) Float64Slice() ([]float64, error) {
return parseSlice(v, parseFloat64)
}
2025-04-13 22:46:22 +03:00
// StringSlice splits Value (separator is ",") and adds
2025-04-09 04:57:40 +03:00
// each of resulting values to []string trimming leading and trailing spaces
// from each string.
func (v Value) StringSlice() []string {
return tidySplit(v, separatorSlice)
}
2025-04-13 22:46:22 +03:00
// Bool tries to interpret Value as bool
2025-04-09 04:57:40 +03:00
// "1", "t", "T", true", "True", "TRUE" yields true
// "0", "f", "F, "false", "False", "FALSE" yields false
// If nothing matches will return false and conf.ErrCouldNotConvert.
func (v Value) Bool() (bool, error) {
return parseValue(v, strconv.ParseBool)
}
2025-04-13 22:46:22 +03:00
// URL tries to interpret value as url.URL
func (v Value) URL() (*url.URL, error) {
return parseValue(v, url.Parse)
}
2025-04-09 04:57:40 +03:00
func parseSlice[T any, S ~string](s S, f func(string) (T, error)) ([]T, error) {
split := tidySplit(s, separatorSlice)
list := make([]T, 0, len(split))
for _, str := range split {
v, err := parseValue(str, f)
if err != nil {
2025-04-09 14:20:43 +03:00
return nil, err
2025-04-09 04:57:40 +03:00
}
list = append(list, v)
}
return list, nil
}
func parseValue[T any, S ~string](s S, f func(string) (T, error)) (T, error) {
v, err := f(string(s))
if err != nil {
return v, errors.Join(ErrCouldNotConvert, err)
}
return v, err
}
func tidySplit[S ~string](s S, sep string) []string {
splitted := strings.Split(string(s), sep)
for i, str := range splitted {
2025-04-09 14:20:43 +03:00
splitted[i] = trim(str)
2025-04-09 04:57:40 +03:00
}
return splitted
}
func parseFloat64(s string) (float64, error) {
return strconv.ParseFloat(s, 64)
}