Skip to content

Commit b15cb55

Browse files
committed
fix: config validation
1 parent d6509ca commit b15cb55

File tree

6 files changed

+243
-170
lines changed

6 files changed

+243
-170
lines changed

config/v1/config.pb.go

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

config/v1/config.pb.validate.go

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

config/v1/config.proto

+15-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ message Server {
1818
}
1919

2020
message Service {
21-
string name = 1 [(validate.rules).string.prefix= "service.goim"];
22-
string version = 2 [(validate.rules).string.min_len=1];
21+
// name has three parts:
22+
// 1. constant value: "goim"
23+
// 2. service type: ["service", "worker", "admin"]
24+
// 3. service name: any string
25+
// parts joined by "."
26+
// example: "goim.service.chat"
27+
string name = 1 [(validate.rules).string = {pattern: "^goim\\.(service|worker|admin)\\.[a-zA-Z0-9_]+$"}];
28+
// version must match like "vx.y.z"
29+
string version = 2 [(validate.rules).string = {pattern: "^v[0-9]+\\.[0-9]+\\.[0-9]+$"}];
2330
optional Server http = 3;
2431
optional Server grpc = 4;
2532
Log log = 5;
@@ -28,11 +35,12 @@ message Service {
2835
MQ mq = 8;
2936
MySQL mysql = 9;
3037
// services name
31-
string gatewayService = 10 [(validate.rules).string.const= "service.goim.gateway"];
32-
string userService = 11 [(validate.rules).string.const= "service.goim.user"];
33-
string pushService = 12 [(validate.rules).string.const= "service.goim.push"];
34-
string storeService = 13 [(validate.rules).string.const= "service.goim.store"];
35-
string msgService = 14 [(validate.rules).string.const= "service.goim.msg"];
38+
string gatewayService = 10;
39+
string userService = 11;
40+
string pushService = 12;
41+
string storeWorker = 13;
42+
string msgService = 14;
43+
string msgWorker = 15;
3644
}
3745

3846
enum Level {

go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ go 1.17
44

55
require (
66
github.com/envoyproxy/protoc-gen-validate v0.6.7
7+
github.com/stretchr/testify v1.7.0
78
google.golang.org/grpc v1.47.0
89
google.golang.org/protobuf v1.28.0
910
)
1011

1112
require (
13+
github.com/davecgh/go-spew v1.1.0 // indirect
1214
github.com/golang/protobuf v1.5.2 // indirect
15+
github.com/pmezard/go-difflib v1.0.0 // indirect
1316
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
1417
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912 // indirect
1518
golang.org/x/text v0.3.7 // indirect
1619
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
20+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
1721
)

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XP
1111
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
1212
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
1313
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
14+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
1415
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1516
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
1617
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@@ -52,6 +53,7 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
5253
github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
5354
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
5455
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
56+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5557
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5658
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
5759
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
@@ -61,6 +63,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
6163
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
6264
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
6365
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
66+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
6467
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6568
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
6669
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
@@ -155,9 +158,11 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
155158
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
156159
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
157160
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
161+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
158162
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
159163
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
160164
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
165+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
161166
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
162167
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
163168
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

tests/config/config_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
configv1 "github.com/go-goim/api/config/v1"
9+
)
10+
11+
func Test_Config_Validate(t *testing.T) {
12+
type args struct {
13+
config *configv1.Service
14+
}
15+
tests := []struct {
16+
name string
17+
args args
18+
wantErr bool
19+
}{
20+
{
21+
name: "validate config",
22+
args: args{
23+
config: &configv1.Service{
24+
Name: "goim.service.test",
25+
Version: "v0.0.1",
26+
},
27+
},
28+
wantErr: false,
29+
},
30+
{
31+
name: "validate config with empty name",
32+
args: args{
33+
config: &configv1.Service{
34+
Name: "",
35+
Version: "v0.0.1",
36+
},
37+
},
38+
wantErr: true,
39+
},
40+
{
41+
name: "validate config with invalid name",
42+
args: args{
43+
config: &configv1.Service{
44+
Name: "goim.xxx.test",
45+
Version: "v0.0.1",
46+
},
47+
},
48+
wantErr: true,
49+
},
50+
{
51+
name: "validate config with empty version",
52+
args: args{
53+
config: &configv1.Service{
54+
Name: "goim.service.test",
55+
Version: "",
56+
},
57+
},
58+
wantErr: true,
59+
},
60+
{
61+
name: "validate config with invalid version",
62+
args: args{
63+
config: &configv1.Service{
64+
Name: "goim.service.test",
65+
Version: "v0.0.1.0",
66+
},
67+
},
68+
wantErr: true,
69+
},
70+
}
71+
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
gotErr := tt.args.config.Validate()
75+
if tt.wantErr {
76+
assert.Error(t, gotErr)
77+
} else {
78+
assert.NoError(t, gotErr)
79+
}
80+
},
81+
)
82+
}
83+
}

0 commit comments

Comments
 (0)