diff --git a/tag.go b/tag.go index 52e5bfa..0d327d7 100644 --- a/tag.go +++ b/tag.go @@ -41,6 +41,17 @@ func (errIllegalTagType) Is(other error) bool { var ErrIllegalTagType = errIllegalTagType{} +func checkHashable(tag any) (yes bool) { + defer func() { + if recover() == nil { + yes = true + } + }() + tmp := map[any]struct{}{} + tmp[tag] = struct{}{} + return +} + func tagExpand(l int, rq *Request, tag any, result []any) ([]any, error) { if l > 10 || len(result) > 100 { return result, ErrTooManyTags @@ -48,6 +59,7 @@ func tagExpand(l int, rq *Request, tag any, result []any) ([]any, error) { switch data := tag.(type) { case string: case template.HTML: + case template.HTMLAttr: case int: case int8: case int16: @@ -62,8 +74,6 @@ func tagExpand(l int, rq *Request, tag any, result []any) ([]any, error) { case float64: case bool: case error: - case []string: - case []template.HTML: case nil: return result, nil @@ -86,7 +96,9 @@ func tagExpand(l int, rq *Request, tag any, result []any) ([]any, error) { } return result, err default: - return append(result, data), nil + if checkHashable(data) { + return append(result, data), nil + } } return result, errIllegalTagType{tag: tag} } diff --git a/tag_test.go b/tag_test.go index fd89f4d..813410f 100644 --- a/tag_test.go +++ b/tag_test.go @@ -56,6 +56,7 @@ func TestTagExpand_IllegalTypesPanic(t *testing.T) { tags := []any{ string("string"), template.HTML("template.HTML"), + template.HTMLAttr("template.HTMLAttr"), int(1), int8(2), int16(3), @@ -72,6 +73,7 @@ func TestTagExpand_IllegalTypesPanic(t *testing.T) { errors.New("error"), []string{"a", "b"}, []template.HTML{"a", "b"}, + map[int]int{1: 1}, } for _, tag := range tags { t.Run(fmt.Sprintf("%T", tag), func(t *testing.T) { @@ -79,13 +81,13 @@ func TestTagExpand_IllegalTypesPanic(t *testing.T) { x := recover() e, ok := x.(error) if !ok { - t.Fail() + t.FailNow() } if !errors.Is(e, ErrIllegalTagType) { - t.Fail() + t.FailNow() } if !strings.Contains(e.Error(), fmt.Sprintf("%T", tag)) { - t.Fail() + t.FailNow() } }() MustTagExpand(nil, tag)