1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
6
+ "strings"
5
7
6
8
"github.com/gin-gonic/gin"
7
9
@@ -12,9 +14,11 @@ import (
12
14
)
13
15
14
16
type HelloRequest struct {
15
- Word string `json:"word"`
17
+ Word string `json:"word" validate:"required,min=2" `
16
18
}
17
19
20
+ var _ fuego.InTransformer = & HelloRequest {}
21
+
18
22
type HelloResponse struct {
19
23
Message string `json:"message"`
20
24
}
@@ -38,13 +42,22 @@ func server() (*gin.Engine, *fuego.OpenAPI) {
38
42
ginRouter .GET ("/gin" , ginController )
39
43
40
44
// Incrementally add OpenAPI spec
41
- // Level 1: Register Gin controller to Gin router, plugs Fuego OpenAPI route declaration
45
+ // 1️⃣ Level 1: Register Gin controller to Gin router, plugs Fuego OpenAPI route declaration
42
46
fuegogin .GetGin (engine , ginRouter , "/gin-with-openapi" , ginController )
43
47
44
- // Level 2: Register Fuego controller to Gin router. Fuego take care of serialization/deserialization, error handling, content-negotiation, etc.
48
+ // 2️⃣ Level 2: Register Gin controller to Gin router, manually add options (not checked inside the Gin controller)
49
+ fuegogin .GetGin (engine , ginRouter , "/gin-with-openapi-and-options" , ginController ,
50
+ // OpenAPI options
51
+ option .Summary ("Gin controller with options" ),
52
+ option .Description ("Some description" ),
53
+ option .OperationID ("MyCustomOperationID" ),
54
+ option .Tags ("Gin" ),
55
+ )
56
+
57
+ // 3️⃣ Level 3: Register Fuego controller to Gin router. Fuego take care of serialization/deserialization, error handling, content-negotiation, etc.
45
58
fuegogin .Get (engine , ginRouter , "/fuego" , fuegoControllerGet )
46
59
47
- // Add some options to the POST endpoint
60
+ // 4️⃣ Level 4: Add some options to the POST endpoint (checks at start-time + validations at request time)
48
61
fuegogin .Post (engine , ginRouter , "/fuego-with-options" , fuegoControllerPost ,
49
62
// OpenAPI options
50
63
option .Description ("Some description" ),
@@ -63,6 +76,7 @@ func server() (*gin.Engine, *fuego.OpenAPI) {
63
76
group := ginRouter .Group ("/my-group/:id" )
64
77
fuegogin .Get (engine , group , "/fuego" , fuegoControllerGet ,
65
78
option .Summary ("Route with group and id" ),
79
+ option .Tags ("Fuego" ),
66
80
)
67
81
68
82
// Serve the OpenAPI spec
@@ -71,3 +85,20 @@ func server() (*gin.Engine, *fuego.OpenAPI) {
71
85
72
86
return ginRouter , engine .OpenAPI
73
87
}
88
+
89
+ func (h * HelloRequest ) InTransform (ctx context.Context ) error {
90
+ // Transformation
91
+ h .Word = strings .ToLower (h .Word )
92
+
93
+ // Custom validation
94
+ if h .Word == "apple" {
95
+ return fuego.BadRequestError {Title : "Please don't use the word 'apple'" }
96
+ }
97
+
98
+ // Context-based transformation
99
+ if user := ctx .Value ("user" ); user == "secret agent" {
100
+ h .Word = "*****"
101
+ }
102
+
103
+ return nil
104
+ }
0 commit comments