-
Notifications
You must be signed in to change notification settings - Fork 1
/
tag.go
104 lines (90 loc) · 1.94 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package jaws
import (
"fmt"
"html/template"
"reflect"
)
type Tag string
func TagString(tag any) string {
if rv := reflect.ValueOf(tag); rv.IsValid() {
if rv.Kind() == reflect.Pointer {
return fmt.Sprintf("%T(%p)", tag, tag)
} else if stringer, ok := tag.(fmt.Stringer); ok {
return fmt.Sprintf("%T(%s)", tag, stringer.String())
}
}
return fmt.Sprintf("%#v", tag)
}
type errTooManyTags struct{}
func (errTooManyTags) Error() string {
return "too many tags"
}
var ErrTooManyTags = errTooManyTags{}
type errIllegalTagType struct {
tag any
}
func (e errIllegalTagType) Error() string {
return fmt.Sprintf("illegal tag type %T", e.tag)
}
func (errIllegalTagType) Is(other error) bool {
return other == ErrIllegalTagType
}
var ErrIllegalTagType = errIllegalTagType{}
func tagExpand(l int, rq *Request, tag any, result []any) ([]any, error) {
if l > 10 || len(result) > 100 {
return result, ErrTooManyTags
}
switch data := tag.(type) {
case string:
case template.HTML:
case int:
case int8:
case int16:
case int32:
case int64:
case uint:
case uint8:
case uint16:
case uint32:
case uint64:
case float32:
case float64:
case bool:
case error:
case []string:
case []template.HTML:
case nil:
return result, nil
case []Tag:
for _, v := range data {
result = append(result, v)
}
return result, nil
case TagGetter:
if newTag := data.JawsGetTag(rq); tag != newTag {
return tagExpand(l+1, rq, newTag, result)
}
return append(result, tag), nil
case []any:
var err error
for _, v := range data {
if result, err = tagExpand(l+1, rq, v, result); err != nil {
break
}
}
return result, err
default:
return append(result, data), nil
}
return result, errIllegalTagType{tag: tag}
}
func TagExpand(rq *Request, tag any) ([]any, error) {
return tagExpand(0, rq, tag, nil)
}
func MustTagExpand(rq *Request, tag any) []any {
result, err := TagExpand(rq, tag)
if err != nil {
panic(err)
}
return result
}