2023-05-27 11:37:40 +00:00
|
|
|
package input
|
|
|
|
|
2023-06-02 09:17:22 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2023-05-27 11:37:40 +00:00
|
|
|
|
2023-06-02 09:17:22 +00:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ListBinder describes an interface that simple lists can implement to be compatible with list based UI components.
|
2023-05-27 11:37:40 +00:00
|
|
|
type ListBinder interface {
|
|
|
|
ListKeyValuePairs() []ListKeyValuePair
|
2023-06-02 09:17:22 +00:00
|
|
|
ListDeleteKey(key string) bool // Removes the entry with the given key, and returns true if it got deleted successfully.
|
|
|
|
ListAddKeyValuePair(key, value string) error // Adds the key value pair to the list.
|
|
|
|
ListLen() int // Returns the length of the list.
|
2023-05-27 11:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListKeyValuePair struct {
|
|
|
|
Key, Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListBindGenericSlice implements ListBinder for slices that are of some arbitrary type.
|
2023-07-02 22:18:58 +00:00
|
|
|
//
|
|
|
|
// List keys will be bound to slice indices, and list values will be bound to a string representation of the slice elements.
|
2023-05-27 11:37:40 +00:00
|
|
|
type ListBindGenericSlice[T any] struct {
|
2023-06-02 09:17:22 +00:00
|
|
|
Slice *[]T
|
2023-05-27 11:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l ListBindGenericSlice[T]) ListKeyValuePairs() []ListKeyValuePair {
|
2023-06-02 09:17:22 +00:00
|
|
|
if l.Slice == nil {
|
2023-05-27 11:37:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]ListKeyValuePair, 0, len(*l.Slice))
|
|
|
|
for i, entry := range *l.Slice {
|
2023-07-02 22:18:58 +00:00
|
|
|
str := ValueBindAny{entry}.StringValue()
|
2023-06-02 09:17:22 +00:00
|
|
|
result = append(result, ListKeyValuePair{Key: strconv.Itoa(i), Value: str})
|
2023-05-27 11:37:40 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-06-02 09:17:22 +00:00
|
|
|
func (l ListBindGenericSlice[T]) ListDeleteKey(key string) bool {
|
|
|
|
if l.Slice == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
index, err := strconv.Atoi(key)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if index < 0 || index >= len(*l.Slice) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
*l.Slice = slices.Delete(*l.Slice, index, index+1)
|
|
|
|
return true
|
2023-05-27 11:37:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 09:17:22 +00:00
|
|
|
func (l ListBindGenericSlice[T]) ListAddKeyValuePair(key, value string) error {
|
|
|
|
index, err := strconv.Atoi(key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse index: %w", err)
|
2023-05-27 11:37:40 +00:00
|
|
|
}
|
2023-06-02 09:17:22 +00:00
|
|
|
|
|
|
|
if index < 0 || index > len(*l.Slice) {
|
|
|
|
return fmt.Errorf("index %d out of bounds", index)
|
|
|
|
}
|
|
|
|
|
2023-07-02 22:18:58 +00:00
|
|
|
var val T
|
|
|
|
if err := (ValueBindAny{&val}).SetStringValue(value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-02 09:17:22 +00:00
|
|
|
*l.Slice = slices.Insert(*l.Slice, index, val)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l ListBindGenericSlice[T]) ListLen() int {
|
|
|
|
return len(*l.Slice)
|
2023-05-27 11:37:40 +00:00
|
|
|
}
|
2023-07-02 22:18:58 +00:00
|
|
|
|
|
|
|
// ListBindGenericValues implements ListBinder for slices that are of some arbitrary type.
|
|
|
|
//
|
|
|
|
// List keys and values will be bound to the string representation of the slice elements.
|
|
|
|
//
|
|
|
|
// This is useful for dropdown components, as the key of the selected element will be set to the bound value.
|
|
|
|
//
|
|
|
|
// This can not modify the bound list.
|
|
|
|
type ListBindGenericValues[T any] []T
|
|
|
|
|
|
|
|
func (l ListBindGenericValues[T]) ListKeyValuePairs() []ListKeyValuePair {
|
|
|
|
result := make([]ListKeyValuePair, 0, len(l))
|
|
|
|
for _, entry := range l {
|
|
|
|
str := ValueBindAny{entry}.StringValue()
|
|
|
|
result = append(result, ListKeyValuePair{Key: str, Value: str})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l ListBindGenericValues[T]) ListDeleteKey(key string) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l ListBindGenericValues[T]) ListAddKeyValuePair(key, value string) error {
|
|
|
|
return fmt.Errorf("the list binder doesn't support list modifications")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l ListBindGenericValues[T]) ListLen() int {
|
|
|
|
return len(l)
|
|
|
|
}
|