-
Notifications
You must be signed in to change notification settings - Fork 0
/
typetohtml.go
134 lines (114 loc) · 5.32 KB
/
typetohtml.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package typetohtml
import (
"fmt"
"reflect"
"strconv"
"github.com/pjotrscholtze/go-bootstrap/cmd/go-bootstrap/htmlwrapper"
)
type typeMapperConv struct {
mappings map[string]func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm
customMappings map[string]func(fieldName string, refStruct interface{}) htmlwrapper.Elm
}
func (tmc *typeMapperConv) HasMapping(kind string) bool {
_, ok := tmc.mappings[kind]
return ok
}
func (tmc *typeMapperConv) HasCustomMapping(fieldName string) bool {
_, ok := tmc.customMappings[fieldName]
return ok
}
func (tmc *typeMapperConv) CustomMapToHTML(fieldName string, refStruct interface{}) (htmlwrapper.Elm, error) {
if !tmc.HasCustomMapping(fieldName) {
return nil, fmt.Errorf("Unkown field value given! No mapping avaiable!")
}
html := tmc.customMappings[fieldName](fieldName, refStruct)
return html, nil
}
// (kind string, fieldValue reflect.Value) (htmlwrapper.Elm, error)
func (tmc *typeMapperConv) MapToHTML(structField reflect.StructField, value reflect.Value, refStruct interface{}) (htmlwrapper.Elm, error) {
if !tmc.HasMapping(structField.Type.String()) {
return nil, fmt.Errorf("Unkown field value given! No mapping avaiable!")
}
html := tmc.mappings[structField.Type.String()](structField, value, refStruct)
return html, nil
}
type TypeMapperConv interface {
KnownTypes() []string
KnownCustomFields() []string
HasMapping(kind string) bool
HasCustomMapping(fieldName string) bool
// MapToHTML(kind string, fieldValue reflect.Value) (htmlwrapper.Elm, error)
MapToHTML(structField reflect.StructField, value reflect.Value, refStruct interface{}) (htmlwrapper.Elm, error)
CustomMapToHTML(fieldName string, refStruct interface{}) (htmlwrapper.Elm, error)
RegisterMapping(forKind string, fn func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm)
RegisterCustomFieldMapping(fieldName string, fn func(fieldName string, refStruct interface{}) htmlwrapper.Elm)
GetMapping(kind string) (func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm, error)
GetCustomMapping(fieldName string) (func(fieldName string, refStruct interface{}) htmlwrapper.Elm, error)
}
func (tmc *typeMapperConv) RegisterCustomFieldMapping(fieldName string, fn func(fieldName string, refStruct interface{}) htmlwrapper.Elm) {
tmc.customMappings[fieldName] = fn
}
func (tmc *typeMapperConv) KnownCustomFields() []string {
out := []string{}
for key := range tmc.customMappings {
out = append(out, key)
}
return out
}
func (tmc *typeMapperConv) KnownTypes() []string {
out := []string{}
for key := range tmc.mappings {
out = append(out, key)
}
return out
}
func (tmc *typeMapperConv) GetMapping(kind string) (func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm, error) {
val, ok := tmc.mappings[kind]
if !ok {
return nil, fmt.Errorf("Failed to find type mapping!")
}
return val, nil
}
func (tmc *typeMapperConv) GetCustomMapping(fieldName string) (func(fieldName string, refStruct interface{}) htmlwrapper.Elm, error) {
val, ok := tmc.customMappings[fieldName]
if !ok {
return nil, fmt.Errorf("Failed to find type mapping!")
}
return val, nil
}
func (tmc *typeMapperConv) RegisterMapping(forKind string, fn func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm) {
tmc.mappings[forKind] = fn
}
func NewTypeMapperConv() TypeMapperConv {
// html := tmc.mappings[structField.Type.String()](structField reflect.StructField, value reflect.Value)
// html := tmc.mappings[structField.Type.String()](value.FieldByName(structField.Name))
out := &typeMapperConv{
mappings: make(map[string]func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm),
customMappings: make(map[string]func(fieldName string, refStruct interface{}) htmlwrapper.Elm),
}
out.mappings["int64"] = func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm {
return &htmlwrapper.TextElm{Content: strconv.FormatInt(value.Int(), 10)}
}
out.mappings["int32"] = out.mappings["int64"]
out.mappings["int16"] = out.mappings["int64"]
out.mappings["int8"] = out.mappings["int64"]
out.mappings["int"] = out.mappings["int64"]
out.mappings["uint64"] = func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm {
return &htmlwrapper.TextElm{Content: strconv.FormatUint(value.Uint(), 10)}
}
out.mappings["uint32"] = out.mappings["uint64"]
out.mappings["uint16"] = out.mappings["uint64"]
out.mappings["uint8"] = out.mappings["uint64"]
out.mappings["uint"] = out.mappings["uint64"]
out.mappings["bool"] = func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm {
return &htmlwrapper.TextElm{Content: strconv.FormatBool(value.Bool())}
}
out.mappings["float32"] = func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm {
return &htmlwrapper.TextElm{Content: strconv.FormatFloat(value.Float(), 'f', 2, 32)}
}
out.mappings["float64"] = out.mappings["float32"]
out.mappings["string"] = func(structField reflect.StructField, value reflect.Value, refStruct interface{}) htmlwrapper.Elm {
return &htmlwrapper.TextElm{Content: value.String()}
}
return out
}