11# Types Plugin
22
33The ` types ` plugin is a [ Goa] ( https://github.com/goadesign/goa/tree/v3 ) plugin
4- that generates Go data types and validation functions for all user types defined
5- in a Goa design package.
4+ that generates Go data types, validation functions, and Protocol Buffer message
5+ definitions for all user types defined in a Goa design package.
66
77Given the following design:
88
@@ -17,7 +17,9 @@ var _ = Type("MyType", func() {
1717})
1818```
1919
20- The plugin generates the file ` gen/types/types.go ` with the following code:
20+ The plugin generates two files:
21+
22+ ## Generated Go Code (` gen/types/types.go ` )
2123
2224``` go
2325package types
@@ -43,6 +45,24 @@ func ValidateMyType(v *MyType) (err error) {
4345}
4446```
4547
48+ ## Generated Protocol Buffer Definitions (` gen/types/types.proto ` )
49+
50+ ``` proto
51+ syntax = "proto3";
52+
53+ package types;
54+
55+ option go_package = "<your-module>/types";
56+
57+ // My type
58+ message MyType {
59+ // Age
60+ int32 age = 1;
61+ // Name
62+ string name = 2;
63+ }
64+ ```
65+
4666## Usage Pattern
4767
4868This plugin makes it possible to share data types between Goa microservices and
@@ -53,6 +73,12 @@ generated by the `types` plugin to decode and validate messages. The adapter can
5373then make requests to a Goa microservice whose design package imports the
5474original design package defining the data types thus guaranteeing compatibility.
5575
76+ The protobuf definitions enable:
77+ - Cross-language compatibility with protobuf-based systems
78+ - Efficient binary serialization
79+ - gRPC service integration
80+ - Schema evolution with backwards compatibility
81+
5682## Enabling the Plugin
5783
5884To enable the plugin simply import both the ` types ` package as follows:
@@ -71,5 +97,107 @@ necessary as the package is imported solely for its side-effects
7197## Effects on Code Generation
7298
7399Enabling the plugin changes the behavior of the ` gen ` command of the ` goa ` tool.
74- The command generates an additional ` types/types.go ` file under the ` gen ` folder
75- containing the type definitions and validations.
100+ The command generates two additional files under the ` gen/types ` folder:
101+ - ` types.go ` - containing the Go type definitions and validations
102+ - ` types.proto ` - containing the Protocol Buffer message definitions
103+
104+ ## Type Mappings
105+
106+ The plugin maps Goa types to both Go and Protocol Buffer types:
107+
108+ | Goa Type | Go Type | Proto Type |
109+ | ------------| ------------| --------------------------|
110+ | Boolean | bool | bool |
111+ | Int | int | int32 |
112+ | Int32 | int32 | int32 |
113+ | Int64 | int64 | int64 |
114+ | UInt | uint | uint32 |
115+ | UInt32 | uint32 | uint32 |
116+ | UInt64 | uint64 | uint64 |
117+ | Float32 | float32 | float |
118+ | Float64 | float64 | double |
119+ | String | string | string |
120+ | Bytes | [ ] byte | bytes |
121+ | Any | interface{}| google.protobuf.Any |
122+ | ArrayOf(T) | [ ] T | repeated T |
123+ | MapOf(K,V) | map[ K] V | map<K, V> |
124+
125+ ## Features
126+
127+ ### Arrays and Nested Types
128+
129+ The plugin handles complex nested structures:
130+
131+ ``` go
132+ var _ = Type (" Order" , func () {
133+ Attribute (" items" , ArrayOf (" OrderItem" ))
134+ })
135+
136+ var _ = Type (" OrderItem" , func () {
137+ Attribute (" name" , String )
138+ Attribute (" quantity" , Int )
139+ })
140+ ```
141+
142+ Generates:
143+
144+ ``` proto
145+ message OrderItem {
146+ string name = 1;
147+ int32 quantity = 2;
148+ }
149+
150+ message Order {
151+ repeated OrderItem items = 1;
152+ }
153+ ```
154+
155+ ### Maps
156+
157+ The plugin supports map types:
158+
159+ ``` go
160+ var _ = Type (" Config" , func () {
161+ Attribute (" settings" , MapOf (String, String ))
162+ })
163+ ```
164+
165+ Generates:
166+
167+ ``` proto
168+ message Config {
169+ map<string, string> settings = 1;
170+ }
171+ ```
172+
173+ ### Any Type Support
174+
175+ The plugin supports the ` Any ` type for dynamic content:
176+
177+ ``` go
178+ var _ = Type (" Envelope" , func () {
179+ Attribute (" metadata" , MapOf (String, Any ))
180+ })
181+ ```
182+
183+ Generates:
184+
185+ ``` proto
186+ import "google/protobuf/any.proto";
187+
188+ message Envelope {
189+ map<string, google.protobuf.Any> metadata = 1;
190+ }
191+ ```
192+
193+ ## Compiling Protocol Buffers
194+
195+ To compile the generated ` .proto ` files to Go code, use the Protocol Buffer compiler:
196+
197+ ``` bash
198+ protoc --go_out=. --go_opt=paths=source_relative \
199+ gen/types/types.proto
200+ ```
201+
202+ This generates ` gen/types/types.pb.go ` which can be used alongside the Goa-generated
203+ ` gen/types/types.go ` for maximum interoperability.
0 commit comments