Skip to content

Commit

Permalink
allow tags with xxxGetterFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Nov 29, 2024
1 parent 1716735 commit 809435b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
15 changes: 11 additions & 4 deletions htmlgetterfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ package jaws
import "html/template"

type htmlGetterFunc struct {
fn func(*Element) template.HTML
fn func(*Element) template.HTML
tags []any
}

func (g htmlGetterFunc) JawsGetHtml(e *Element) template.HTML {
var _ TagGetter = &htmlGetterFunc{}

func (g *htmlGetterFunc) JawsGetHtml(e *Element) template.HTML {
return g.fn(e)
}

func (g *htmlGetterFunc) JawsGetTag(e *Request) any {
return g.tags
}

// HtmlGetterFunc wraps a function and returns a HtmlGetter.
func HtmlGetterFunc(fn func(*Element) template.HTML) HtmlGetter {
return htmlGetterFunc{fn: fn}
func HtmlGetterFunc(fn func(*Element) template.HTML, tags ...any) HtmlGetter {
return &htmlGetterFunc{fn: fn, tags: tags}
}
7 changes: 6 additions & 1 deletion htmlgetterfunc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package jaws

import (
"html/template"
"reflect"
"testing"
)

func TestHtmlGetterFunc(t *testing.T) {
tt := &testSelfTagger{}
hg := HtmlGetterFunc(func(e *Element) template.HTML {
return "foo"
})
}, tt)
if s := hg.JawsGetHtml(nil); s != "foo" {
t.Error(s)
}
if tags := MustTagExpand(nil, hg); !reflect.DeepEqual(tags, []any{tt}) {
t.Error(tags)
}
}
13 changes: 9 additions & 4 deletions stringgetterfunc.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package jaws

type stringGetterFunc struct {
fn func(*Element) string
fn func(*Element) string
tags []any
}

func (g stringGetterFunc) JawsGetString(e *Element) string {
func (g *stringGetterFunc) JawsGetString(e *Element) string {
return g.fn(e)
}

func (g *stringGetterFunc) JawsGetTag(e *Request) any {
return g.tags
}

// StringGetterFunc wraps a function and returns a StringGetter.
func StringGetterFunc(fn func(*Element) string) StringGetter {
return stringGetterFunc{fn: fn}
func StringGetterFunc(fn func(*Element) string, tags ...any) StringGetter {
return &stringGetterFunc{fn: fn, tags: tags}
}
7 changes: 6 additions & 1 deletion stringgetterfunc_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package jaws

import (
"reflect"
"testing"
)

func TestStringGetterFunc(t *testing.T) {
tt := &testSelfTagger{}
sg := StringGetterFunc(func(e *Element) string {
return "foo"
})
}, tt)
if s := sg.JawsGetString(nil); s != "foo" {
t.Error(s)
}
if tags := MustTagExpand(nil, sg); !reflect.DeepEqual(tags, []any{tt}) {
t.Error(tags)
}
}

0 comments on commit 809435b

Please sign in to comment.