-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrouter.go
130 lines (113 loc) · 3.05 KB
/
router.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
package main
import (
"net/http"
)
type Router struct {
name string
routes []*Route
}
// func (r *Router) Query(opId string, fn interface{}) *Route {
// // todo: should I return error?
// inpType, outType, err := validateAndRetrieveHandlerParamType(fn)
// if err != nil {
// panic(err)
// }
// handler := &Handler{inpType, outType, fn}
// route := &Route{
// handler: handler,
// routeConfig: &RouteConfig{
// opId: opId,
// opType: "query",
// method: http.MethodGet,
// url: "/v1/" + opId,
// jsonSchema: &JSONSchemaForInputAndOutput{
// input: jsonschema.ReflectFromType(inpType),
// output: jsonschema.ReflectFromType(outType),
// },
// }}
// r.routes = append(r.routes, route)
// return route
// }
func (r *Router) Query(items ...interface{}) *Route {
var operationId string
var function interface{}
var route *Route
// if the Query function is called with operationID
if len(items) >= 1 {
operationId = items[0].(string)
// prepare the config.
// todo: can be refactored into a prepareConfig func
var config RouteConfig
config.opId = operationId
config.opType = "query"
config.method = http.MethodGet
config.url = "/v1/" + operationId
route = NewRoute(&config)
}
// if the function is called alongwith the Handler fn
if len(items) == 2 {
function = items[1]
route = route.Fn(function)
} else if len(items) < 1 || len(items) > 2 {
panic("Illegal number of arguments provided to Query")
}
r.routes = append(r.routes, route)
return route
}
func (r *Router) Mutation(items ...interface{}) *Route {
var operationId string
var function interface{}
var route *Route
// if the Mutation function is called with operationID
if len(items) >= 1 {
operationId = items[0].(string)
var config RouteConfig
config.opId = operationId
config.opType = "mutation"
config.method = http.MethodPost
config.url = "/v1/" + operationId
route = NewRoute(&config)
}
// if the function is called alongwith the Handler fn
if len(items) == 2 {
function = items[1]
route = route.Fn(function)
} else if len(items) < 1 || len(items) > 2 {
panic("Illegal number of arguments provided to Mutation")
}
r.routes = append(r.routes, route)
return route
}
// For now, this wrapper is not needed
// func wrapper(fn interface{}) func(context.Context, interface{}) []interface{} {
// fnValue := reflect.ValueOf(fn)
// return func(ctx context.Context, i interface{}) []interface{} {
// args := []reflect.Value{
// reflect.ValueOf(ctx),
// reflect.ValueOf(i),
// }
// result := fnValue.Call(args)
// if len(result) == 1 {
// err := result[0].Interface()
// return []interface{}{
// err,
// }
// } else if len(result) == 2 {
// res := result[0].Interface()
// err := result[1].Interface()
// if err != nil {
// return []interface{}{
// nil,
// err,
// }
// }
// return []interface{}{
// res,
// nil,
// }
// }
// return []interface{}{
// fmt.Errorf("Invalid function signature: expected (context.Context, struct) (struct, error)"),
// }
// }
// }