diff --git a/stringgetterfunc.go b/stringgetterfunc.go new file mode 100644 index 0000000..86ef503 --- /dev/null +++ b/stringgetterfunc.go @@ -0,0 +1,19 @@ +package jaws + +type stringGetterFunc struct { + fn func(*Element) string + tags []any +} + +func (g *stringGetterFunc) JawsGet(e *Element) string { + return g.fn(e) +} + +func (g *stringGetterFunc) JawsGetTag(e *Request) any { + return g.tags +} + +// StringGetterFunc wraps a function and returns a Getter[string] +func StringGetterFunc(fn func(elem *Element) (s string), tags ...any) Getter[string] { + return &stringGetterFunc{fn: fn, tags: tags} +} diff --git a/stringgetterfunc_test.go b/stringgetterfunc_test.go new file mode 100644 index 0000000..3df2e03 --- /dev/null +++ b/stringgetterfunc_test.go @@ -0,0 +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.JawsGet(nil); s != "foo" { + t.Error(s) + } + if tags := MustTagExpand(nil, sg); !reflect.DeepEqual(tags, []any{tt}) { + t.Error(tags) + } +}