Skip to content

Commit e8196ee

Browse files
committed
generate code use new cato plugin
1 parent 6090401 commit e8196ee

File tree

21 files changed

+364
-132
lines changed

21 files changed

+364
-132
lines changed

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
2-
3-
build-proto:
4-
protoc -I=. -I="$(PROTO_LIB_PATH)" --cato_out=../ --cato_opt=ext_out_dir=../ ./proto/*/*.proto
5-
61
build:
7-
go build -o proto-cato-gen ./cmd
2+
find proto -name "*.proto" | xargs protoc -I=. -I=$(PROTO_LIB_PATH) --cato_out=../ --cato_opt=ext_out_dir=../,swagger_path=bms_swagger.json,api_host=localhost

bms_swagger.json

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"definitions": {
3+
"proto.domain.BookBrief": {
4+
"description": "",
5+
"properties": {
6+
"cover": {
7+
"description": "",
8+
"type": "string"
9+
},
10+
"id": {
11+
"description": "",
12+
"type": "int64"
13+
},
14+
"name": {
15+
"description": "",
16+
"type": "string"
17+
}
18+
},
19+
"required": [],
20+
"type": "object"
21+
},
22+
"proto.inspect.SearchBooksByCategoryRequest": {
23+
"description": "",
24+
"properties": {
25+
"body": {
26+
"$ref": "#/definitions/proto.inspect.SearchBooksByCategoryRequestBody",
27+
"description": "",
28+
"type": "object"
29+
},
30+
"code": {
31+
"description": "",
32+
"type": "string"
33+
},
34+
"message": {
35+
"description": "",
36+
"type": "string"
37+
}
38+
},
39+
"required": [],
40+
"type": "object"
41+
},
42+
"proto.inspect.SearchBooksByCategoryRequestBody": {
43+
"description": "",
44+
"properties": {
45+
"category": {
46+
"description": "",
47+
"type": "string"
48+
}
49+
},
50+
"required": [],
51+
"type": "object"
52+
},
53+
"proto.inspect.SearchBooksByCategoryResponse": {
54+
"description": "",
55+
"properties": {
56+
"body": {
57+
"$ref": "#/definitions/proto.domain.BookBrief",
58+
"description": "",
59+
"type": "object"
60+
},
61+
"code": {
62+
"description": "",
63+
"type": "string"
64+
},
65+
"message": {
66+
"description": "",
67+
"type": "string"
68+
}
69+
},
70+
"required": [],
71+
"type": "object"
72+
}
73+
},
74+
"host": "localhost",
75+
"info": {
76+
"title": "Generated By Cato",
77+
"version": "1768745762"
78+
},
79+
"paths": {
80+
"/api/bms/v1/search": {
81+
"get": {
82+
"description": "",
83+
"operationId": "",
84+
"parameters": [
85+
{
86+
"description": "",
87+
"in": "body",
88+
"name": "SearchBooksByCategoryRequest",
89+
"required": true,
90+
"schema": {
91+
"$ref": "#/definitions/proto.inspect.SearchBooksByCategoryRequest"
92+
}
93+
}
94+
],
95+
"produces": [
96+
"application/json"
97+
],
98+
"responses": {
99+
"200": {
100+
"description": "",
101+
"schema": {
102+
"$ref": "#/definitions/proto.inspect.SearchBooksByCategoryResponse"
103+
}
104+
}
105+
},
106+
"summary": "",
107+
"tags": [
108+
"BookManageService"
109+
]
110+
}
111+
}
112+
},
113+
"schemes": [
114+
"http",
115+
"https"
116+
],
117+
"swagger": "2.0"
118+
}

go.mod

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
module cato-example-bms
22

3-
go 1.23.3
3+
go 1.24.0
44

5-
require github.com/Masterminds/squirrel v1.5.4
5+
require (
6+
github.com/Masterminds/squirrel v1.5.4
7+
github.com/gin-gonic/gin v1.11.0
8+
github.com/ncuhome/cato v0.0.4
9+
)
610

711
require (
812
github.com/bytedance/sonic v1.14.0 // indirect
913
github.com/bytedance/sonic/loader v0.3.0 // indirect
1014
github.com/cloudwego/base64x v0.1.6 // indirect
1115
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
1216
github.com/gin-contrib/sse v1.1.0 // indirect
13-
github.com/gin-gonic/gin v1.11.0 // indirect
1417
github.com/go-playground/locales v0.14.1 // indirect
1518
github.com/go-playground/universal-translator v0.18.1 // indirect
1619
github.com/go-playground/validator/v10 v10.27.0 // indirect
1720
github.com/goccy/go-json v0.10.2 // indirect
1821
github.com/goccy/go-yaml v1.18.0 // indirect
1922
github.com/json-iterator/go v1.1.12 // indirect
20-
github.com/julienschmidt/httprouter v1.3.0 // indirect
2123
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
2224
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 // indirect
2325
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 // indirect
@@ -28,7 +30,6 @@ require (
2830
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
2931
github.com/quic-go/qpack v0.5.1 // indirect
3032
github.com/quic-go/quic-go v0.54.0 // indirect
31-
github.com/stretchr/testify v1.11.1 // indirect
3233
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
3334
github.com/ugorji/go/codec v1.3.0 // indirect
3435
go.uber.org/mock v0.5.0 // indirect

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w
1515
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
1616
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
1717
github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls=
18+
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
19+
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
1820
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
1921
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
2022
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
@@ -25,11 +27,11 @@ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
2527
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
2628
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
2729
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
30+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
31+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
2832
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
2933
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
3034
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
31-
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
32-
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
3335
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
3436
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
3537
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0 h1:SOEGU9fKiNWd/HOJuq6+3iTQz8KNCLtVX6idSoTLdUw=
@@ -44,6 +46,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
4446
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
4547
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
4648
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
49+
github.com/ncuhome/cato v0.0.4 h1:9jaKfELoaQar/LVyhDkT296z1jbv3lDEj4mD70q5AsA=
50+
github.com/ncuhome/cato v0.0.4/go.mod h1:CDMOCTYCkdL/XPm6fVV/tqY3M0AuZbQ/SI4BDF/d4Ko=
4751
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
4852
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
4953
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

internal/handlers/books/container.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

internal/handlers/books/entry.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package books
22

33
import (
4-
"strings"
5-
64
"github.com/gin-gonic/gin"
75

86
"cato-example-bms/internal/config/app"
@@ -12,11 +10,14 @@ func init() {
1210
handler := NewBookManageServiceHttpHandler(
1311
NewBookManagerService(),
1412
NewBookManagerServiceTier(),
15-
NewBookManageServiceContainer(),
13+
NewBookMangerServiceContainer(),
1614
)
1715
srv := app.GetApp().Group(handler.GetRouterGroupBase())
1816
for path, runner := range handler.GetRoutersMap() {
19-
pattern := strings.SplitN(path, " ", 2)
20-
srv.Handle(pattern[0], pattern[1], gin.WrapF(runner))
17+
m, p, err := handler.DecodeKey(path)
18+
if err != nil {
19+
panic(err)
20+
}
21+
srv.Handle(m, p, gin.WrapF(runner))
2122
}
2223
}

internal/handlers/books/handlers.cato.go

Lines changed: 16 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/handlers/books/service.cato.go

Lines changed: 2 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/handlers/books/service.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
booksinspect "cato-example-bms/internal/inspect/books"
7+
domainbooks "cato-example-bms/internal/models/domain/books"
78
)
89

910
func NewBookManagerService() BookManageServiceService {
@@ -14,5 +15,17 @@ type bookManagerService struct {
1415
}
1516

1617
func (b *bookManagerService) SearchBooksByCategoryV1(ctx context.Context, request *booksinspect.SearchBooksByCategoryRequest) (*booksinspect.SearchBooksByCategoryResponse, error) {
17-
return &booksinspect.SearchBooksByCategoryResponse{}, nil
18+
// get repo
19+
//bookRepo := books.GetRepo()
20+
return &booksinspect.SearchBooksByCategoryResponse{
21+
Code: "0",
22+
Message: "ok",
23+
Body: []*domainbooks.BookBrief{
24+
{
25+
Id: 0,
26+
Name: request.Body.Category,
27+
Cover: "this is cover",
28+
},
29+
},
30+
}, nil
1831
}

internal/handlers/books/tier.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,42 @@ package books
22

33
import (
44
"context"
5+
"encoding/json"
56
"net/http"
7+
"time"
8+
9+
"github.com/ncuhome/cato/core/httpc"
610

711
booksinspect "cato-example-bms/internal/inspect/books"
812
)
913

14+
func NewBookMangerServiceContainer() httpc.Container {
15+
c := new(httpc.BasicMapContainer)
16+
c.Init()
17+
return c
18+
}
19+
1020
func NewBookManagerServiceTier() BookManageServiceServiceTier {
1121
return new(bookManagerServiceTier)
1222
}
1323

14-
type bookManagerServiceTier struct {
15-
}
24+
type bookManagerServiceTier struct{}
1625

17-
func (b bookManagerServiceTier) BuildSearchBooksByCategoryV1Request(input *http.Request) (context.Context, *booksinspect.SearchBooksByCategoryRequest) {
18-
return input.Context(), &booksinspect.SearchBooksByCategoryRequest{}
26+
func (b bookManagerServiceTier) BeforeSearchBooksByCategoryV1Request(input *http.Request) (context.Context, *booksinspect.SearchBooksByCategoryRequest) {
27+
ctx, cancel := context.WithTimeout(input.Context(), time.Second)
28+
defer cancel()
29+
return ctx, &booksinspect.SearchBooksByCategoryRequest{
30+
Body: &booksinspect.SearchBooksByCategoryRequestBody{Category: input.URL.Query().Get("category")},
31+
}
1932
}
2033

21-
func (b bookManagerServiceTier) WrapSearchBooksByCategoryV1Response(output http.ResponseWriter, resp *booksinspect.SearchBooksByCategoryResponse, bizErr error) {
22-
output.Header().Set("Content-Type", "application/json")
23-
output.Write([]byte("{\"books\": \"not found\"}"))
24-
output.WriteHeader(http.StatusOK)
34+
func (b bookManagerServiceTier) AfterSearchBooksByCategoryV1Response(output http.ResponseWriter, resp *booksinspect.SearchBooksByCategoryResponse, err error) {
35+
data, err := json.Marshal(resp)
36+
if err != nil {
37+
output.Write([]byte("Service error"))
38+
output.WriteHeader(503)
39+
} else {
40+
output.Write(data)
41+
output.WriteHeader(200)
42+
}
2543
}

0 commit comments

Comments
 (0)