1212
1313[ English] ( README.md ) | [ 中文] ( README_CN.md )
1414
15- ## Project Overview
15+ > This project is under continuous development, and its features and capabilities are being actively enhanced.
1616
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:
2020
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
2325
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.
2831
2932## Features
3033
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
4679
4780## Installation
4881
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:**
5183
52- To install this tool separately:
84+ Use the [ gs] ( https://github.com/go-spring/gs ) integrated development tool.
85+
86+ * ** Standalone installation:**
5387
5488``` bash
5589go install github.com/go-spring/gs-http-gen@latest
5690```
5791
5892## Usage
5993
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.
6197
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:**
63105
64106``` 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
68110
69- // Define enums
111+ // Enums
70112enum ErrCode {
71113 ERR_OK = 0
72114 PARAM_ERROR = 1003
@@ -78,103 +120,125 @@ enum Department {
78120 SALES = 3
79121}
80122
81- // Define data structures
123+ // Data structures
82124type Manager {
83125 string id
84126 string name (validate="len($) > 0 && len($) <= 64")
85127 int? age (validate="$ >= MIN_AGE && $ <= MAX_AGE")
86- Department dept
128+ Department dept (enum_as_string)
87129}
88130
89131type Response<T> {
90- ErrCode errno = ErrCode.ERR_OK (validate="OneOfErrCode($)")
132+ ErrCode errno (validate="OneOfErrCode($)")
91133 string errmsg
92134 T data
93135}
94136
95- // Define request and response
137+ // Request & response types
96138type ManagerReq {
97139 string id (path="id")
98140}
99141
100142type GetManagerResp Response<Manager?>
101143
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
103152type StreamReq {
104- string id
153+ string ID (json="id")
105154}
106155
107156type StreamResp {
108157 string id
109158 string data
159+ Payload payload
110160}
111161
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="")
117166}
118167
119- // Example of streaming interface
168+ // Streaming RPC
120169rpc Stream(StreamReq) stream<StreamResp> {
121170 method="GET"
122171 path="/stream/{id}"
123- summary="Stream data transfer "
172+ summary="Stream data by ID "
124173}
125174```
126175
127- ### Step 2: Generate code
176+ ### Step 2: Generate Code
128177
129- Use the CLI tool to generate code:
178+ Run the CLI tool to generate code:
130179
131180``` 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
134183
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
137186```
138187
139- Command-line options:
188+ ** Command-line options:**
140189
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 ` |
146197
147- ### Step 3: Use the generated code
198+ ### Step 3: Use the Generated Code
148199
149- The generated code includes data models, validation logic, and HTTP handlers:
200+ ** Example: **
150201
151202``` go
152- // Implement service interface
203+ // Implement the service interface
153204type MyManagerServer struct {}
154205
155206func (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+ }
162215}
163216
164217func (m *MyManagerServer ) Stream (ctx context .Context , req *proto .StreamReq , resp chan <- *proto .StreamResp ) {
165- // Streaming logic
218+ // Streaming response
166219 for i := 0 ; i < 5 ; i++ {
167220 resp <- &proto.StreamResp {
168221 Id: strconv.Itoa (i),
222+ Payload: proto.Payload {
223+ TextData: " data" ,
224+ },
169225 }
170226 }
171227}
172228
173229// Register routes
174230mux := http.NewServeMux ()
175231proto.InitRouter (mux, &MyManagerServer{})
232+
233+ http.ListenAndServe (" :8080" , mux)
176234```
177235
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+
178242## License
179243
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