Skip to content

Commit

Permalink
add StringGetterFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Dec 18, 2024
1 parent a260b31 commit 038bfb4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
19 changes: 19 additions & 0 deletions stringgetterfunc.go
Original file line number Diff line number Diff line change
@@ -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}
}
19 changes: 19 additions & 0 deletions stringgetterfunc_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 038bfb4

Please sign in to comment.