D3vugu-components/components/input/value-binder.go

255 lines
6.6 KiB
Go
Raw Permalink Normal View History

2023-05-25 20:55:42 +00:00
package input
import (
"encoding"
2023-05-25 20:55:42 +00:00
"fmt"
"reflect"
2023-05-25 20:55:42 +00:00
"strconv"
"time"
2023-05-25 20:55:42 +00:00
)
// TODO: Locale dependent formatting
// ValueGetter can be implemented by types that need to be bound to UI components.
type ValueGetter interface {
StringValue() string // StringValue returns the value as a string formatted in the users/current locale.
}
// ValueSetter can be implemented by types that need to be bound to UI components.
type ValueSetter interface {
SetStringValue(string) error // SetStringValue parses the string value in the users/current locale.
}
// ValueBinder can be implemented by types that need to be bound to UI components.
type ValueBinder interface {
ValueGetter
ValueSetter
2023-05-25 20:55:42 +00:00
}
// HTMLInputTyper can be implemented by types that need to be bound to UI components.
type HTMLInputTyper interface {
HTMLInputType() string // HTMLInputType returns the to be used type parameter for the HTML input element.
}
// ValueBindAny implements the ValueBinder interface for all basic types and the encoding.TextMarshaler, encoding.TextUnmarshaler interfaces.
type ValueBindAny struct{ Value any }
2023-05-25 20:55:42 +00:00
func (f ValueBindAny) StringValue() string {
2023-05-25 20:55:42 +00:00
switch v := f.Value.(type) {
case ValueGetter: // Support nested value getters.
return v.StringValue()
case *time.Time: // Put time.Time before TextMarshaler, so we can define a custom format that is compatible with input fields.
return v.Local().Format("2006-01-02T15:04")
case time.Time:
return v.Local().Format("2006-01-02T15:04")
case encoding.TextMarshaler:
data, _ := v.MarshalText() // Ignore any errors, as we can't handle them here.
return string(data)
2023-05-25 20:55:42 +00:00
}
// Determine the reflection type so we can support any type that wraps basic types.
str, err := valueBindAnyMarshalBasicValue(reflect.ValueOf(f.Value))
if err != nil {
return err.Error()
}
return str
2023-05-25 20:55:42 +00:00
}
func (f ValueBindAny) SetStringValue(value string) error {
2023-05-25 20:55:42 +00:00
switch v := f.Value.(type) {
case ValueSetter: // Support nested value setters.
return v.SetStringValue(value)
case *time.Time: // Put time.Time before TextMarshaler, so we can define a custom format that is compatible with input fields.
val, err := time.ParseInLocation("2006-01-02T15:04", value, time.Local)
if err != nil {
return err
}
*v = val
case encoding.TextUnmarshaler:
return v.UnmarshalText([]byte(value))
}
return valueBindAnyUnmarshalBasicValue(value, reflect.ValueOf(f.Value))
}
func (f ValueBindAny) HTMLInputType() string {
switch v := f.Value.(type) {
case HTMLInputTyper: // Support nested values.
return v.HTMLInputType()
case *time.Time:
return "datetime-local"
}
v := reflect.ValueOf(f.Value)
switch v.Kind() {
case reflect.Pointer, reflect.Interface:
if v.IsNil() {
return "text"
}
return ValueBindAny{Value: v.Elem().Interface()}.HTMLInputType()
case reflect.Bool:
return "text" // We will render the boolean as text, as this is an input field, not a checkbox.
case reflect.String:
return "text"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return "number"
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "number"
case reflect.Float32, reflect.Float64:
return "number"
}
return "text"
}
func valueBindAnyMarshalBasicValue(v reflect.Value) (string, error) {
if !v.IsValid() {
return "", fmt.Errorf("bound value is invalid")
}
switch v.Kind() {
case reflect.Pointer, reflect.Interface:
if v.IsNil() {
return "", fmt.Errorf("bound value pointer is nil")
}
return valueBindAnyMarshalBasicValue(v.Elem())
case reflect.Bool:
return strconv.FormatBool(v.Bool()), nil
case reflect.String:
return v.String(), nil
case reflect.Int:
return strconv.FormatInt(v.Int(), 10), nil
case reflect.Int8:
return strconv.FormatInt(v.Int(), 10), nil
case reflect.Int16:
return strconv.FormatInt(v.Int(), 10), nil
case reflect.Int32:
return strconv.FormatInt(v.Int(), 10), nil
case reflect.Int64:
return strconv.FormatInt(v.Int(), 10), nil
case reflect.Uint:
return strconv.FormatUint(v.Uint(), 10), nil
case reflect.Uint8:
return strconv.FormatUint(v.Uint(), 10), nil
case reflect.Uint16:
return strconv.FormatUint(v.Uint(), 10), nil
case reflect.Uint32:
return strconv.FormatUint(v.Uint(), 10), nil
case reflect.Uint64:
return strconv.FormatUint(v.Uint(), 10), nil
case reflect.Float32:
return strconv.FormatFloat(v.Float(), 'f', -1, 32), nil
case reflect.Float64:
return strconv.FormatFloat(v.Float(), 'f', -1, 64), nil
}
return "", fmt.Errorf("bound type %T is not supported", v.Interface())
}
func valueBindAnyUnmarshalBasicValue(str string, v reflect.Value) error {
if !v.IsValid() {
return fmt.Errorf("bound value is invalid")
}
// Get value that is stored in pointer or interface.
kind := v.Kind()
if kind == reflect.Pointer || kind == reflect.Interface {
if v.IsNil() {
return fmt.Errorf("bound value pointer is nil")
}
return valueBindAnyUnmarshalBasicValue(str, v.Elem())
}
// Check if we are allowed to modify the value.
if !v.CanSet() {
return fmt.Errorf("bound value can't be modified")
}
switch kind {
case reflect.Bool:
val, err := strconv.ParseBool(str)
if err != nil {
return err
}
v.SetBool(val)
case reflect.String:
v.SetString(str)
case reflect.Int:
val, err := strconv.ParseInt(str, 10, 0)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetInt(val)
case reflect.Int8:
val, err := strconv.ParseInt(str, 10, 8)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetInt(val)
case reflect.Int16:
val, err := strconv.ParseInt(str, 10, 16)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetInt(val)
case reflect.Int32:
val, err := strconv.ParseInt(str, 10, 32)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetInt(val)
case reflect.Int64:
val, err := strconv.ParseInt(str, 10, 64)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetInt(val)
case reflect.Uint:
val, err := strconv.ParseUint(str, 10, 0)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetUint(val)
case reflect.Uint8:
val, err := strconv.ParseUint(str, 10, 8)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetUint(val)
case reflect.Uint16:
val, err := strconv.ParseUint(str, 10, 16)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetUint(val)
case reflect.Uint32:
val, err := strconv.ParseUint(str, 10, 32)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetUint(val)
case reflect.Uint64:
val, err := strconv.ParseUint(str, 10, 64)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetUint(val)
case reflect.Float32:
val, err := strconv.ParseFloat(str, 32)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetFloat(val)
case reflect.Float64:
val, err := strconv.ParseFloat(str, 64)
2023-05-25 20:55:42 +00:00
if err != nil {
return err
}
v.SetFloat(val)
2023-05-25 20:55:42 +00:00
default:
return fmt.Errorf("bound type %T is not supported", v.Interface())
2023-05-25 20:55:42 +00:00
}
return nil
}