You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.6 KiB

4 years ago
package value
import (
"time"
4 years ago
"gitoa.ru/go-4devs/console/input/flag"
4 years ago
)
4 years ago
type Value interface {
String() string
Int() int
Int64() int64
Uint() uint
Uint64() uint64
Float64() float64
Bool() bool
Duration() time.Duration
Time() time.Time
Any() interface{}
Strings() []string
Ints() []int
Int64s() []int64
Uints() []uint
Uint64s() []uint64
Float64s() []float64
Bools() []bool
Durations() []time.Duration
Times() []time.Time
}
type Append interface {
4 years ago
Value
Append(string) error
}
4 years ago
//nolint: gocyclo
func New(v interface{}) Append {
4 years ago
switch val := v.(type) {
case string:
4 years ago
return &String{Val: []string{val}, Flag: flag.String}
4 years ago
case int:
4 years ago
return &Int{Val: []int{val}, Flag: flag.Int}
4 years ago
case int64:
4 years ago
return &Int64{Val: []int64{val}, Flag: flag.Int64}
4 years ago
case uint:
4 years ago
return &Uint{Val: []uint{val}, Flag: flag.Uint}
4 years ago
case uint64:
4 years ago
return &Uint64{Val: []uint64{val}, Flag: flag.Uint64}
4 years ago
case float64:
4 years ago
return &Float64{Val: []float64{val}, Flag: flag.Float64}
4 years ago
case bool:
4 years ago
return &Bool{Val: []bool{val}, Flag: flag.Bool}
4 years ago
case time.Duration:
4 years ago
return &Duration{Val: []time.Duration{val}, Flag: flag.Duration}
4 years ago
case time.Time:
4 years ago
return &Time{Val: []time.Time{val}, Flag: flag.Time}
4 years ago
case []int64:
4 years ago
return &Int64{Val: val, Flag: flag.Int64 | flag.Array}
4 years ago
case []uint:
4 years ago
return &Uint{Val: val, Flag: flag.Uint | flag.Array}
4 years ago
case []uint64:
4 years ago
return &Uint64{Val: val, Flag: flag.Uint64 | flag.Array}
4 years ago
case []float64:
4 years ago
return &Float64{Val: val, Flag: flag.Float64 | flag.Array}
4 years ago
case []bool:
4 years ago
return &Bool{Val: val, Flag: flag.Bool | flag.Array}
4 years ago
case []time.Duration:
4 years ago
return &Duration{Val: val, Flag: flag.Duration | flag.Array}
4 years ago
case []time.Time:
4 years ago
return &Time{Val: val, Flag: flag.Time | flag.Array}
4 years ago
case []string:
4 years ago
return &String{Val: val, Flag: flag.String | flag.Array}
4 years ago
case []int:
4 years ago
return &Int{Val: val, Flag: flag.Int | flag.Array}
4 years ago
case []interface{}:
4 years ago
return &Any{Val: val, Flag: flag.Any | flag.Array}
case Append:
4 years ago
return val
case Value:
return &Read{Value: val}
4 years ago
default:
if v != nil {
4 years ago
return &Any{Val: []interface{}{v}, Flag: flag.Any}
4 years ago
}
return &empty{}
4 years ago
}
}
func ByFlag(f flag.Flag) Append {
4 years ago
switch {
4 years ago
case f.IsInt():
return &Int{Flag: f | flag.Int}
case f.IsInt64():
return &Int64{Flag: f | flag.Int64}
case f.IsUint():
return &Uint{Flag: f | flag.Uint}
case f.IsUint64():
return &Uint64{Flag: f | flag.Uint64}
case f.IsFloat64():
return &Float64{Flag: f | flag.Float64}
case f.IsBool():
return &Bool{Flag: f | flag.Bool}
case f.IsDuration():
return &Duration{Flag: f | flag.Duration}
case f.IsTime():
return &Time{Flag: f | flag.Time}
case f.IsAny():
return &Any{Flag: f | flag.Any}
4 years ago
default:
return &String{}
}
}