From a60a1f4af7c8de68129174c108bfc27f8c8f2032 Mon Sep 17 00:00:00 2001 From: David Vogel Date: Sun, 2 Jul 2023 01:17:41 +0200 Subject: [PATCH] Improve ListBindGenericSlice - Add support for encoding.TextMarshaler and encoding.TextUnmarshaler --- components/input/list-binder.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/components/input/list-binder.go b/components/input/list-binder.go index 0765fa5..1c4cd56 100644 --- a/components/input/list-binder.go +++ b/components/input/list-binder.go @@ -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: