/
main.go
215 lines (171 loc) · 4.27 KB
/
main.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
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
package main
import (
"encoding/json"
"fmt"
"github.com/unrolled/render"
"go.wandrs.dev/binding"
httpw "go.wandrs.dev/http"
"go.wandrs.dev/inject"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"reflect"
)
type Person struct {
Name string `json:"name"`
}
type errorWrapper interface {
error
}
func h_no_return() {
}
func h_returns_error() error {
return nil
}
var _ error = &fs.PathError{}
func h_returns_custom_error() *fs.PathError {
return nil
}
func h_returns_custom_error_ptr() fs.PathError {
return fs.PathError{}
}
func h_returns_custom_error_interface() errorWrapper {
return nil
}
func h_returns_string() string {
return "handler"
}
func h_returns_int() int {
return 69
}
func h_returns_bool() bool {
return true
}
func h_returns_byte_array() []byte {
return []byte("handler")
}
func h_returns_string_array() []string {
// return []string{"handler"}
// return []string{}
return nil
}
func h_returns_int_array() []int {
return []int{69}
}
func h_returns_bool_array() []bool {
return []bool{true}
}
func h_returns_struct() Person {
return Person{Name: "John"}
}
func h_returns_slice() []Person {
return []Person{
{Name: "John"},
{Name: "Jane"},
}
}
func h_returns_struct_err() (Person, error) {
return Person{Name: "John"}, nil
}
func h_returns_too_many_returns() (int, Person, error) {
return http.StatusOK, Person{Name: "John"}, nil
}
func main_create_new_object() {
a := Person{
Name: "a",
}
fmt.Printf("%+v\n", a)
pt := reflect.TypeOf(a)
// creates new object of Pointer type
b_ptr := reflect.New(pt).Interface().(*Person)
b_ptr.Name = "b"
fmt.Printf("%+v\n", b_ptr)
// creates new object of Pointer type. We use .Elem() to get the value type
b := reflect.New(pt).Elem().Interface().(Person) // .Interface().(*Person2)
b.Name = "b"
fmt.Printf("%+v\n", b)
}
var (
errorType = reflect.TypeOf((*error)(nil)).Elem()
responseWriterType = reflect.TypeOf((*http.ResponseWriter)(nil)).Elem()
)
func main() {
// fn := h_returns_custom_error_interface
fn := h_returns_string_array
data, _ := json.Marshal(fn())
fmt.Println(string(data))
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
binding.Injector(render.New())(binding.HandlerFunc(fn)).ServeHTTP(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header.Get("Content-Type"))
fmt.Println(string(body))
}
func main__() {
var w http.ResponseWriter = httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
injector := inject.New()
injector.Map(w)
injector.Map(req)
injector.Map(req.Context())
injector.Map(httpw.NewResponseWriter(w, req, render.New()))
typ := reflect.TypeOf(h_returns_custom_error_interface) //h_returns_custom_error_ptr)
fmt.Println(typ.String())
if typ.Kind() == reflect.Func {
switch typ.NumOut() {
case 0:
return // nothing more to check
case 1:
// var err error
// fmt.Println(reflect.TypeOf((error)(nil)))
fmt.Println(errorType.String())
rtyp := typ.Out(0)
fmt.Println(rtyp.Implements(errorType))
fmt.Println(reflect.New(rtyp).Type().String())
fmt.Println(reflect.New(rtyp).Type().Implements(errorType))
case 2:
default:
panic(fmt.Sprintf("found %d return values, allow at most 2", typ.NumOut()))
}
}
}
func main_() {
fn := h_returns_custom_error
var w http.ResponseWriter = httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/", nil)
injector := inject.New()
injector.Map(w)
injector.Map(req)
injector.Map(req.Context())
injector.Map(httpw.NewResponseWriter(w, req, render.New()))
vals, err := injector.Invoke(fn)
if err != nil {
panic(err)
}
fmt.Println(len(vals))
ww := injector.GetVal(responseWriterType).Interface().(httpw.ResponseWriter)
fmt.Println(ww)
typ := reflect.TypeOf(fn)
if typ.Kind() == reflect.Func {
switch typ.NumOut() {
case 0:
return // nothing more to check
case 1:
// var err error
// fmt.Println(reflect.TypeOf((error)(nil)))
fmt.Println(errorType.String())
rtyp := typ.Out(0)
fmt.Println(rtyp.Implements(errorType))
case 2:
rtyp := typ.Out(1)
if !rtyp.Implements(errorType) {
panic("2nd return value must implement error")
}
default:
panic(fmt.Sprintf("found %d return values, allow at most 2", typ.NumOut()))
}
}
}