Skip to content

Commit c5c574c

Browse files
authored
Merge pull request #51 from devimteam/develop
0.7.0
2 parents d8f021e + 297d4b8 commit c5c574c

24 files changed

Lines changed: 463 additions & 96 deletions

example/generated/endpoints.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (E *Endpoints) Uppercase(ctx context.Context, stringsMap map[string]string)
2929
return endpointUppercaseResponse.(*UppercaseResponse).Ans, err
3030
}
3131

32-
func (E *Endpoints) Count(ctx context.Context, text string, symbol string) (count int, positions []int, err error) {
32+
func (E *Endpoints) Count(ctx context.Context, text string, symbol int) (count int, positions []int, err error) {
3333
endpointCountRequest := CountRequest{
3434
Symbol: symbol,
3535
Text: text,

example/generated/exchanges.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type UppercaseResponse struct {
1414

1515
type CountRequest struct {
1616
Text string `json:"text"`
17-
Symbol string `json:"symbol"`
17+
Symbol int `json:"symbol"`
1818
}
1919

2020
type CountResponse struct {

example/generated/middleware/error_logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (L *serviceErrorLogging) Uppercase(ctx context.Context, stringsMap map[stri
3333
return L.next.Uppercase(ctx, stringsMap)
3434
}
3535

36-
func (L *serviceErrorLogging) Count(ctx context.Context, text string, symbol string) (count int, positions []int, err error) {
36+
func (L *serviceErrorLogging) Count(ctx context.Context, text string, symbol int) (count int, positions []int, err error) {
3737
defer func() {
3838
if err != nil {
3939
L.logger.Log("method", "Count", "message", err)

example/generated/middleware/logging.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (L *serviceLogging) Uppercase(ctx context.Context, stringsMap map[string]st
3535
return L.next.Uppercase(ctx, stringsMap)
3636
}
3737

38-
func (L *serviceLogging) Count(ctx context.Context, text string, symbol string) (count int, positions []int, err error) {
38+
func (L *serviceLogging) Count(ctx context.Context, text string, symbol int) (count int, positions []int, err error) {
3939
defer func(begin time.Time) {
4040
L.logger.Log(
4141
"method", "Count",
@@ -74,7 +74,7 @@ type logUppercaseRequest struct {
7474

7575
type logCountRequest struct {
7676
Text string
77-
Symbol string
77+
Symbol int
7878
}
7979

8080
type logCountResponse struct {

example/generated/middleware/recovering.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (R *serviceRecovering) Uppercase(ctx context.Context, stringsMap map[string
3535
return R.next.Uppercase(ctx, stringsMap)
3636
}
3737

38-
func (R *serviceRecovering) Count(ctx context.Context, text string, symbol string) (count int, positions []int, err error) {
38+
func (R *serviceRecovering) Count(ctx context.Context, text string, symbol int) (count int, positions []int, err error) {
3939
defer func() {
4040
if r := recover(); r != nil {
4141
R.logger.Log("method", "Count", "message", r)

example/generated/svc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
type StringService interface {
1313
// @logs-ignore ans, err
1414
Uppercase(ctx context.Context, stringsMap map[string]string) (ans string, err error)
15-
Count(ctx context.Context, text string, symbol string) (count int, positions []int, err error)
15+
// @http-method geT
16+
Count(ctx context.Context, text string, symbol int) (count int, positions []int, err error)
1617
// @logs-len comments
1718
TestCase(ctx context.Context, comments []*entity.Comment) (tree map[string]int, err error)
1819
}

example/generated/transport/converter/http/exchange_converters.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import (
66
bytes "bytes"
77
context "context"
88
json "encoding/json"
9+
errors "errors"
910
generated "github.com/devimteam/microgen/example/generated"
11+
mux "github.com/gorilla/mux"
1012
ioutil "io/ioutil"
1113
http "net/http"
14+
path "path"
15+
strconv "strconv"
1216
)
1317

1418
func CommonHTTPRequestEncoder(_ context.Context, r *http.Request, request interface{}) error {
@@ -32,9 +36,28 @@ func DecodeHTTPUppercaseRequest(_ context.Context, r *http.Request) (interface{}
3236
}
3337

3438
func DecodeHTTPCountRequest(_ context.Context, r *http.Request) (interface{}, error) {
35-
var req generated.CountRequest
36-
err := json.NewDecoder(r.Body).Decode(&req)
37-
return &req, err
39+
var (
40+
_param string
41+
)
42+
var ok bool
43+
_vars := mux.Vars(r)
44+
_param, ok = _vars["text"]
45+
if !ok {
46+
return nil, errors.New("param text not found")
47+
}
48+
text := _param
49+
_param, ok = _vars["symbol"]
50+
if !ok {
51+
return nil, errors.New("param symbol not found")
52+
}
53+
symbol, err := strconv.ParseInt(_param, 10, 64)
54+
if err != nil {
55+
return nil, err
56+
}
57+
return &generated.CountRequest{
58+
Symbol: int(symbol),
59+
Text: string(text),
60+
}, nil
3861
}
3962

4063
func DecodeHTTPTestCaseRequest(_ context.Context, r *http.Request) (interface{}, error) {
@@ -62,14 +85,21 @@ func DecodeHTTPTestCaseResponse(_ context.Context, r *http.Response) (interface{
6285
}
6386

6487
func EncodeHTTPUppercaseRequest(ctx context.Context, r *http.Request, request interface{}) error {
88+
r.URL.Path = path.Join(r.URL.Path, "uppercase")
6589
return CommonHTTPRequestEncoder(ctx, r, request)
6690
}
6791

6892
func EncodeHTTPCountRequest(ctx context.Context, r *http.Request, request interface{}) error {
69-
return CommonHTTPRequestEncoder(ctx, r, request)
93+
req := request.(*generated.CountRequest)
94+
r.URL.Path = path.Join(r.URL.Path, "count",
95+
req.Text,
96+
strconv.FormatInt(int64(req.Symbol), 10),
97+
)
98+
return nil
7099
}
71100

72101
func EncodeHTTPTestCaseRequest(ctx context.Context, r *http.Request, request interface{}) error {
102+
r.URL.Path = path.Join(r.URL.Path, "test-case")
73103
return CommonHTTPRequestEncoder(ctx, r, request)
74104
}
75105

example/generated/transport/http/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func NewHTTPClient(addr string, opts ...http.ClientOption) (generated.StringServ
2020
}
2121
return &generated.Endpoints{
2222
CountEndpoint: http.NewClient(
23-
"POST",
23+
"GET",
2424
u,
2525
http1.EncodeHTTPCountRequest,
2626
http1.DecodeHTTPCountResponse,

example/generated/transport/http/server.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,29 @@ import (
66
generated "github.com/devimteam/microgen/example/generated"
77
http2 "github.com/devimteam/microgen/example/generated/transport/converter/http"
88
http "github.com/go-kit/kit/transport/http"
9+
mux "github.com/gorilla/mux"
910
http1 "net/http"
1011
)
1112

1213
func NewHTTPHandler(endpoints *generated.Endpoints, opts ...http.ServerOption) http1.Handler {
13-
handler := http1.NewServeMux()
14-
handler.Handle("/uppercase", http.NewServer(
15-
endpoints.UppercaseEndpoint,
16-
http2.DecodeHTTPUppercaseRequest,
17-
http2.EncodeHTTPUppercaseResponse,
18-
opts...))
19-
handler.Handle("/count", http.NewServer(
20-
endpoints.CountEndpoint,
21-
http2.DecodeHTTPCountRequest,
22-
http2.EncodeHTTPCountResponse,
23-
opts...))
24-
handler.Handle("/test-case", http.NewServer(
25-
endpoints.TestCaseEndpoint,
26-
http2.DecodeHTTPTestCaseRequest,
27-
http2.EncodeHTTPTestCaseResponse,
28-
opts...))
29-
return handler
14+
mux := mux.NewRouter()
15+
mux.Methods("POST").Path("uppercase").Handler(
16+
http.NewServer(
17+
endpoints.UppercaseEndpoint,
18+
http2.DecodeHTTPUppercaseRequest,
19+
http2.EncodeHTTPUppercaseResponse,
20+
opts...))
21+
mux.Methods("GET").Path("count/{text}/{symbol}").Handler(
22+
http.NewServer(
23+
endpoints.CountEndpoint,
24+
http2.DecodeHTTPCountRequest,
25+
http2.EncodeHTTPCountResponse,
26+
opts...))
27+
mux.Methods("POST").Path("test-case").Handler(
28+
http.NewServer(
29+
endpoints.TestCaseEndpoint,
30+
http2.DecodeHTTPTestCaseRequest,
31+
http2.EncodeHTTPTestCaseResponse,
32+
opts...))
33+
return mux
3034
}

generator/decide.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"strings"
88

9+
mstrings "github.com/devimteam/microgen/generator/strings"
910
"github.com/devimteam/microgen/generator/template"
1011
lg "github.com/devimteam/microgen/logger"
1112
"github.com/devimteam/microgen/util"
@@ -29,6 +30,9 @@ const (
2930
GrpcClientTag = template.GrpcClientTag
3031
MainTag = template.MainTag
3132
ErrorLoggingMiddlewareTag = template.ErrorLoggingMiddlewareTag
33+
34+
HttpMethodTag = template.HttpMethodTag
35+
HttpMethodPath = template.HttpMethodPath
3236
)
3337

3438
func ListTemplatesForGen(iface *types.Interface, force bool, importPackageName, absOutPath, sourcePath string) (units []*generationUnit, err error) {
@@ -47,8 +51,8 @@ func ListTemplatesForGen(iface *types.Interface, force bool, importPackageName,
4751
Iface: iface,
4852
AbsOutPath: absOutPath,
4953
SourceFilePath: absSourcePath,
50-
ProtobufPackage: fetchMetaInfo(TagMark+ProtobufTag, iface.Docs),
51-
GRPCRegAddr: fetchMetaInfo(TagMark+GRPCRegAddr, iface.Docs),
54+
ProtobufPackage: mstrings.FetchMetaInfo(TagMark+ProtobufTag, iface.Docs),
55+
GRPCRegAddr: mstrings.FetchMetaInfo(TagMark+GRPCRegAddr, iface.Docs),
5256
FileHeader: defaultFileHeader,
5357
}
5458
stubSvc, err := NewGenUnit(template.NewStubInterfaceTemplate(info), absOutPath)
@@ -84,17 +88,6 @@ func ListTemplatesForGen(iface *types.Interface, force bool, importPackageName,
8488
return units, nil
8589
}
8690

87-
// Fetch information from slice of comments (docs).
88-
// Returns appendix of first comment which has tag as prefix.
89-
func fetchMetaInfo(tag string, comments []string) string {
90-
for _, comment := range comments {
91-
if len(comment) > len(tag) && strings.HasPrefix(comment, tag) {
92-
return comment[len(tag)+1:]
93-
}
94-
}
95-
return ""
96-
}
97-
9891
func tagToTemplate(tag string, info *template.GenerationInfo) (tmpls []template.Template) {
9992
switch tag {
10093
case MiddlewareTag:

0 commit comments

Comments
 (0)