Skip to content

Commit 5436a61

Browse files
authored
Merge pull request #368 from bavix/2.x-duplicated
[2.x] strict mode
2 parents 5e56227 + ffb31e1 commit 5436a61

File tree

20 files changed

+268
-238
lines changed

20 files changed

+268
-238
lines changed

go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.22
44

55
require (
66
github.com/bavix/gripmock/protogen v0.0.0
7-
github.com/goccy/go-yaml v1.11.3
7+
github.com/goccy/go-yaml v1.12.0
88
github.com/google/uuid v1.6.0
99
github.com/gorilla/mux v1.8.1
1010
github.com/hashicorp/go-memdb v1.3.4
@@ -32,7 +32,7 @@ require (
3232
github.com/felixge/httpsnoop v1.0.4 // indirect
3333
github.com/go-logr/logr v1.4.2 // indirect
3434
github.com/go-logr/stdr v1.2.2 // indirect
35-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
35+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0 // indirect
3636
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
3737
github.com/hashicorp/golang-lru v1.0.2 // indirect
3838
github.com/mattn/go-colorable v0.1.13 // indirect
@@ -46,9 +46,9 @@ require (
4646
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
4747
golang.org/x/net v0.27.0 // indirect
4848
golang.org/x/sys v0.22.0 // indirect
49-
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
50-
google.golang.org/genproto/googleapis/api v0.0.0-20240708141625-4ad9e859172b // indirect
51-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b // indirect
49+
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9 // indirect
50+
google.golang.org/genproto/googleapis/api v0.0.0-20240730163845-b1a4ccb954bf // indirect
51+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf // indirect
5252
gopkg.in/yaml.v3 v3.0.1 // indirect
5353
)
5454

go.sum

+10-10
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
2525
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
2626
github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k=
2727
github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
28-
github.com/goccy/go-yaml v1.11.3 h1:B3W9IdWbvrUu2OYQGwvU1nZtvMQJPBKgBUuweJjLj6I=
29-
github.com/goccy/go-yaml v1.11.3/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU=
28+
github.com/goccy/go-yaml v1.12.0 h1:/1WHjnMsI1dlIBQutrvSMGZRQufVO3asrHfTwfACoPM=
29+
github.com/goccy/go-yaml v1.12.0/go.mod h1:wKnAMd44+9JAAnGQpWVEgBzGt3YuTaQ4uXoHvE4m7WU=
3030
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
3131
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
3232
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3333
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
3434
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3535
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
3636
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
37-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
38-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
37+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0 h1:CWyXh/jylQWp2dtiV33mY4iSSp6yf4lmn+c7/tN+ObI=
38+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.21.0/go.mod h1:nCLIt0w3Ept2NwF8ThLmrppXsfT07oC8k0XNDxd8sVU=
3939
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
4040
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
4141
github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
@@ -146,12 +146,12 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
146146
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
147147
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
148148
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
149-
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
150-
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
151-
google.golang.org/genproto/googleapis/api v0.0.0-20240708141625-4ad9e859172b h1:y/kpOWeX2pWERnbsvh/hF+Zmo69wVmjyZhstreXQQeA=
152-
google.golang.org/genproto/googleapis/api v0.0.0-20240708141625-4ad9e859172b/go.mod h1:mw8MG/Qz5wfgYr6VqVCiZcHe/GJEfI+oGGDCohaVgB0=
153-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b h1:04+jVzTs2XBnOZcPsLnmrTGqltqJbZQ1Ey26hjYdQQ0=
154-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
149+
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9 h1:LLhsEBxRTBLuKlQxFBYUOU8xyFgXv6cOTp2HASDlsDk=
150+
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
151+
google.golang.org/genproto/googleapis/api v0.0.0-20240730163845-b1a4ccb954bf h1:GillM0Ef0pkZPIB+5iO6SDK+4T9pf6TpaYR6ICD5rVE=
152+
google.golang.org/genproto/googleapis/api v0.0.0-20240730163845-b1a4ccb954bf/go.mod h1:OFMYQFHJ4TM3JRlWDZhJbZfra2uqc3WLBZiaaqP4DtU=
153+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf h1:liao9UHurZLtiEwBgT9LMOnKYsHze6eA6w1KQCMVN2Q=
154+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
155155
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
156156
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
157157
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=

internal/app/rest_server.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type StubsServer struct {
3434
ok atomic.Bool
3535
}
3636

37-
func NewRestServer(path string) (*StubsServer, error) {
37+
func NewRestServer(path string, strictMode bool) (*StubsServer, error) {
3838
stubsStorage, err := storage.New()
3939
if err != nil {
4040
return nil, err
@@ -48,7 +48,7 @@ func NewRestServer(path string) (*StubsServer, error) {
4848
}
4949

5050
if path != "" {
51-
server.readStubs(path) // TODO: someday you will need to rewrite this code
51+
server.readStubs(path, strictMode) // TODO: someday you will need to rewrite this code
5252
}
5353

5454
return server, nil
@@ -233,7 +233,7 @@ func (h *StubsServer) writeResponseError(err error, w http.ResponseWriter) {
233233
}
234234

235235
//nolint:cyclop
236-
func (h *StubsServer) readStubs(path string) {
236+
func (h *StubsServer) readStubs(path string, strictMode bool) {
237237
files, err := os.ReadDir(path)
238238
if err != nil {
239239
log.Printf("Can't read stub from %s. %v\n", path, err)
@@ -243,7 +243,7 @@ func (h *StubsServer) readStubs(path string) {
243243

244244
for _, file := range files {
245245
if file.IsDir() {
246-
h.readStubs(path + "/" + file.Name())
246+
h.readStubs(path+"/"+file.Name(), strictMode)
247247

248248
continue
249249
}
@@ -280,7 +280,7 @@ func (h *StubsServer) readStubs(path string) {
280280
continue
281281
}
282282

283-
h.stubs.Add(storageStubs...)
283+
h.stubs.AddFromFiles(strictMode, storageStubs...)
284284
}
285285
}
286286

main.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func main() {
6262
outputPointer := flag.String("output", "", "directory to output server.go. Default is $GOPATH/src/grpc/")
6363
flag.StringVar(outputPointer, "o", *outputPointer, "alias for -output")
6464

65+
strictMode := false
66+
flag.BoolVar(&strictMode, "strict", false, "enable strict mode")
67+
6568
stubPath := flag.String("stub", "", "Path where the stub files are (Optional)")
6669
imports := flag.String("imports", "/protobuf,/googleapis", "comma separated imports path. default path /protobuf,/googleapis is where gripmock Dockerfile install WKT protos") //nolint:lll
6770

@@ -114,9 +117,10 @@ func main() {
114117

115118
// run admin stub server
116119
stub.RunRestServer(ctx, chReady, stub.Options{
117-
StubPath: *stubPath,
118-
Port: *adminPort,
119-
BindAddr: *adminBindAddr,
120+
StubPath: *stubPath,
121+
Port: *adminPort,
122+
BindAddr: *adminBindAddr,
123+
StrictMode: strictMode,
120124
})
121125

122126
importDirs := strings.Split(*imports, ",")

pkg/storage/stubs.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package storage
22

33
import (
44
"errors"
5+
"fmt"
6+
"log"
57
"slices"
68
"sync"
79
"sync/atomic"
@@ -14,6 +16,7 @@ import (
1416
var (
1517
ErrServiceNotFound = errors.New("service not found")
1618
ErrMethodNotFound = errors.New("method not found")
19+
ErrAlreadyExists = errors.New("already exists")
1720
)
1821

1922
type Stub struct {
@@ -100,10 +103,11 @@ func (s *storage) CheckHeaders() bool {
100103
}
101104

102105
type StubStorage struct {
103-
mu sync.Mutex
104-
used map[uuid.UUID]struct{}
105-
db *memdb.MemDB
106-
total int64
106+
mu sync.Mutex
107+
used map[uuid.UUID]struct{}
108+
loadedFromFiles map[uuid.UUID]struct{}
109+
db *memdb.MemDB
110+
total int64
107111
}
108112

109113
func New() (*StubStorage, error) {
@@ -112,7 +116,32 @@ func New() (*StubStorage, error) {
112116
return nil, err
113117
}
114118

115-
return &StubStorage{db: db, used: map[uuid.UUID]struct{}{}}, nil
119+
return &StubStorage{
120+
db: db,
121+
used: make(map[uuid.UUID]struct{}, 128),
122+
loadedFromFiles: make(map[uuid.UUID]struct{}, 128),
123+
}, nil
124+
}
125+
126+
func (r *StubStorage) AddFromFiles(
127+
strict bool,
128+
stubs ...*Stub,
129+
) []uuid.UUID {
130+
for _, stub := range stubs {
131+
if _, ok := r.loadedFromFiles[stub.GetID()]; ok {
132+
msg := fmt.Sprintf("[WARN] stub %s already loaded from file", stub.GetID())
133+
134+
if strict {
135+
log.Fatalln(msg)
136+
}
137+
138+
log.Println(msg)
139+
}
140+
141+
r.loadedFromFiles[stub.GetID()] = struct{}{}
142+
}
143+
144+
return r.Add(stubs...)
116145
}
117146

118147
func (r *StubStorage) Add(stubs ...*Stub) []uuid.UUID {

protoc-gen-gripmock/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ require (
2121
golang.org/x/net v0.27.0 // indirect
2222
golang.org/x/sync v0.7.0 // indirect
2323
golang.org/x/sys v0.22.0 // indirect
24-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b // indirect
24+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf // indirect
2525
google.golang.org/grpc v1.65.0 // indirect
2626
)

protoc-gen-gripmock/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
3535
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
3636
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
3737
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
38-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b h1:04+jVzTs2XBnOZcPsLnmrTGqltqJbZQ1Ey26hjYdQQ0=
39-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
38+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf h1:liao9UHurZLtiEwBgT9LMOnKYsHze6eA6w1KQCMVN2Q=
39+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
4040
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
4141
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
4242
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=

protogen/example/ms/ms_grpc.pb.go

+18-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protogen/example/multi-files/file1_grpc.pb.go

+18-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)