Repository navigation
Expand file tree
/
Copy pathhandler.go
More file actions
235 lines (213 loc) · 5.46 KB
/
Copy pathhandler.go
File metadata and controls
235 lines (213 loc) · 5.46 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package api
import (
"reflect"
"github.com/ohait/forego/api/openapi"
"github.com/ohait/forego/ctx"
"github.com/ohait/forego/ctx/log"
)
// each type of object T has its own handler
type Handler[T any] struct {
typ reflect.Type
auth *field
init map[int]reflect.Value
in []field
out []field
}
// helper for NewHandler().Server()
func NewServer[T any](c ctx.C, init T) (Server[T], error) {
h, err := NewHandler(c, init)
return Server[T]{h}, err
}
// helper for NewHandler().Client()
func NewClient[T any](c ctx.C, init T) (Client[T], error) {
h, err := NewHandler(c, init)
return Client[T]{h}, err
}
// we accept either Type or *Type
func NewHandler[T any](c ctx.C, init T) (Handler[T], error) {
log.Debugf(c, "NewHandler[%T]", init)
initV := reflect.ValueOf(init)
this := Handler[T]{
init: map[int]reflect.Value{},
}
if initV.Kind() != reflect.Pointer {
return this, ctx.NewErrorf(c, "expected *struct, got %T", init)
}
if initV.IsZero() {
initV = reflect.New(reflect.TypeOf(init).Elem())
}
//log.Debugf(c, "initV: %+v", initV)
initV = initV.Elem()
this.typ = initV.Type()
if this.typ.Kind() != reflect.Struct {
return this, ctx.NewErrorf(c, "expected *struct, got %T", init)
}
// TODO(oha) use init as initializer
for i := 0; i < this.typ.NumField(); i++ {
ft := this.typ.Field(i)
tag, err := parseTags(c, ft)
if err != nil {
return this, err
}
//log.Debugf(c, "%v.%s %+v", this.typ, ft.Name, tag)
f := field{i, tag}
if tag.auth {
if this.auth != nil {
return this, ctx.NewErrorf(c, "only 1 auth field is supported: %T.%s", init, ft.Name)
}
this.auth = &f
}
if tag.in {
this.in = append(this.in, f)
}
if tag.out {
this.out = append(this.out, f)
}
if !tag.in && !tag.out {
v := initV.Field(f.i)
if !v.IsZero() {
this.init[f.i] = v
//log.Debugf(c, "init %v.%s = %#v", this.typ, f.tag.name, v)
}
}
}
return this, nil
}
func (this *Handler[T]) Type() reflect.Type {
return this.typ
}
func (this *Handler[T]) UpdateOpenAPI(c ctx.C, o *openapi.Service, path string) (*openapi.PathItem, error) {
in := &openapi.Schema{
Type: "object",
Format: this.typ.Name(),
Properties: map[string]*openapi.Schema{},
}
out := &openapi.Schema{
Type: "object",
Format: this.typ.Name(),
Properties: map[string]*openapi.Schema{},
}
for _, f := range this.in {
ft := this.typ.Field(f.i)
s, err := o.SchemaFromType(c, ft.Type, &ft.Tag)
if err != nil {
return nil, ctx.NewErrorf(c, "field[%q]: %w", f.tag.name, err)
}
in.Properties[f.tag.name] = s
if f.tag.required {
in.Required = append(in.Required, f.tag.name)
}
}
for _, f := range this.out {
ft := this.typ.Field(f.i)
s, err := o.SchemaFromType(c, ft.Type, &ft.Tag)
if err != nil {
return nil, ctx.NewErrorf(c, "field[%q]: %w", f.tag.name, err)
}
out.Properties[f.tag.name] = s
}
pi := &openapi.PathItem{
Summary: this.typ.PkgPath() + "." + this.typ.Name(),
RequestBody: &openapi.RequestBody{
Content: map[string]openapi.MediaType{
"application/json": {
Schema: in,
},
},
},
Responses: map[string]openapi.Response{
"200": {
Content: map[string]openapi.Content{
"application/json": {
Schema: out,
},
},
},
},
}
if this.auth != nil {
pi.SetJWT(this.auth.tag.required)
}
o.Paths[path] = &openapi.Path{
Post: pi,
}
// NOTE(oha): should we add GET if len(this.in) == 0?
return pi, nil
}
func (this *Handler[T]) Server() Server[T] {
return Server[T]{*this}
}
func (this *Handler[T]) Client() Client[T] {
return Client[T]{*this}
}
type field struct {
i int
tag tag
}
type Server[T any] struct {
Handler[T]
}
type Client[T any] struct {
Handler[T]
}
func (this Client[T]) Send(c ctx.C, obj T, data ClientRequest) error {
v := reflect.ValueOf(obj).Elem()
for _, f := range this.in {
fv := v.Field(f.i)
err := data.Marshal(c, f.tag.name, fv)
if err != nil {
return ctx.NewErrorf(c, "can't SendRequest %T.%s: %w", obj, f.tag.name, err)
}
}
return nil
}
func (this Server[T]) Recv(c ctx.C, req ServerRequest) (T, error) {
var zero T
log.Debugf(c, "Server[%T].Recv(%+v)", zero, req)
ptrV := reflect.New(this.typ)
v := ptrV.Elem()
for i, fv := range this.init {
v.Field(i).Set(fv)
//log.Debugf(c, "init %T.%v = %#v", this.typ, this.typ.Field(i).Name, fv)
}
for _, f := range this.in {
fv := v.Field(f.i)
err := req.Unmarshal(c, f.tag.name, fv)
if err != nil {
return zero, ctx.NewErrorf(c, "can't RecvRequest %T.%s: %w", zero, f.tag.name, err)
}
if f.tag.required && fv.IsZero() {
return zero, ctx.NewErrorf(c, "missing field %q in request", f.tag.name)
}
}
if this.auth != nil {
fv := v.Field(this.auth.i)
err := req.Auth(c, fv, this.auth.tag.required)
if err != nil {
return zero, ctx.NewErrorf(c, "can't RecvRequest %T Auth(): %w", zero, err)
}
}
return v.Addr().Interface().(T), nil
}
func (this Server[T]) Send(c ctx.C, obj T, res ServerResponse) (err error) {
v := reflect.ValueOf(obj).Elem()
for _, f := range this.out {
fv := v.Field(f.i)
err := res.Marshal(c, f.tag.name, fv)
if err != nil {
return ctx.NewErrorf(c, "can't SendResponse %T.%s: %w", obj, f.tag.name, err)
}
}
return nil
}
func (this Client[T]) Recv(c ctx.C, res ClientResponse, into T) (err error) {
v := reflect.ValueOf(into).Elem()
for _, f := range this.out {
fv := v.Field(f.i)
err := res.Unmarshal(c, f.tag.name, fv)
if err != nil {
return ctx.NewErrorf(c, "can't RecvResponse %T.%s: %w", into, f.tag.name, err)
}
}
return nil
}