Skip to content

Commit

Permalink
have Bind() support fmt.Stringer
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Nov 21, 2024
1 parent 746af07 commit 81d7b34
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
16 changes: 15 additions & 1 deletion bind.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jaws

import (
"fmt"
"sync"
"time"
)
Expand Down Expand Up @@ -53,6 +54,12 @@ func (bind Binding[T]) JawsSet(elem *Element, value T) error {
}

func (bind Binding[T]) JawsGetTag(*Request) any {
if x, ok := any(*bind.P).(fmt.Stringer); ok {
return x
}
if x, ok := any(bind.P).(fmt.Stringer); ok {
return x
}
return bind.P
}

Expand All @@ -61,6 +68,12 @@ func (bind Binding[T]) JawsSetString(e *Element, val string) (err error) {
}

func (bind Binding[T]) JawsGetString(e *Element) string {
if x, ok := any(*bind.P).(fmt.Stringer); ok {
return x.String()
}
if x, ok := any(bind.P).(fmt.Stringer); ok {
return x.String()
}
return any(bind.JawsGet(e)).(string)
}

Expand Down Expand Up @@ -90,7 +103,8 @@ func (bind Binding[T]) JawsSetTime(elem *Element, value time.Time) error {

// Bind returns a Binding[T] with the given sync.Locker (or RWLocker) and a pointer to the underlying value of type T.
// It implements Setter[T]. It also implements BoolSetter, FloatSetter, StringSetter and TimeSetter, but will panic
// if the underlying type T is not correct.
// if the underlying type T is not correct or not settable.
// It has special support for fmt.Stringer, and will call T.String() for JawsGetString().
// The pointer will be used as the UI tag.
func Bind[T comparable](l sync.Locker, p *T) Binding[T] {
return Binding[T]{L: l, P: p}
Expand Down
6 changes: 5 additions & 1 deletion stringer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package jaws

import "fmt"
import (
"fmt"
)

type stringizer[T any] struct {
v *T
Expand All @@ -14,6 +16,8 @@ func (s stringizer[T]) JawsGetTag(*Request) any {
return s.v
}

// Stringer returns a fmt.Stringer using fmt.Sprint(*T)
// unless *T or T implements fmt.Stringer, in which case that will be returned directly.
func Stringer[T any](v *T) fmt.Stringer {
if x, ok := any(*v).(fmt.Stringer); ok {
return x
Expand Down
32 changes: 24 additions & 8 deletions stringer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *testPtrStringer) String() string {
func TestStringer(t *testing.T) {
txt := "text"
stringer := Stringer(&txt)
if s := stringer.String(); s != "text" {
if s := Bind(nil, &stringer).JawsGetString(nil); s != "text" {
t.Error(s)
}
if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{&txt}) {
Expand All @@ -29,7 +29,7 @@ func TestStringer(t *testing.T) {

num := int(123)
stringer = Stringer(&num)
if s := stringer.String(); s != "123" {
if s := Bind(nil, &stringer).JawsGetString(nil); s != "123" {
t.Error(s)
}
if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{&num}) {
Expand All @@ -41,23 +41,39 @@ func TestStringer(t *testing.T) {
if !reflect.DeepEqual(stringer, teststringer) {
t.Errorf("%#v != %#v", stringer, teststringer)
}
if s := stringer.String(); s != (testStringer{}).String() {
if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{teststringer}) {
t.Errorf("%#v", tags)
}
b1 := Bind(nil, &stringer)
if s := b1.JawsGetString(nil); s != (testStringer{}).String() {
t.Error(s)
}
if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{teststringer}) {
t.Error(tags)
if tags := MustTagExpand(nil, b1); !reflect.DeepEqual(tags, []any{teststringer}) {
t.Errorf("%#v", tags)
}

testptrstringer := &testPtrStringer{}
stringer = Stringer(testptrstringer)
if !reflect.DeepEqual(stringer, testptrstringer) {
t.Errorf("%#v != %#v", stringer, testptrstringer)
}
if s := stringer.String(); s != (&testPtrStringer{}).String() {
if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{testptrstringer}) {
t.Errorf("%#v", tags)
}

b2 := Bind(nil, &stringer)
if s := b2.JawsGetString(nil); s != (&testPtrStringer{}).String() {
t.Error(s)
}
if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{testptrstringer}) {
t.Error(tags)
if tags := MustTagExpand(nil, b2); !reflect.DeepEqual(tags, []any{testptrstringer}) {
t.Errorf("%#v", tags)
}

b3 := Bind(nil, testptrstringer)
if s := b3.JawsGetString(nil); s != (&testPtrStringer{}).String() {
t.Error(s)
}
if tags := MustTagExpand(nil, b3); !reflect.DeepEqual(tags, []any{testptrstringer}) {
t.Errorf("%#v", tags)
}
}

0 comments on commit 81d7b34

Please sign in to comment.