Skip to content

Commit 2f11099

Browse files
committed
adjust code
1 parent de5dfd7 commit 2f11099

File tree

7 files changed

+94
-73
lines changed

7 files changed

+94
-73
lines changed

api/serverNameExample/v1/userExample.proto

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,26 @@ service userExample {
125125
}
126126

127127

128-
// Some notes on defining fields under message:
129-
// (1) Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
130-
// (2) Suggest using camel hump naming for message field names, and for names ending in 'id',
131-
// use xxxID naming format, such as userID, orderID, etc.
132-
// (3) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
133-
// you must add annotations for snake case names, such as string fieldName = 1 [json_name = "field_name"],
134-
// to ensure that the front end and back end JSON naming is consistent.
135-
// (4) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
136-
// message must contain the name of the path parameter and the name should be
137-
// added with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
138-
// (5) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
139-
// a form tag must be added when defining the query parameter in the message,
140-
// such as string name = 1 [(tagger.tags) = "form:\"name\""].
128+
/*
129+
Notes for defining message fields:
130+
1. Suggest using camel case style naming for message field names, such as firstName, lastName, etc.
131+
2. If the message field name ending in 'id', it is recommended to use xxxID naming format, such as userID, orderID, etc.
132+
3. Add validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules, such as:
133+
uint64 id = 1 [(validate.rules).uint64.gte = 1];
134+
135+
If used to generate code that supports the HTTP protocol, notes for defining message fields:
136+
1. If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
137+
message must contain the name of the path parameter and the name should be added
138+
with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
139+
2. If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
140+
a form tag must be added when defining the query parameter in the message, such as:
141+
string name = 1 [(tagger.tags) = "form:\"name\""].
142+
3. If the message field name contain underscores(such as 'field_name'), it will cause a problem
143+
where the JSON field names of the Swagger request parameters are different from those of the
144+
GRPC JSON tag names. There are two solutions: Solution 1, remove the underline from the
145+
message field name. Option 2, use the tool 'protoc-go-inject-tag' to modify the JSON tag name,
146+
such as: string first_name = 1 ; // @gotags: json:"firstName"
147+
*/
141148

142149

