Repository navigation
Expand file tree
/
Copy pathrip.slide
More file actions
533 lines (335 loc) · 15.4 KB
/
Copy pathrip.slide
File metadata and controls
533 lines (335 loc) · 15.4 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# RIP
REST in Peace
3 Feb 2024
Tags: web, REST, RESTful, golang, Go, generics
Tanguy Herrmann
Senior Software Engineer, Tuxago⧓
https://hachyderm.io/@dolanor
https://github.com/dolanor/rip
: Hello everybody
: I'm gonna talk to you about a project of mine I called, RIP
## Genesis
.image assets/govircon-2021-generics.jpeg _ 1000
: in 2021, Robert Griesemer and Ian Lance Taylor gave a talk on how and when to use generics
: Ian implemented several version of generics (I believe more than 6), so he knew
: the pros and cons of the current implementation
: it was still the best implementation of it, for the Go language
: and he gave some advice on how to use them
## Ian's advice for generics in Go
.video assets/wise-generics.mp4 video/mp4 550 _
: his advice was <play video> `to use the generics wisely`
: of course, when some authority figures tells you "to use something wisely", what do you do?
: the exact opposite! Because, F Authority, that's why
## CrowdStrike's Generics Challenge
.image assets/crowdstrike-challenge.png _ 800
[Original Post](https://discord.com/channels/755435423177638059/918280718013054986/918280720005337158)
: so, some mischievous security gophers from CrowdStrike had a challenge idea
: as if none of those security people are to be trusted
: *point finger to the crowd* I see you
: we had a lot of fun, and awful things: monads, Perl style
## Generics Challenge: Async/Await
.image assets/async-await.png _ 1000
[Original Post](https://discord.com/channels/755435423177638059/918280718013054986/918311335144587304)
[go.dev/play link](https://gotipplay.golang.org/p/dZUeHixD7Ua)
: this, is, my personal attempt to make the World a Worse Place
: an async/await in Go
## Generics Challenge: Try/Catch
.image assets/try-catch.png _ 1000
[Original Post](https://discord.com/channels/755435423177638059/918280718013054986/918538237306339348)
[go.dev/play link](https://gotipplay.golang.org/p/FGOD0V1LuqZ)
: Heck, even somebody implemented a try/catch in Go
: because F you, that's why!
: but I also submitted something that I saw could be useful. In fact, I worked on it while the generics where hidden behind an experimental flag.
: This talk is to share about this idea
## Generics Challenge: success
.image assets/gopher-crowdstrike.jpeg 500 _
: thanks to the awful work I've commited this time, I earned a beautiful CrowdStrike gopher plush and a T-shirt
: some ill-advised people might tell you that any participants in this foolish contest got goodies from CrowdStrike.
: but again, security people are not to be trusted!
## Generics Challenge: all the submissions
[Original Post](https://discord.com/channels/755435423177638059/775877786698776576/918957768533245952)
[Element Extraction (@Tanguy)](https://gotipplay.golang.org/p/rtSs9zdtQnI)
[New() (@Tanguy)](https://gotipplay.golang.org/p/3UoOMsQZMlW)
[ProMess, Async, Await (@Tanguy)](https://gotipplay.golang.org/p/dZUeHixD7Ua)
[Print (@Tanguy)](https://gotipplay.golang.org/p/1T5Yn1MFZL6)
[Try, Catch, Finally (@terrabitz )](https://gotipplay.golang.org/p/87QFmuQ-OVA)
[Monads (@danicat )](https://gotipplay.golang.org/p/IQBorcFUTW5)
[Fluent Method Chains (@danicat )](https://gotipplay.golang.org/p/8rqkQPt0CJS)
[Perl (@Andy Walker - (he/him))](https://go.dev/play/p/YidoeekdtIo?v=gotip)
[AJAX (@Ben Woodward | CrowdStrike )](https://gotipplay.golang.org/p/Xryrf6yR9bE)
[HTTP Request/Response (@Tanguy)](https://go.dev/play/p/SV-aeh51526?v=gotip)
## About me
- Tanguy
- from France
- 17 years in IT
- CEO of HTMX
: So who am I?
: I'm Tanguy
: I'm from France
: I've worked in IT for 17 years, started with 8 years in consulting. In multiple domains: avionics, 3D mobile app in airports, logistics, healthcare, finance.
: But that was before…
## About me
.video assets/crowdsurf.mp4 video/mp4 550 _
: as you can see from this video
: I'm a freelancer. I'm ready to do anything for money
: It's a few months old video now, I didn't do something as foolish as that since then
: Maybe I can fix that with this presentation!
## About me
- freelancer specialized in Go since 2015
- mostly classic RESTful API backends
- some blockchain
- containers in CI/CD as code [@dagger_io](https://dagger.io)
- interested in pushing Go in more areas: GUI, video games, AI, embedded, …
: I'm a freelancer specialized in Go since 2015
: I've worked in more domains with Go. Technically mostly in API backends
: I've worked in some blockchain projects as well
: I took a pause in freelancing for a year working at Dagger. I think what they're doing is awesome, go check them out!
: I love Go, and I think it has other uses than just backend, devops and CLI
: There are a lot of other areas that I think would suit Go as well
: Let's go back on the topic. I'll start with a question: What is this?
: can anyone recognized this next piece of code?
## What is this?
.code snippets/classic/handler.go /start classic OMIT/,/end classic OMIT/
: Who, among you, wrote this kind of code?
: For those who don't, it's an HTTP handler, it has JSON decoding
: a validation function if you're that fancy
: and then, we encode the data back to JSON to the client
: So, what's the point of all this code?
## What's the point?
.code snippets/classic/handler.go /start classic OMIT/,/end classic OMIT/ HLinterestingCall
: What is interesting, really, to us?
: is the HTTP part really interesting to us?
: What is interesting, is that single line.
: the backend function. Can be a database, can be another API.
: it can be domain logic as well, otherwise, you're just doing CRUD
: The rest, is mostly uninteresting to us, glue code for HTTP
## Let's add a new handler
.code snippets/classic/handler.go /start classic2 OMIT/,/end classic2 OMIT/ HLclassic2
: now, let's add a new handler, for a new type of resource
: so we changed, here, here, and here
##
.image assets/let-me-think.jpg 500 _
: Did you notice something?
## Let's add a new one
.code snippets/classic/handler.go /start classic2 OMIT/,/end classic2 OMIT/ HLgarbage2
: What is everything in bold?
##
.image assets/garbage.jpg 500 _
: Yep, it is total duplicate boilerplate garbage
## Solution: Abstract the handler
.code snippets/interface/backend.go /start BackendFuncSignature OMIT/,/end BackendFuncSignature OMIT/
.code snippets/interface/handler.go /start Handle OMIT/,/end Handle OMIT/
: We have some solution, we can abstract the handler and make it work with empty interfaces
: but it will make us abstract the backend function with interfaces as well. Which has its own issues
: as a wise person famously said : `interface{}` means nothing
: *whisper* it was Rob Pike, in his Go Proverbs talk, for the newcomers
## Abstract the handler: backend wrapper
.code snippets/interface/backend.go /start BackendWrapper OMIT/,/end BackendWrapper OMIT/ HLBackend
: as we have a very open API with empty interfaces, we need to get back to real types
: and that takes work.
## Abstract the handler: input converter
.code snippets/interface/backend.go /start inputFromMap OMIT/,/end inputFromMap OMIT/ HLBackend
: first we have to make sure we have the value in our map
: then we need to convert in the right field type
: and potentially converting the type in the final one, like this float64
: and after dealing with so many cases where it can go wrong, we finally have a real typed data
: and all of this gymnastic, for a struct with 2 fields.
: imagine with many level of depth in that struct, and you have hundred of lines of code to deal with that
: … for 1 handler, 1 input, 1 output
## Abstract the handler: real backend
.code snippets/interface/backend.go /start realBackendCall OMIT/,/end realBackendCall OMIT/ HLBackend
: Finally, we have real types we can work with.
: obviously, your real backend code might be more complex.
: or, you have found the best niche SaaS in the world if you can make money out of it.
## Conclusion
- lot of runtime/reflect boilerplate to get back to types
- potential reuse of the handler
: So, what's about this?
: you have to create a lot of runtime/reflect type of checks to get back to types in your backend part
: and have a lot of tests to make sure your logic is sound
: but, you can potentially reuse the handler code
: copy pasting full handler code doesn't seem so bad, in the end
: there are other ways, but along those years, I have not been really satisfied with them
: too much work at runtime to get to the types
## If only
## Generics to the rescue
.image assets/generics.jpg 500 _
: Now, we have generics
## Generics: Pros
- better type safety
- better performance than `interface{}`/`any` (except for this use case)
+ Go check this [article from Vicent Marti](https://planetscale.com/blog/generics-can-make-your-go-code-slower) (deprecated?)
- more readable code (`math` package, for example)
- DRY
## Without generics: math.Min
```go
x := 1
y := 2
z := math.Min(x, y)
fmt.Println(z)
```
: Does this code works?
## Without generics: math.Min
```go
x := 1
y := 2
z := math.Min(x, y)
fmt.Println(z)
```
```
math.go:11:16: cannot use x (variable of type int) as float64 value in argument to math.Min
math.go:11:19: cannot use y (variable of type int) as float64 value in argument to math.Min
```
: Nope, it doesn't even compile
## Without generics: math.Min: Solution
.code snippets/generic/math.go /start min/,/end min/ HLUgly
: my reaction to this, is…
##
.image assets/angry-toddler.gif 500 _
: but we have generics now, it must be better, right? nore readable?
## With generics: min
```
func min[T cmp.Ordered](x T, y ...T) T
```
: I see, you're not convinced!
## With generics: min
```
func min[T cmp.Ordered](x T, y ...T) T
```
```
func Min(x, y float64) float64 {
```
generics library code is less readable
: the generics library code is less readable, more cryptic
## With generics: min: user code
```go
x := 1
y := 2
z := min(x, y)
fmt.Println(z)
```
This just works
: but the user code is way more readable
## RIP
.image assets/rip.png 500 _
: now, let's talk about RIP
: RIP is supposed to make most of the HTTP boilerplate disappear
## RIP: User code
```go
func Uppercase(ctx context.Context, name string) (string, error) {
return strings.ToUpper(name), nil
}
```
```go
http.HandleFunc("/uppercase", rip.Handle(http.MethodPost, Uppercase, opts))
```
: Let's reuse some standard library, and wrap it in a rip.InputOutputFunc
: then we can just use it in the standard library ServeMux, and all the HTTP part is handled magically
: now that I can basically wrap nearly every request/response API into HTTP
: how about creating every endpoints needed for some RESTFul resources?
## RIP: Library code
.code snippets/rip/handler.go /start BackendFunc OMIT/,/end BackendFunc OMIT/
.code snippets/rip/handler.go /start Handle OMIT/,/end Handle OMIT/
: as I said, generics library code is more complex by default, harder to read
: but the bonus in user code is pretty great
##
.image assets/inception-deeper.gif 500 _
## REST in Peace
A key concept of REST services is the notion of resource
- accessible via a URI
- action on the resource URI via HTTP methods (POST, PUT, GET, DELETE, …)
- current state sent back via HTTP response
## User code: Entity handler
.code snippets/rip/examples/srv-example/main.go /start HandleFuncEntities OMIT/,/end HandleFuncEntities OMIT/
.code snippets/rip/resource.go /start EntityProvider OMIT/,/end EntityProvider OMIT/
: in DDD, a resource that can be uniquely identified is called and entity
: I decided to use this terminology as it makes sense to me
: (and was clearly a shorter name than IdentifiableResource)
## Lib code
.code snippets/rip/handler.go /start HandleEntities OMIT/,/end HandleEntities OMIT/
## What you get
- creation of CRUD HTTP endpoints
- content negociation for many encodings (json, xml, protobuf, msgpack, HTML, HTML Forms, …)
- automated resource web pages that can edit the resource
- harmonious way of handling common scenarios (unknown resource: return a 404, etc)
## Encoding: JSON
.code snippets/rip/encoding/json/json.go
## Quote
RIP is to HTTP what an ORM is to SQL
- me, probably
: abstract the HTTP part and just stay high level
: I'm afraid of this quote, because I know a lot of people hate ORM with a passion
: so I don't know if I already made you hate this project now
## Demo
.link https://github.com/dolanor/rip
.image ./assets/github-project.qrcode.png _ 500
: So my demo is basically me running a server, then running some curl commands on it
: to show what RIP can handle for you
: If I have time, I'll show some code of implementation on a Go minecraft clone project and demo it too
## Demo
.video ./assets/demo.mp4 video/mp4 500 _
: So I run the server.
: the log in yellow is the log from the http logger middleware and the log from your implementation
: the rest is just the output of the command
: we can even get that resource in XML. This API allows for time travelling.
## User Code
## Route Option
.code snippets/rip/examples/srv-example/main.go /start route option OMIT/,/end route option OMIT/
## User Code: Entity
.code snippets/rip/examples/srv-example/memuser/user.go /start User Entity OMIT/,/end User Entity OMIT/
: You need to implement some interface about handling IDs
: if somebody have a better idea about that, come and talk to me about it
## User Code: Entity Provider
.code snippets/rip/examples/srv-example/memuser/memuser.go /start User Provider OMIT/,/end User Provider OMIT/
.code snippets/rip/examples/srv-example/memuser/memuser.go /start User Provider Update OMIT/,/end User Provider Update OMIT/
: You just add implementations for Create, Get, Delete and List
## Future
- <s>per-route options (encoding, middleware)</s>
- <s>access/update fields independantly (GET/POST/PUT/DELETE `/users/1/name`)</s>
- nested resources (eg, `/users/1/posts/1` also points to `/posts/1` )
- pagination
- OpenAPI autogeneration
- more HATEOAS (Hypermedia As The Engine Of Application State)
+ links
+ API auto documentation/explorability
+ support for JSON-LD
- improve the API
## Future
- <s>protobuf encoding</s>
- use of `log/slog` logger interface
- better error handling
- <s>better error type</s>
- nice standard way to return errors to user
- customization of HTML template
- authorization of HTML pages
- generation of GUI apps based on your API
## Call to action
- feedback
- discussion
- contribution
.link https://github.com/dolanor/rip
.image ./assets/github-project.qrcode.png 400 _
: I did this for myself at first, but I think it can help other people as well
: I'd like to get some feedback from you all
: especially if you think it won't work in the longer term
: and you had some experience with some existing framework like this (spring boot, django rest API)
: you could avoid me some lost hours
## Thanks
- The Go Team
- Go SXB Go Meetup (Strasbourg)
- Thierry Pfeiffer for the logo
- You for watching that talk
- FOSDEM and the Go devroom organizers
- HTMX
.image ./assets/htmx-logo-pickle.jpeg 300 _
: The Go Team for delivering the generics
: Go SXB Go Meetup (Strasbourg), for reviewing this talk
: Thierry Pfeiffer for the logo
: You for watching that talk
: FOSDEM and the Go devroom organizers
: HTMX, for the library it provides, and all the memes that make me laugh on Twitter
: Talking about Twitter, I'm gonna leave the platform, so if you wanna follow me
: I'll be exclusively on mastodon starting next week
: I encourage you to do the same, as the platform has becoming more and more hostile for many people
: and I joined the Go community for its values in inclusivity and diversity.
: I hope we can continue in this direction