From 038bfb46afd09a99946e2fc169f40481646105d4 Mon Sep 17 00:00:00 2001 From: Johan Lindh Date: Wed, 18 Dec 2024 07:25:23 +0100 Subject: [PATCH] add StringGetterFunc --- stringgetterfunc.go | 19 +++++++++++++++++++ stringgetterfunc_test.go | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 stringgetterfunc.go create mode 100644 stringgetterfunc_test.go 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) + } +}