Improve ListBindGenericSlice

- Add support for encoding.TextMarshaler and encoding.TextUnmarshaler
This commit is contained in:
David Vogel 2023-07-02 01:17:41 +02:00
parent a0e2314138
commit a60a1f4af7

View File

@ -1,6 +1,7 @@
package input
import (
"encoding"
"fmt"
"strconv"
@ -35,6 +36,12 @@ func (l ListBindGenericSlice[T]) ListKeyValuePairs() []ListKeyValuePair {
var str string
switch v := any(entry).(type) {
case encoding.TextMarshaler:
if text, err := v.MarshalText(); err != nil {
str = fmt.Sprintf("%d: %s", i, err.Error())
} else {
str = string(text)
}
case string:
str = v
case int:
@ -89,6 +96,10 @@ func (l ListBindGenericSlice[T]) ListDeleteKey(key string) bool {
func (l ListBindGenericSlice[T]) ListAddKeyValuePair(key, value string) error {
var val T
switch v := any(&val).(type) {
case encoding.TextUnmarshaler:
if err := v.UnmarshalText([]byte(value)); err != nil {
return err
}
case *string:
*v = value
case *int: