-
Notifications
You must be signed in to change notification settings - Fork 5
/
view.go
97 lines (83 loc) · 2.1 KB
/
view.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
package fiber
import (
"fmt"
"reflect"
"github.com/gofiber/fiber/v2"
contractshttp "github.com/goravel/framework/contracts/http"
)
type View struct {
instance *fiber.Ctx
}
func NewView(instance *fiber.Ctx) *View {
return &View{instance: instance}
}
func (receive *View) Make(view string, data ...any) contractshttp.Response {
shared := ViewFacade.GetShared()
if len(data) == 0 {
return &HtmlResponse{shared, receive.instance, view}
} else {
dataType := reflect.TypeOf(data[0])
switch dataType.Kind() {
case reflect.Struct:
dataMap := structToMap(data[0])
for key, value := range dataMap {
shared[key] = value
}
return &HtmlResponse{shared, receive.instance, view}
case reflect.Map:
fillShared(data[0], shared)
return &HtmlResponse{data[0], receive.instance, view}
default:
panic(fmt.Sprintf("make %s view failed, data must be map or struct", view))
}
}
}
func (receive *View) First(views []string, data ...any) contractshttp.Response {
for _, view := range views {
if ViewFacade.Exists(view) {
return receive.Make(view, data...)
}
}
panic("no view exists")
}
func structToMap(data any) map[string]any {
res := make(map[string]any)
modelType := reflect.TypeOf(data)
modelValue := reflect.ValueOf(data)
if modelType.Kind() == reflect.Pointer {
modelType = modelType.Elem()
modelValue = modelValue.Elem()
}
for i := 0; i < modelType.NumField(); i++ {
if !modelType.Field(i).IsExported() {
continue
}
dbColumn := modelType.Field(i).Name
if modelValue.Field(i).Kind() == reflect.Pointer {
if modelValue.Field(i).IsNil() {
res[dbColumn] = nil
} else {
res[dbColumn] = modelValue.Field(i).Elem().Interface()
}
} else {
res[dbColumn] = modelValue.Field(i).Interface()
}
}
return res
}
func fillShared(data any, shared map[string]any) {
dataValue := reflect.ValueOf(data)
keys := dataValue.MapKeys()
for key, value := range shared {
exist := false
for _, k := range keys {
if k.String() == key {
exist = true
break
}
}
if !exist {
dataValue.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
}
}
}