From eddd630982c6e3f5830d3e1a3e9dc0d2902da872 Mon Sep 17 00:00:00 2001 From: Johan Lindh Date: Tue, 17 Dec 2024 14:07:41 +0100 Subject: [PATCH] remove Fmt, HTMLer ToHTML and Stringer --- fmt.go | 37 ------------------------- htmler.go | 54 ------------------------------------- htmler_test.go | 38 -------------------------- stringer.go | 33 ----------------------- stringer_test.go | 70 ------------------------------------------------ tohtml.go | 37 ------------------------- tohtml_test.go | 20 -------------- ui_test.go | 4 +++ 8 files changed, 4 insertions(+), 289 deletions(-) delete mode 100644 fmt.go delete mode 100644 htmler.go delete mode 100644 htmler_test.go delete mode 100644 stringer.go delete mode 100644 stringer_test.go delete mode 100644 tohtml.go delete mode 100644 tohtml_test.go diff --git a/fmt.go b/fmt.go deleted file mode 100644 index c6cec70..0000000 --- a/fmt.go +++ /dev/null @@ -1,37 +0,0 @@ -package jaws - -import "fmt" - -type stringizer[T any] struct { - f string - v *T -} - -func (s stringizer[T]) String() string { - if s.f == "" { - return fmt.Sprint(*s.v) - } - return fmt.Sprintf(s.f, *s.v) -} - -func (s stringizer[T]) JawsGetTag(*Request) any { - return s.v -} - -// Fmt returns a fmt.Stringer using fmt.Sprintf(formatting, *T). -// If formatting is omitted and *T or T implements fmt.Stringer, it will be returned as-is. -// If formatting is omitted fmt.Sprint(*T) is used. -func Fmt[T any](p *T, formatting ...string) fmt.Stringer { - var f string - if len(formatting) > 0 { - f = formatting[0] - } else { - if x, ok := any(*p).(fmt.Stringer); ok { - return x - } - if x, ok := any(p).(fmt.Stringer); ok { - return x - } - } - return stringizer[T]{f, p} -} diff --git a/htmler.go b/htmler.go deleted file mode 100644 index 39828fe..0000000 --- a/htmler.go +++ /dev/null @@ -1,54 +0,0 @@ -package jaws - -import ( - "fmt" - "html" - "html/template" - "sync" -) - -type htmler struct { - l sync.Locker - f string - args []any -} - -func (h htmler) JawsGetHTML(e *Element) template.HTML { - if rl, ok := h.l.(RWLocker); ok { - rl.RLock() - defer rl.RUnlock() - } else { - h.l.Lock() - defer h.l.Unlock() - } - var args []any - for _, arg := range h.args { - switch x := arg.(type) { - case string: - arg = html.EscapeString(x) - case fmt.Stringer: - arg = html.EscapeString(x.String()) - } - args = append(args, arg) - } - return template.HTML( /*#nosec G203*/ fmt.Sprintf(h.f, args...)) -} - -func (h htmler) JawsGetTag(*Request) any { - var tags []any - for _, arg := range h.args { - if _, ok := arg.(fmt.Stringer); ok { - tags = append(tags, arg) - } - } - return tags -} - -// HTMLer return a lock protected jaws.HTMLGetter using the given formatting -// and arguments. Arguments of type string or fmt.Stringer will be escaped -// using html.EscapeString(). -// -// Returns all fmt.Stringer arguments as UI tags. -func HTMLer(l sync.Locker, formatting string, args ...any) HTMLGetter { - return htmler{l, formatting, args} -} diff --git a/htmler_test.go b/htmler_test.go deleted file mode 100644 index 231005f..0000000 --- a/htmler_test.go +++ /dev/null @@ -1,38 +0,0 @@ -package jaws - -import ( - "reflect" - "sync" - "testing" -) - -func TestHTMLer(t *testing.T) { - var mu sync.Mutex - var mu2 sync.RWMutex - - txt := "" - htmler := HTMLer(&mu, "

%s

", Fmt(&txt)) - if s := htmler.JawsGetHTML(nil); s != "

<text>

" { - t.Error(s) - } - tags, err := TagExpand(nil, htmler) - if err != nil { - t.Error(err) - } - if !reflect.DeepEqual(tags, []any{&txt}) { - t.Error(tags) - } - - num := 123 - htmler = HTMLer(&mu2, "

%s%s%s

", Fmt(&txt), "", Fmt(&num)) - if s := htmler.JawsGetHTML(nil); s != "

<text><!>123