143150
enum GenderType {

pkg/conf/parse.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,14 @@ func Parse(configFile string, obj interface{}, reloads ...func()) error {
4646
}
4747

4848
// ParseConfigData parse data to struct
49-
func ParseConfigData(data []byte, format string, obj interface{}, reloads ...func()) error {
49+
func ParseConfigData(data []byte, format string, obj interface{}) error {
5050
viper.SetConfigType(format)
5151
err := viper.ReadConfig(bytes.NewBuffer(data))
5252
if err != nil {
5353
return err
5454
}
5555

56-
err = viper.Unmarshal(obj)
57-
if err != nil {
58-
return err
59-
}
60-
61-
if len(reloads) > 0 {
62-
watchConfig(obj, reloads...)
63-
}
64-
65-
return nil
56+
return viper.Unmarshal(obj)
6657
}
6758

6859
// listening for profile updates

pkg/conf/parse_test.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,15 @@ func TestParseErr(t *testing.T) {
7373
func TestParseConfigData(t *testing.T) {
7474
conf := make(map[string]interface{})
7575

76-
reloads := []func(){
77-
func() {
78-
fmt.Println("close and reconnect mysql")
79-
fmt.Println("close and reconnect redis")
80-
},
76+
data, err := os.ReadFile("test.yml")
77+
if err != nil {
78+
t.Error(err)
79+
return
8180
}
82-
83-
data, _ := os.ReadFile("test.yml")
84-
err := ParseConfigData(data, "yaml", &conf, reloads...)
81+
err = ParseConfigData(data, "yaml", &conf)
8582
if err != nil {
8683
t.Error(err)
8784
return
8885
}
89-
90-
time.Sleep(time.Second)
91-
content, _ := os.ReadFile("test.yml")
92-
contentChange := append(content, byte('#'))
93-
time.Sleep(time.Millisecond * 100)
94-
_ = os.WriteFile("test.yml", contentChange, 0666) // change file
95-
time.Sleep(time.Millisecond * 100)
96-
_ = os.WriteFile("test.yml", content, 0666) // recovery documents
97-
time.Sleep(time.Millisecond * 100)
86+
t.Log(Show(conf))
9887
}

pkg/errcode/http_error.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,13 @@ func (e *Error) WithDetails(details ...string) *Error {
9797
return newError
9898
}
9999

100+
// RewriteMsg rewrite error message
101+
func (e *Error) RewriteMsg(msg string) *Error {
102+
return &Error{code: e.code, msg: msg}
103+
}
104+
100105
// WithOutMsg out error message
101-
// Deprecated: in Err or ErrToHTTP parameter msg can be used to replace the original message.
106+
// Deprecated: use RewriteMsg instead
102107
func (e *Error) WithOutMsg(msg string) *Error {
103108
return &Error{code: e.code, msg: msg}
104109
}

pkg/errcode/http_error_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestNewError(t *testing.T) {
5555
errorsCodes = append(errorsCodes,
5656
DataLoss.WithDetails("foo", "bar"),
5757
DataLoss.WithOutMsg("foobar"),
58+
DataLoss.RewriteMsg("foobar2"),
5859
NewError(1010, "unknown"),
5960
)
6061

pkg/gin/middleware/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
var (
1414
// Print body max length
15-
defaultMaxLength = 40
15+
defaultMaxLength = 300
1616

1717
// default zap log
1818
defaultLogger, _ = zap.NewProduction()

pkg/sql2code/parser/template.go

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,15 @@ service {{.TName}} {
123123
rpc ListByLastID(List{{.TableName}}ByLastIDRequest) returns (List{{.TableName}}ByLastIDReply) {}
124124
}
125125
126-
// Some notes on defining fields under message:
127-
// Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
126+
127+
/*
128+
Notes for defining message fields:
129+
1. Suggest using camel case style naming for message field names, such as firstName, lastName, etc.
130+
2. If the message field name ending in 'id', it is recommended to use xxxID naming format, such as userID, orderID, etc.
131+
3. Add validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules, such as:
132+
uint64 id = 1 [(validate.rules).uint64.gte = 1];
133+
*/
134+
128135
129136
// protoMessageCreateCode
130137
@@ -227,8 +234,15 @@ service {{.TName}} {
227234
rpc List(List{{.TableName}}Request) returns (List{{.TableName}}Reply) {}
228235
}
229236
230-
// Some notes on defining fields under message:
231-
// Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
237+
238+
/*
239+
Notes for defining message fields:
240+
1. Suggest using camel case style naming for message field names, such as firstName, lastName, etc.
241+
2. If the message field name ending in 'id', it is recommended to use xxxID naming format, such as userID, orderID, etc.
242+
3. Add validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules, such as:
243+
uint64 id = 1 [(validate.rules).uint64.gte = 1];
244+
*/
245+
232246
233247
// protoMessageCreateCode
234248
@@ -472,19 +486,26 @@ service {{.TName}} {
472486
}
473487
474488
475-
// Some notes on defining fields under message:
476-
// (1) Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
477-
// (2) Suggest using camel hump naming for message field names, and for names ending in 'id',
478-
// use xxxID naming format, such as userID, orderID, etc.
479-
// (3) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
480-
// you must add annotations for snake case names, such as string fieldName = 1 [json_name = "field_name"],
481-
// to ensure that the front end and back end JSON naming is consistent.
482-
// (4) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
483-
// message must contain the name of the path parameter and the name should be
484-
// added with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
485-
// (5) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
486-
// a form tag must be added when defining the query parameter in the message,
487-
// such as string name = 1 [(tagger.tags) = "form:\"name\""].
489+
/*
490+
Notes for defining message fields:
491+
1. Suggest using camel case style naming for message field names, such as firstName, lastName, etc.
492+
2. If the message field name ending in 'id', it is recommended to use xxxID naming format, such as userID, orderID, etc.
493+
3. Add validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules, such as:
494+
uint64 id = 1 [(validate.rules).uint64.gte = 1];
495+
496+
If used to generate code that supports the HTTP protocol, notes for defining message fields:
497+
1. If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
498+
message must contain the name of the path parameter and the name should be added
499+
with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
500+
2. If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
501+
a form tag must be added when defining the query parameter in the message, such as:
502+
string name = 1 [(tagger.tags) = "form:\"name\""].
503+
3. If the message field name contain underscores(such as 'field_name'), it will cause a problem
504+
where the JSON field names of the Swagger request parameters are different from those of the
505+
GRPC JSON tag names. There are two solutions: Solution 1, remove the underline from the
506+
message field name. Option 2, use the tool 'protoc-go-inject-tag' to modify the JSON tag name,
507+
such as: string first_name = 1 ; // @gotags: json:"firstName"
508+
*/
488509
489510
490511
// protoMessageCreateCode
@@ -692,19 +713,26 @@ service {{.TName}} {
692713
}
693714
694715
695-
// Some notes on defining fields under message:
696-
// (1) Fill in the validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules
697-
// (2) Suggest using camel hump naming for message field names, and for names ending in 'id',
698-
// use xxxID naming format, such as userID, orderID, etc.
699-
// (3) When using the protoc-gen-openapiv2 plugin, if the defined fields are snake case,
700-
// you must add annotations for snake case names, such as string fieldName = 1 [json_name = "field_name"],
701-
// to ensure that the front end and back end JSON naming is consistent.
702-
// (4) If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
703-
// message must contain the name of the path parameter and the name should be
704-
// added with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
705-
// (5) If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
706-
// a form tag must be added when defining the query parameter in the message,
707-
// such as string name = 1 [(tagger.tags) = "form:\"name\""].
716+
/*
717+
Notes for defining message fields:
718+
1. Suggest using camel case style naming for message field names, such as firstName, lastName, etc.
719+
2. If the message field name ending in 'id', it is recommended to use xxxID naming format, such as userID, orderID, etc.
720+
3. Add validate rules https://github.com/envoyproxy/protoc-gen-validate#constraint-rules, such as:
721+
uint64 id = 1 [(validate.rules).uint64.gte = 1];
722+
723+
If used to generate code that supports the HTTP protocol, notes for defining message fields:
724+
1. If the route contains the path parameter, such as /api/v1/userExample/{id}, the defined
725+
message must contain the name of the path parameter and the name should be added
726+
with a new tag, such as int64 id = 1 [(tagger.tags) = "uri:\"id\""];
727+
2. If the request url is followed by a query parameter, such as /api/v1/getUserExample?name=Tom,
728+
a form tag must be added when defining the query parameter in the message, such as:
729+
string name = 1 [(tagger.tags) = "form:\"name\""].
730+
3. If the message field name contain underscores(such as 'field_name'), it will cause a problem
731+
where the JSON field names of the Swagger request parameters are different from those of the
732+
GRPC JSON tag names. There are two solutions: Solution 1, remove the underline from the
733+
message field name. Option 2, use the tool 'protoc-go-inject-tag' to modify the JSON tag name,
734+
such as: string first_name = 1 ; // @gotags: json:"firstName"
735+
*/
708736
709737
710738
// protoMessageCreateCode

0 commit comments

Comments
 (0)