WIP on v1
This commit is contained in:
136
settings.go
136
settings.go
@@ -7,38 +7,23 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("key not found")
|
||||
ErrParsingBool = errors.New("value can not be interpreted as bool")
|
||||
ErrCouldNotConvert = errors.New("could not cast one or more values to required type")
|
||||
ErrNotFound = errors.New("conf: key not found")
|
||||
ErrCouldNotConvert = errors.New("conf: could not cast one or more values to required type")
|
||||
)
|
||||
|
||||
var valuesSeparator = ","
|
||||
|
||||
var boolMap = map[string]bool{
|
||||
// what evaluates to true
|
||||
"true": true,
|
||||
"t": true,
|
||||
"1": true,
|
||||
"yes": true,
|
||||
"on": true,
|
||||
// what evaluates to false
|
||||
"false": false,
|
||||
"f": false,
|
||||
"0": false,
|
||||
"no": false,
|
||||
"off": false,
|
||||
}
|
||||
const valuesSeparator = ","
|
||||
|
||||
// Setting represents key-value pair read from config file.
|
||||
// It's Value field holds the value of key parsed from the configuration
|
||||
type Setting struct {
|
||||
Name string
|
||||
Value string
|
||||
}
|
||||
|
||||
// Int converts Setting Value to int. Returned error
|
||||
// will be non nil if convesion failed.
|
||||
func (st Setting) Int() (int, error) {
|
||||
return parseInt(st.Value)
|
||||
func (s Setting) Int() (int, error) {
|
||||
return parseValue(s.Value, strconv.Atoi)
|
||||
}
|
||||
|
||||
// IntSlice splits Setting Value (separator is ",") and adds
|
||||
@@ -46,19 +31,14 @@ func (st Setting) Int() (int, error) {
|
||||
// If one or more values can not be converted to float64 those will be dropped
|
||||
// and method will return conf.ErrCouldNotConvert.
|
||||
// Check error to be sure that all required values were parsed.
|
||||
func (st Setting) IntSlice() ([]int, error) {
|
||||
return parseIntSlice(st.Value, valuesSeparator)
|
||||
func (s Setting) IntSlice() ([]int, error) {
|
||||
return parseSlice(s.Value, strconv.Atoi)
|
||||
}
|
||||
|
||||
/* func (st Setting) split(sep string) Setting {
|
||||
st.sep = sep //Choose separator to split values ?
|
||||
return st
|
||||
} */
|
||||
|
||||
// Float64 converts Setting Value to float64. Returned error
|
||||
// will be non nil if convesion failed.
|
||||
func (st Setting) Float64() (float64, error) {
|
||||
return parseFloat64(st.Value)
|
||||
func (s Setting) Float64() (float64, error) {
|
||||
return parseValue(s.Value, parseFloat64)
|
||||
}
|
||||
|
||||
// Float64Slice splits Setting Value (separator is ",") and adds
|
||||
@@ -66,92 +46,64 @@ func (st Setting) Float64() (float64, error) {
|
||||
// If one or more values can not be converted to float64 those will be dropped
|
||||
// and method will return conf.ErrCouldNotConvert.
|
||||
// Check error to be sure that all required values were parsed.
|
||||
func (st Setting) Float64Slice() ([]float64, error) {
|
||||
return parseFloat64Slice(st.Value, valuesSeparator)
|
||||
func (s Setting) Float64Slice() ([]float64, error) {
|
||||
return parseSlice(s.Value, parseFloat64)
|
||||
}
|
||||
|
||||
// String returns option Value as string
|
||||
// This method also implements Stringer interface from fmt module
|
||||
func (st Setting) String() string {
|
||||
return st.Value
|
||||
func (s Setting) String() string {
|
||||
return s.Value
|
||||
}
|
||||
|
||||
// StringSlice splits Setting's Value (separator is ",") and adds
|
||||
// each of resulting values to []string trimming leading and trailing spaces
|
||||
// from each string.
|
||||
func (st Setting) StringSlice() []string {
|
||||
return tidySplit(st.Value, valuesSeparator)
|
||||
func (s Setting) StringSlice() []string {
|
||||
return tidySplit(s.Value)
|
||||
}
|
||||
|
||||
// Bool tries to interpret Setting's Value as bool
|
||||
// "1", "true", "on", "yes" (case insensitive) yields true
|
||||
// "0", "false", "off", "no" (case insensitive) yields false
|
||||
// If nothing matches will return false and conf.ErrParsingBool
|
||||
func (st Setting) Bool() (bool, error) {
|
||||
return parseBool(st.Value)
|
||||
// "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 (s Setting) Bool() (bool, error) {
|
||||
return parseValue(s.Value, strconv.ParseBool)
|
||||
}
|
||||
|
||||
func parseInt(s string) (n int, err error) {
|
||||
n, err = strconv.Atoi(s)
|
||||
return
|
||||
}
|
||||
func parseSlice[T any](s string, f func(string) (T, error)) ([]T, error) {
|
||||
split := tidySplit(s)
|
||||
|
||||
func parseIntSlice(s, sep string) ([]int, error) {
|
||||
var (
|
||||
n int
|
||||
err error
|
||||
slice []int
|
||||
digits []string
|
||||
)
|
||||
digits = tidySplit(s, sep)
|
||||
for _, d := range digits {
|
||||
n, err = strconv.Atoi(d)
|
||||
list := make([]T, 0, len(split))
|
||||
for _, str := range split {
|
||||
v, err := parseValue(str, f)
|
||||
if err != nil {
|
||||
err = ErrCouldNotConvert
|
||||
break
|
||||
return list, err
|
||||
}
|
||||
slice = append(slice, n)
|
||||
|
||||
list = append(list, v)
|
||||
}
|
||||
return slice, err
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func parseFloat64(s string) (n float64, err error) {
|
||||
n, err = strconv.ParseFloat(s, 64)
|
||||
return
|
||||
}
|
||||
|
||||
func parseFloat64Slice(s, sep string) ([]float64, error) {
|
||||
var (
|
||||
n float64
|
||||
err error
|
||||
slice []float64
|
||||
digits []string
|
||||
)
|
||||
digits = tidySplit(s, sep)
|
||||
for _, d := range digits {
|
||||
n, err = strconv.ParseFloat(d, 64)
|
||||
if err != nil {
|
||||
err = ErrCouldNotConvert
|
||||
break
|
||||
}
|
||||
slice = append(slice, n)
|
||||
func parseValue[T any](s string, f func(string) (T, error)) (T, error) {
|
||||
v, err := f(s)
|
||||
if err != nil {
|
||||
return v, errors.Join(ErrCouldNotConvert, err)
|
||||
}
|
||||
return slice, err
|
||||
|
||||
return v, err
|
||||
}
|
||||
|
||||
func parseBool(s string) (value bool, err error) {
|
||||
s = strings.ToLower(s)
|
||||
value, ok := boolMap[s]
|
||||
if !ok {
|
||||
err = ErrParsingBool
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func tidySplit(s, sep string) []string {
|
||||
splitted := strings.Split(s, sep)
|
||||
func tidySplit(s string) []string {
|
||||
splitted := strings.Split(s, valuesSeparator)
|
||||
for i, str := range splitted {
|
||||
splitted[i] = strings.Trim(str, " ")
|
||||
splitted[i] = strings.TrimSpace(str)
|
||||
}
|
||||
return splitted
|
||||
}
|
||||
|
||||
func parseFloat64(s string) (float64, error) {
|
||||
return strconv.ParseFloat(s, 64)
|
||||
}
|
||||
|
Reference in New Issue
Block a user