12
12
13
13
[ English] ( README.md ) | [ 中文] ( README_CN.md )
14
14
15
- ## Project Overview
15
+ > This project is under continuous development, and its features and capabilities are being actively enhanced.
16
16
17
- ** gs-http-gen** is an HTTP code generation tool based on ** IDL (Interface Definition Language)** , designed for the *
18
- * Go-Spring ** framework. It can automatically generate HTTP server and client code from interface definitions, including
19
- data models, validation logic, and route bindings.
17
+ ` gs-http-gen ` is an ** IDL (Interface Definition Language)-based HTTP code generation tool ** .
18
+ It can generate ** Go server-side code ** and ** client-side code in other languages ** based on unified interface
19
+ definitions. The server-side code includes:
20
20
21
- The tool aims to simplify the development workflow of Go web services. By using declarative IDL definitions, it
22
- generates boilerplate code automatically, improving development efficiency and reducing human errors.
21
+ * Data models
22
+ * Validation logic
23
+ * HTTP route binding
24
+ * Support for both regular and streaming (SSE) interfaces
23
25
24
- More importantly, IDL is not only used for code generation—it also serves as a unified contract and documentation for
25
- APIs across frontend-backend teams and departments. With standardized IDL definitions, key details such as request
26
- parameters, response formats, and validation rules become clear, reducing communication costs and ensuring API
27
- consistency and correctness.
26
+ By using a declarative IDL description, developers can focus on business logic while reducing boilerplate code
27
+ and human errors.
28
+
29
+ Additionally, IDL serves as a ** contract and documentation for cross-team and frontend-backend collaboration** ,
30
+ helping teams reduce communication overhead and ensure interface consistency.
28
31
29
32
## Features
30
33
31
- - ** IDL-driven** : Define service interfaces and data models using a simple interface definition language.
32
- - ** Automatic code generation** : Generate Go code from IDL files, including:
33
- - Data model structs
34
- - Data validation logic
35
- - HTTP route bindings
36
- - Server interface definitions
37
- - Client call code
38
- - ** Rich type support** : Supports basic types, structs, enums, lists, optional types, etc.
39
- - ** Data validation** : Built-in validation rules with support for custom validators.
40
- - ** HTTP parameter binding** : Automatically bind HTTP request parameters (path, query, header, body) to data models.
41
- - ** Type embedding** : Supports type inheritance and field reuse to reduce redundancy.
42
- - ** Flexible configuration** : Generate server code, client code, or both.
43
- - ** Enum support** : Enum types with optional string serialization.
44
- - ** Streaming support** : Generate streaming RPC interfaces.
45
- - ** Annotation support** : Add Markdown-style comments in IDL (not yet implemented).
34
+ ### 🌟 IDL-Driven
35
+
36
+ * Define services and data models using a concise interface definition language.
37
+ * Supports:
38
+
39
+ * Constants, enums, structs, and ` oneof ` types
40
+ * Generics and type embedding (field reuse)
41
+ * RPC interface definitions
42
+ * Custom annotations (e.g., ` json ` , ` go.type ` , ` enum_as_string ` , etc.)
43
+
44
+ ### ⚙️ Automatic Code Generation
45
+
46
+ Generate Go server-side code and client code in other languages from IDL files:
47
+
48
+ * Data model structures
49
+ * Parameter and data validation logic
50
+ * Automatic HTTP request parameter binding (path, query, header, body)
51
+ * Support for both regular and streaming (SSE) interfaces
52
+ * Server interface definitions and route binding
53
+ * Client-side call code
54
+
55
+ ### 📦 Rich Data Type Support
56
+
57
+ * Basic types: ` bool ` , ` int ` , ` float ` , ` string `
58
+ * Advanced types: ` list ` , ` map ` , ` oneof `
59
+ * Nullable fields: supported via ` ? ` suffix
60
+ * Type redefinitions and generics
61
+
62
+ ### 🔎 Efficient Data Validation
63
+
64
+ * High performance, reflection-free implementation
65
+ * Expression-based validation rules
66
+ * Auto-generated ` OneOfXXX ` validation functions for enums
67
+ * Custom validation functions supported
68
+
69
+ ### 🌐 HTTP-Friendly
70
+
71
+ * Automatic binding of HTTP request parameters (path, query, header, body)
72
+ * Supports ` form ` , ` json ` , and ` multipart-form ` formats
73
+ * Native support for streaming RPC (SSE) interfaces
74
+
75
+ ### 📝 Comments & Documentation
76
+
77
+ * Supports single-line and multi-line comments
78
+ * Planned support for Markdown comments for richer documentation generation
46
79
47
80
## Installation
48
81
49
- ** Recommended** : Install via the ** gs** integrated development tool,
50
- see [ https://github.com/go-spring/gs ] ( https://github.com/go-spring/gs ) .
82
+ * ** Recommended:**
51
83
52
- To install this tool separately:
84
+ Use the [ gs] ( https://github.com/go-spring/gs ) integrated development tool.
85
+
86
+ * ** Standalone installation:**
53
87
54
88
``` bash
55
89
go install github.com/go-spring/gs-http-gen@latest
56
90
```
57
91
58
92
## Usage
59
93
60
- ### Step 1: Define an IDL file
94
+ ### Step 1: Define IDL Files
95
+
96
+ Create ` .idl ` files to describe your services and data models.
61
97
62
- First, create an IDL file to define service interfaces and data models:
98
+ > ** Syntax Notes:**
99
+ >
100
+ > * A document consists of zero or more definitions, separated by newlines or semicolons, and ends with EOF.
101
+ > * Identifiers consist of letters, digits, and underscores, but cannot start with a digit.
102
+ > * Use ` ? ` to denote nullable fields.
103
+
104
+ ** Example:**
63
105
64
106
``` idl
65
- // Define constants
66
- const int MAX_AGE = 150
67
- const int MIN_AGE = 18
107
+ // Constants
108
+ const int MAX_AGE = 150 // years
109
+ const int MIN_AGE = 18 // years
68
110
69
- // Define enums
111
+ // Enums
70
112
enum ErrCode {
71
113
ERR_OK = 0
72
114
PARAM_ERROR = 1003
@@ -78,103 +120,125 @@ enum Department {
78
120
SALES = 3
79
121
}
80
122
81
- // Define data structures
123
+ // Data structures
82
124
type Manager {
83
125
string id
84
126
string name (validate="len($) > 0 && len($) <= 64")
85
127
int? age (validate="$ >= MIN_AGE && $ <= MAX_AGE")
86
- Department dept
128
+ Department dept (enum_as_string)
87
129
}
88
130
89
131
type Response<T> {
90
- ErrCode errno = ErrCode.ERR_OK (validate="OneOfErrCode($)")
132
+ ErrCode errno (validate="OneOfErrCode($)")
91
133
string errmsg
92
134
T data
93
135
}
94
136
95
- // Define request and response
137
+ // Request & response types
96
138
type ManagerReq {
97
139
string id (path="id")
98
140
}
99
141
100
142
type GetManagerResp Response<Manager?>
101
143
102
- // Define streaming types
144
+ // Regular RPC interface
145
+ rpc GetManager(ManagerReq) GetManagerResp {
146
+ method="GET"
147
+ path="/managers/{id}"
148
+ summary="Get manager info by ID"
149
+ }
150
+
151
+ // Streaming RPC example
103
152
type StreamReq {
104
- string id
153
+ string ID (json="id")
105
154
}
106
155
107
156
type StreamResp {
108
157
string id
109
158
string data
159
+ Payload payload
110
160
}
111
161
112
- // Define RPC interface
113
- rpc GetManager(ManagerReq) GetManagerResp {
114
- method="GET"
115
- path="/managers/{id}"
116
- summary="Get manager info by ID"
162
+ oneof Payload {
163
+ string text_data
164
+ int? numberData (json="number_data")
165
+ bool boolean_data (json="")
117
166
}
118
167
119
- // Example of streaming interface
168
+ // Streaming RPC
120
169
rpc Stream(StreamReq) stream<StreamResp> {
121
170
method="GET"
122
171
path="/stream/{id}"
123
- summary="Stream data transfer "
172
+ summary="Stream data by ID "
124
173
}
125
174
```
126
175
127
- ### Step 2: Generate code
176
+ ### Step 2: Generate Code
128
177
129
- Use the CLI tool to generate code:
178
+ Run the CLI tool to generate code:
130
179
131
180
``` bash
132
- # Generate server code only (default)
133
- gs-http-gen --server --output ./generated --package myservice
181
+ # Generate server-side code only (default)
182
+ gs-http-gen --server --output ./generated --go_package myservice
134
183
135
- # Generate both server and client code
136
- gs-http-gen --server --client --output ./generated --package myservice
184
+ # Generate both server-side and client-side code
185
+ gs-http-gen --server --client --output ./generated --go_package myservice
137
186
```
138
187
139
- Command-line options:
188
+ ** Command-line options:**
140
189
141
- * ` --server ` : Generate server code (HTTP handlers, route bindings, etc.)
142
- * ` --client ` : Generate client code (HTTP call wrappers)
143
- * ` --output ` : Output directory for generated code (default: current directory)
144
- * ` --package ` : Go package name (default: "proto")
145
- * ` --language ` : Target language (currently only ` "go" ` supported)
190
+ | Option | Description | Default |
191
+ | ----------------| -----------------------------------------------------| ---------|
192
+ | ` --server ` | Generate server-side code (HTTP handlers & routing) | false |
193
+ | ` --client ` | Generate client-side code (HTTP call wrappers) | false |
194
+ | ` --output ` | Output directory | ` . ` |
195
+ | ` --go_package ` | Go package name for generated code | ` proto ` |
196
+ | ` --language ` | Target language (currently only ` go ` ) | ` go ` |
146
197
147
- ### Step 3: Use the generated code
198
+ ### Step 3: Use the Generated Code
148
199
149
- The generated code includes data models, validation logic, and HTTP handlers:
200
+ ** Example: **
150
201
151
202
``` go
152
- // Implement service interface
203
+ // Implement the service interface
153
204
type MyManagerServer struct {}
154
205
155
206
func (m *MyManagerServer ) GetManager (ctx context .Context , req *proto .ManagerReq ) *proto .GetManagerResp {
156
- // Business logic
157
- data := proto.NewManager ()
158
- data.SetName (" Jim" )
159
- res := proto.NewGetManagerResp ()
160
- res.SetData (data)
161
- return res
207
+ // Regular response
208
+ return &proto.GetManagerResp {
209
+ Data: &proto.Manager {
210
+ Id: " 1" ,
211
+ Name: " Jim" ,
212
+ Dept: proto.Department_ENGINEERING ,
213
+ },
214
+ }
162
215
}
163
216
164
217
func (m *MyManagerServer ) Stream (ctx context .Context , req *proto .StreamReq , resp chan <- *proto .StreamResp ) {
165
- // Streaming logic
218
+ // Streaming response
166
219
for i := 0 ; i < 5 ; i++ {
167
220
resp <- &proto.StreamResp {
168
221
Id: strconv.Itoa (i),
222
+ Payload: proto.Payload {
223
+ TextData: " data" ,
224
+ },
169
225
}
170
226
}
171
227
}
172
228
173
229
// Register routes
174
230
mux := http.NewServeMux ()
175
231
proto.InitRouter (mux, &MyManagerServer{})
232
+
233
+ http.ListenAndServe (" :8080" , mux)
176
234
```
177
235
236
+ ## ⚠️ Notes
237
+
238
+ * Generated code does ** not** enforce required fields; you must handle this in your business logic.
239
+ * Validation logic does not automatically invoke ` Validate() ` ; invoke it explicitly as needed for deep validation.
240
+ * It’s recommended to manage generated code centrally and keep it in sync with IDL files to avoid divergence.
241
+
178
242
## License
179
243
180
- This project is licensed under the ** Apache License 2.0** . See the [ LICENSE ] ( LICENSE ) file for details .
244
+ This project is licensed under the [ Apache License 2.0] ( LICENSE ) .
0 commit comments