11# Microgen
22
3- Tool to generate microservices, based on [ go-kit] ( https://gokit.io/ ) , by specified service interface.
3+ Tool to generate microservices, based on [ go-kit] ( https://gokit.io/ ) , by specified service interface.
4+ The goal is to generate code for service which not fun to write but it should be written.
45
56## Install
67```
@@ -11,42 +12,108 @@ go get -u github.com/devimteam/microgen/cmd/microgen
1112``` sh
1213microgen [OPTIONS]
1314```
14- microgen is stable, so you can generate without flag ` -init ` any time your interface changed (e.g. added new method)
15+ microgen tool search in file first ` type * interface ` with docs, that contains ` // @microgen ` .
16+
17+ generation parameters provides through [ "tags"] ( #tags ) in interface docs after general ` // @microgen ` tag (space before @ __ required__ ).
18+
1519### Options
1620
17- | Name | Default | Description |
18- | :------------| :-----------------| :------------------------------------------------------------------------------|
19- | -file | service.go | Relative path to source file with service interface |
20- | -interface* | | Service interface name in source file |
21- | -out | writes to stdout | Relative or absolute path to directory, where you want to see generated files |
22- | -package* | | Package path of your service interface source file |
23- | -debug | false | Display some debug information |
24- | -grpc | false | Render client, server and converters for gRPC protocol |
25- | -init | false | With flag ` -grpc ` generate stub methods for converters |
21+ | Name | Default | Description |
22+ | :------ | :-----------| :------------------------------------------------------------------------------|
23+ | -file | service.go | Relative path to source file with service interface |
24+ | -out | . | Relative or absolute path to directory, where you want to see generated files |
25+ | -force | false | With flag generate stub methods. |
26+ | -help | false | Print usage information |
2627
2728\* __ Required option__
2829
30+ ### Markers
31+ Markers is a general tags, that participate in generation process.
32+ Typical syntax is: ` // @<tag-name>: `
33+
34+ #### @microgen
35+ Main tag for microgen tool. Microgen scan file for the first interface which docs contains this tag.
36+ To add templates for generation, add their tags, separated by comma after ` @microgen: `
37+ Example:
38+ ``` go
39+ // @microgen middleware, logging
40+ type StringService interface {
41+ ServiceMethod ()
42+ }
43+ ```
44+ #### @protobuf
45+ Protobuf tag is used for package declaration of compiled with ` protoc ` grpc package.
46+ Example:
47+ ``` go
48+ // @microgen grpc-server
49+ // @protobuf github.com/user/repo/path/to/protobuf
50+ type StringService interface {
51+ ServiceMethod ()
52+ }
53+ ```
54+ ` @protobuf ` tag is optional, but required for ` grpc ` , ` grpc-server ` , ` grpc-client ` generation.
55+ #### @grpc-addr
56+ gRPC address tag is used for gRPC go-kit-based client generation.
57+ Example:
58+ ``` go
59+ // @microgen grpc
60+ // @protobuf github.com/user/repo/path/to/protobuf
61+ // @grpc-addr some.service.address
62+ type StringService interface {
63+ ServiceMethod ()
64+ }
65+ ```
66+ ` @grpc-addr ` tag is optional, but required for ` grpc-client ` generation.
67+
68+ ### Method's tags
69+ #### @logs-ignore
70+ This tag is used for logging middleware, when some arguments or results should not be logged, e.g. passwords or files.
71+ If ` context.Context ` is first, it ignored by default.
72+ Provide parameters names, separated by comma, to exclude them from logs.
73+ Example:
74+ ``` go
75+ // @microgen logging
76+ type FileService interface {
77+ // @logs-ignore data
78+ UploadFile (ctx context.Context , name string , data []byte ) (link string , err error )
79+ }
80+ ```
81+
82+ ### Tags
83+ All allowed tags for customize generation provided here.
84+
85+ | Tag | Description |
86+ | :------------| :---------------------------------------------------------------------------------------|
87+ | middleware | General application middleware interface |
88+ | logging | Middleware that writes to logger all request/response information with handled time |
89+ | grpc-client | Generates client for grpc transport with request/response encoders/decoders |
90+ | grpc-server | Generates server for grpc transport with request/response encoders/decoders |
91+ | grpc | Generates client and server for grpc transport with request/response encoders/decoders |
92+
2993## Example
3094Follow this short guide to try microgen tool.
3195
32961 . Create file ` service.go ` inside GOPATH and add code below.
33- ``` golang
97+ ``` go
3498package stringsvc
3599
36100import (
37- " context"
101+ " context"
38102
39- drive " google.golang.org/api/drive/v3"
103+ drive " google.golang.org/api/drive/v3"
40104)
41105
106+ // @microgen grpc, middleware, logging
107+ // @protobuf github.com/devimteam/proto-utils
108+ // @grpc-addr test.address
42109type StringService interface {
43- Uppercase (ctx context.Context , str string ) (ans string , err error )
44- Count (ctx context.Context , text string , symbol string ) (count int , positions []int )
45- TestCase (ctx context.Context , comments []*drive.Comment ) (err error )
110+ Uppercase (ctx context.Context , str string ) (ans string , err error )
111+ Count (ctx context.Context , text string , symbol string ) (count int , positions []int )
112+ TestCase (ctx context.Context , comments []*drive.Comment ) (err error )
46113}
47114```
481152 . Open command line next to your ` service.go ` .
49- 3 . Enter ` microgen -file ./service.go -interface StringService -out . -grpc -init ` . __ * __
116+ 3 . Enter ` microgen ` . __ * __
501174 . You should see something like that:
51118```
52119exchanges.go
@@ -65,23 +132,14 @@ All files successfully generated
65132
66133__ * __ ` GOPATH/bin ` should be in your PATH.
67134
68- ### Interface declaration rules
135+ ## Interface declaration rules
69136For correct generation, please, follow rules below.
70137
71138* All interface method's arguments and results should be named and should be different (name duplicating unacceptable).
72139* First argument of each method should be of type ` context.Context ` (from [ standard library] ( https://golang.org/pkg/context/ ) ).
73- * Method results should contain at least one variable of ` error ` type.
74- * [ Some names] ( #not-allowed-names ) are not allowed to be an argument or result.
140+ * Last result should should be ` error ` type.
141+ ---
142+ * Name of _ protobuf_ service should be the same, as interface name.
143+ * Function names in _ protobuf_ should be the same, as in interface.
144+ * Message names in _ protobuf_ should be named ` <FunctionName>Request ` or ` <FunctionName>Response ` for request/response message respectively.
75145* Field names in _ protobuf_ messages should be the same, as in interface methods (_ protobuf_ - snake_case, interface - camelCase).
76-
77- #### Not allowed names:
78- ```
79- req
80- request
81- resp
82- response
83- ```
84-
85- ### Misc
86-
87- Microgen uses __ 0.9.* __ version of [ devimteam/go-kit] ( https://github.com/devimteam/go-kit )
0 commit comments