-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrud.go
106 lines (102 loc) · 3.12 KB
/
crud.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
package http
import (
"reflect"
)
type CRUDHandlerer interface {
Name() string
Elem() interface{}
Create(data interface{}) error
Update(id int64, data interface{}) error
Delete(id int64) error
Retrieve(limit, offset int64) (interface{}, int64, error)
}
func (this *Router) CRUD(h CRUDHandlerer) {
this.Handle("Create "+h.Name(), "/create", func() interface{} {
handler := reflect.FuncOf(
[]reflect.Type{reflect.TypeOf(h.Elem())},
[]reflect.Type{reflect.TypeOf(&Status{})},
false,
)
return reflect.MakeFunc(handler, func(args []reflect.Value) []reflect.Value {
if err := h.Create(args[0].Interface()); err != nil {
Error(err)
return []reflect.Value{reflect.ValueOf(STATUS_ERROR_DB)}
}
var nilret *Status
return []reflect.Value{reflect.ValueOf(nilret)}
}).Interface()
}())
this.Handle("Update "+h.Name(), "/update", func() interface{} {
inStruct := reflect.StructOf([]reflect.StructField{
{
Name: "Id",
Type: reflect.TypeOf(int64(0)),
Tag: `json:"id" valid:"(0,),message=invalid id"`,
},
{
Name: "Data",
Type: reflect.TypeOf(h.Elem()),
Tag: `json:"data" valid:"message=invalid data"`,
},
})
handler := reflect.FuncOf(
[]reflect.Type{reflect.PtrTo(inStruct)},
[]reflect.Type{reflect.TypeOf(&Status{})},
false,
)
return reflect.MakeFunc(handler, func(args []reflect.Value) []reflect.Value {
if err := h.Update(
args[0].Elem().FieldByName("Id").Int(),
args[0].Elem().FieldByName("Data").Interface(),
); err != nil {
return []reflect.Value{reflect.ValueOf(STATUS_ERROR_DB)}
}
var nilret *Status
return []reflect.Value{reflect.ValueOf(nilret)}
}).Interface()
}())
this.Handle("Delete "+h.Name(), "/delete", func(in *struct {
Id int64 `json:"id" valid:"(0,),message=invalid id"`
}) *Status {
if err := h.Delete(in.Id); err != nil {
return STATUS_ERROR_DB
}
return nil
})
this.Handle("Retrieve "+h.Name(), "/retrieve", func() interface{} {
var in *struct {
Size int64 `json:"size" valid:"message=invalid size"`
Offset int64 `json:"offset" valid:"message=invalid offset"`
}
outStruct := reflect.StructOf([]reflect.StructField{
{
Name: "Total",
Type: reflect.TypeOf(int64(0)),
Tag: `json:"total" comment:"数据总量,用于分页,根据约定返回"`,
},
{
Name: "List",
Type: reflect.SliceOf(reflect.TypeOf(h.Elem())),
Tag: `json:"list" comment:"数据列表"`,
},
})
handler := reflect.FuncOf(
[]reflect.Type{reflect.TypeOf(in), reflect.PtrTo(outStruct)},
[]reflect.Type{reflect.TypeOf(&Status{})},
false,
)
return reflect.MakeFunc(handler, func(args []reflect.Value) []reflect.Value {
if data, lastIdOrOffset, err := h.Retrieve(
args[0].Elem().FieldByName("Size").Int(),
args[0].Elem().FieldByName("Offset").Int(),
); err != nil {
return []reflect.Value{reflect.ValueOf(STATUS_ERROR_DB)}
} else {
args[1].Elem().FieldByName("Total").Set(reflect.ValueOf(lastIdOrOffset))
args[1].Elem().FieldByName("List").Set(reflect.ValueOf(data).Elem())
}
var nilret *Status
return []reflect.Value{reflect.ValueOf(nilret)}
}).Interface()
}())
}