" { - t.Error(s) - } - tags, err = TagExpand(nil, htmler) - if err != nil { - t.Error(err) - } - if !reflect.DeepEqual(tags, []any{&txt, &num}) { - t.Error(tags) - } -} diff --git a/stringer.go b/stringer.go deleted file mode 100644 index 6fa57ec..0000000 --- a/stringer.go +++ /dev/null @@ -1,33 +0,0 @@ -package jaws - -import ( - "fmt" - "sync" -) - -type lockedstringer struct { - l sync.Locker - s fmt.Stringer -} - -func (s lockedstringer) String() (value string) { - if rl, ok := s.l.(RWLocker); ok { - rl.RLock() - defer rl.RUnlock() - } else { - s.l.Lock() - defer s.l.Unlock() - } - return s.s.String() -} - -func (s lockedstringer) JawsGetTag(*Request) any { - return s.s -} - -// Stringer returns a lock protected fmt.Stringer using fmt.Sprintf(formatting, *T). -// If formatting is omitted fmt.Sprint(*T) is used. -// If formatting is omitted and *T or T implements fmt.Stringer, that will be used instead of fmt.Sprint. -func Stringer[T any](l sync.Locker, p *T, formatting ...string) fmt.Stringer { - return lockedstringer{l, Fmt(p, formatting...)} -} diff --git a/stringer_test.go b/stringer_test.go deleted file mode 100644 index 73f1157..0000000 --- a/stringer_test.go +++ /dev/null @@ -1,70 +0,0 @@ -package jaws - -import ( - "reflect" - "sync" - "testing" -) - -type testStringer struct{} - -func (s testStringer) String() string { - return "" -} - -type testPtrStringer struct{} - -func (s *testPtrStringer) String() string { - return "" -} - -func TestStringer(t *testing.T) { - var mu sync.Mutex - var mu2 sync.RWMutex - - txt := "text" - stringer := Stringer(&mu, &txt) - if s := stringer.String(); s != "text" { - t.Error(s) - } - if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{&txt}) { - t.Error(tags) - } - - num := int(123) - stringer = Stringer(&mu, &num) - if s := stringer.String(); s != "123" { - t.Error(s) - } - if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{&num}) { - t.Error(tags) - } - - flt := float64(456.123) - stringer = Stringer(&mu, &flt, "%.0f") - if s := stringer.String(); s != "456" { - t.Error(s) - } - if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{&flt}) { - t.Error(tags) - } - - teststringer := testStringer{} - stringer = Stringer(&mu, &teststringer) - if s := stringer.String(); s != (testStringer{}).String() { - t.Error(s) - } - if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{teststringer}) { - t.Errorf("%#v", tags) - } - - testptrstringer := &testPtrStringer{} - stringer = Stringer(&mu2, testptrstringer) - if s := stringer.String(); s != (&testPtrStringer{}).String() { - t.Error(s) - } - if tags := MustTagExpand(nil, stringer); !reflect.DeepEqual(tags, []any{testptrstringer}) { - t.Errorf("%#v", tags) - } - -} diff --git a/tohtml.go b/tohtml.go deleted file mode 100644 index a5df558..0000000 --- a/tohtml.go +++ /dev/null @@ -1,37 +0,0 @@ -package jaws - -import ( - "fmt" - "html" - "html/template" -) - -type formathtml struct { - s fmt.Stringer - f string -} - -func (fh formathtml) JawsGetHTML(*Element) template.HTML { - s := html.EscapeString(fh.s.String()) - if fh.f != "" { - s = fmt.Sprintf(fh.f, s) - } - return template.HTML(s) //#nosec G203 -} - -func (fh formathtml) JawsGetTag(*Request) any { - return fh.s -} - -// ToHTML return a jaws.HTMLGetter using the given stringer with -// the string returned from the stringer escaped using html.EscapeString. -// -// If formatting is provided the escaped result is passed to fmt.Sprintf(formatting, escapedstring). -// Make sure any provided formatting produces correct HTML. -func ToHTML(stringer fmt.Stringer, formatting ...string) HTMLGetter { - var f string - if len(formatting) > 0 { - f = formatting[0] - } - return formathtml{stringer, f} -} diff --git a/tohtml_test.go b/tohtml_test.go deleted file mode 100644 index 401a105..0000000 --- a/tohtml_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package jaws - -import ( - "reflect" - "sync" - "testing" -) - -func TestToHTML(t *testing.T) { - var mu sync.Mutex - - txt := "

text

" - htmler := ToHTML(Stringer(&mu, &txt), "
%s
") - if s := htmler.JawsGetHTML(nil); s != "
<p>text</p>
" { - t.Error(s) - } - if tags := MustTagExpand(nil, htmler); !reflect.DeepEqual(tags, []any{&txt}) { - t.Error(tags) - } -} diff --git a/ui_test.go b/ui_test.go index 984ec70..8c2a245 100644 --- a/ui_test.go +++ b/ui_test.go @@ -47,6 +47,10 @@ func TestRequest_NewElement_DebugPanicsIfNotComparable(t *testing.T) { t.Fail() } +type testStringer struct{} + +func (testStringer) String() string { return "foo" } + func TestRequest_JawsRender_DebugOutput(t *testing.T) { is := newTestHelper(t) rq := newTestRequest()