From 7bb6fbbb81ae68412a3b37433a7ebbfaf8594ee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B5=A9?= Date: Sun, 8 May 2022 00:09:02 +0800 Subject: [PATCH 01/19] feat: add ignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 698e891b6..e6155ae25 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ # For files created by specific development environment (e.g. editor), # use alternative ways to exclude files from git. # For example, set up .git/info/exclude or use a global .gitignore. +.idea From 2443a0ee4696acaa9aa4bf8c2e0586d7c724c645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E6=B5=A9?= Date: Sun, 8 May 2022 02:00:31 +0800 Subject: [PATCH 02/19] feat: gen message struct support add extra field tags --- cmd/protoc-gen-go/internal_gengo/gotags.go | 128 ++++++++++++++++++ .../internal_gengo/gotags_test.go | 42 ++++++ cmd/protoc-gen-go/internal_gengo/main.go | 6 +- 3 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 cmd/protoc-gen-go/internal_gengo/gotags.go create mode 100644 cmd/protoc-gen-go/internal_gengo/gotags_test.go diff --git a/cmd/protoc-gen-go/internal_gengo/gotags.go b/cmd/protoc-gen-go/internal_gengo/gotags.go new file mode 100644 index 000000000..a74e85ff0 --- /dev/null +++ b/cmd/protoc-gen-go/internal_gengo/gotags.go @@ -0,0 +1,128 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// Added by Hao Luo +// Source: https://github.com/hacksomecn/protobuf-go.git +// Branch: feature/tags + +// Package internal_gengo Support add custom struct field tags +// Protoc parse field tags from field's tailing comment, declare extra tags like: +// message Example { +// ... +// string name = 1; // @go_tags(`bson:"name" yaml:"name"`) example nam +// ... +// } +// +// Go tags regexp: `(\s?|//)@go_tags\(` + "(`.*`)" + `\)\s` +package internal_gengo + +import ( + "google.golang.org/protobuf/compiler/protogen" + "log" + "regexp" + "strings" +) + +var tailingGoTagsExcludeKeys = map[string]bool{ + "protobuf": true, + "protobuf_key": true, + "protobuf_val": true, +} + +var commentGoTagsRe *regexp.Regexp + +func init() { + var err error + commentGoTagsRe, err = regexp.Compile(`(\s?|//)@go_tags\(` + "(`.*`)" + `\)\s`) + if err != nil { + log.Fatalf("compile comment go tags regexp failed. %s", err) + return + } +} + +// AppendGoTagsFromTailingComment append extra tags parsed from tailing comment +// tag with same name will be replaced except protobuf tags like "protobuf"、"protobuf_key"、"protobuf_val" +func AppendGoTagsFromTailingComment( + existsTags structTags, + tailComment protogen.Comments, +) ( + newTags structTags, + newTailing protogen.Comments, +) { + newTags = existsTags + newTailing = tailComment + + tagsMap := map[string]string{} // key -> value + seqKeys := make([]string, 0) + for _, existTags := range existsTags { + key := existTags[0] + value := existTags[1] + tagsMap[key] = value + seqKeys = append(seqKeys, key) + } + + tailTags, newTailing := ParseGoTagsFromTailingComment(tailComment) + for key, tailTag := range tailTags { + if tailingGoTagsExcludeKeys[key] { + continue + } + + _, exists := tagsMap[key] + if !exists { + seqKeys = append(seqKeys, key) + } + + tagsMap[key] = tailTag + } + + newTags = make([][2]string, 0) + for _, key := range seqKeys { + tag := tagsMap[key] + newTags = append(newTags, [2]string{key, tag}) + } + + return +} + +// ParseGoTagsFromTailingComment parse go tags from comment +func ParseGoTagsFromTailingComment(tailing protogen.Comments) ( + tags map[string]string, + newTailing protogen.Comments, +) { + tags = make(map[string]string) + newTailing = tailing + + matched := commentGoTagsRe.FindStringSubmatch(string(tailing)) + if len(matched) != 3 { + return + } + + strMatched := matched[0] + strStart := matched[1] + strTagsReplacer := strings.Replace(strMatched, strStart, "", 1) + newTailing = protogen.Comments(strings.Replace(string(tailing), strTagsReplacer, "", 1)) + + strTags := matched[2] + strTags = strings.Trim(strTags, "`") + + strPairs := strings.Split(strTags, " ") + for _, pair := range strPairs { + pair = strings.TrimSpace(pair) + if pair == "" { + continue + } + + separateIndex := strings.Index(pair, ":") + if separateIndex < 0 || separateIndex == len(pair)-1 { + continue + } + + key := pair[:separateIndex] + value := pair[separateIndex+1:] + value = strings.Trim(value, "\"") + + tags[key] = value + } + + return +} diff --git a/cmd/protoc-gen-go/internal_gengo/gotags_test.go b/cmd/protoc-gen-go/internal_gengo/gotags_test.go new file mode 100644 index 000000000..03d2a7c2d --- /dev/null +++ b/cmd/protoc-gen-go/internal_gengo/gotags_test.go @@ -0,0 +1,42 @@ +package internal_gengo + +import ( + "fmt" + "google.golang.org/protobuf/compiler/protogen" + "regexp" + "testing" +) + +func TestCommentTagsReg(t *testing.T) { + re, err := regexp.Compile(`(\s?)@go_tags\(` + "(`.*`)" + `\)\s`) + if err != nil { + t.Error(err) + return + } + + str := " @go_tags(`json:\"name\"`) abc" + matched := re.FindStringSubmatch(str) + fmt.Println(len(matched), matched) +} + +func TestParseGoTagsFromTailingComment(t *testing.T) { + str := " @go_tags(`json:\"name,omitempty\"`) abc" + tags, newTailing := ParseGoTagsFromTailingComment(protogen.Comments(str)) + for key, value := range tags { + fmt.Println(key, value) + } + fmt.Println(newTailing) +} + +func TestAppendGoTagsFromTailingComment(t *testing.T) { + tags := structTags{ + {"protobuf", "abc"}, + {"json", "efg"}, + {"protobuf_key", "string"}, + {"protobuf_val", "string"}, + } + str := " @go_tags(`json:\"name,omitempty\" bson:\"name\"`) abc" + newTags, newTailing := AppendGoTagsFromTailingComment(tags, protogen.Comments(str)) + fmt.Println(newTailing) + fmt.Println(newTags) +} diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go index d34efa9b1..ac7992f19 100644 --- a/cmd/protoc-gen-go/internal_gengo/main.go +++ b/cmd/protoc-gen-go/internal_gengo/main.go @@ -415,6 +415,9 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie tags = append(tags, gotrackTags...) } + var leftTailing protogen.Comments + tags, leftTailing = AppendGoTagsFromTailingComment(tags, field.Comments.Trailing) + name := field.GoName if field.Desc.IsWeak() { name = genid.WeakFieldPrefix_goname + name @@ -424,7 +427,8 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated()) g.P(leadingComments, name, " ", goType, tags, - trailingComment(field.Comments.Trailing)) + //trailingComment(field.Comments.Trailing)) + trailingComment(leftTailing)) sf.append(field.GoName) } From ab0bdedb93ad6d72016593a24c675bdce553e022 Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 19 Sep 2023 12:21:17 +0800 Subject: [PATCH 03/19] temp --- cmd/protoc-gen-go/internal_gengo/gotags.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/protoc-gen-go/internal_gengo/gotags.go b/cmd/protoc-gen-go/internal_gengo/gotags.go index a74e85ff0..e4b45acd5 100644 --- a/cmd/protoc-gen-go/internal_gengo/gotags.go +++ b/cmd/protoc-gen-go/internal_gengo/gotags.go @@ -13,7 +13,7 @@ // ... // } // -// Go tags regexp: `(\s?|//)@go_tags\(` + "(`.*`)" + `\)\s` +// Go tags regexp: `(\s?)@go_tags\(` + "(`.*`)" + `\)\s` package internal_gengo import ( @@ -33,7 +33,7 @@ var commentGoTagsRe *regexp.Regexp func init() { var err error - commentGoTagsRe, err = regexp.Compile(`(\s?|//)@go_tags\(` + "(`.*`)" + `\)\s`) + commentGoTagsRe, err = regexp.Compile(`(\s?)@go_tags\(` + "(`.*`)" + `\)\s`) if err != nil { log.Fatalf("compile comment go tags regexp failed. %s", err) return From b35b0bfffe8c58725bfb8632d439b45847871153 Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 19 Sep 2023 13:21:21 +0800 Subject: [PATCH 04/19] modify --- go.mod | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 8858601bb..eab176dbb 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module google.golang.org/protobuf +module github.com/hacksomecn/protobuf-go go 1.11 @@ -6,3 +6,7 @@ require ( github.com/golang/protobuf v1.5.0 github.com/google/go-cmp v0.5.5 ) + +replace ( + google.golang.org/protobuf => ./ +) \ No newline at end of file From d822a9ffbb5259afee0caf5c92ef004cf17a35b2 Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 19 Sep 2023 13:23:19 +0800 Subject: [PATCH 05/19] modify --- go.mod | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/go.mod b/go.mod index eab176dbb..8858601bb 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/hacksomecn/protobuf-go +module google.golang.org/protobuf go 1.11 @@ -6,7 +6,3 @@ require ( github.com/golang/protobuf v1.5.0 github.com/google/go-cmp v0.5.5 ) - -replace ( - google.golang.org/protobuf => ./ -) \ No newline at end of file From 404aec5286a7df354ec32f265420a6ea0040e11e Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:01:48 +0800 Subject: [PATCH 06/19] modify --- go.mod | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 8858601bb..37a4cc400 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,13 @@ -module google.golang.org/protobuf +module github.com/hacksomecn/protobuf-go go 1.11 require ( github.com/golang/protobuf v1.5.0 github.com/google/go-cmp v0.5.5 + google.golang.org/protobuf v0.0.0 +) + +replace ( + google.golang.org/protobuf v0.0.0 => github.com/hacksomecn/protobuf-go@"feature/gotags" ) From e5f2e4be9e4b5686aa01e1acd5d9b182c682a603 Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:06:47 +0800 Subject: [PATCH 07/19] modify --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 37a4cc400..a910c17ff 100644 --- a/go.mod +++ b/go.mod @@ -9,5 +9,5 @@ require ( ) replace ( - google.golang.org/protobuf v0.0.0 => github.com/hacksomecn/protobuf-go@"feature/gotags" + google.golang.org/protobuf v0.0.0 => github.com/hacksomecn/protobuf-go "feature/gotags" ) From 97cb298899791c4961fd27d1dfdc12c1030788ba Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:08:52 +0800 Subject: [PATCH 08/19] modfy --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a910c17ff..ce0898346 100644 --- a/go.mod +++ b/go.mod @@ -9,5 +9,5 @@ require ( ) replace ( - google.golang.org/protobuf v0.0.0 => github.com/hacksomecn/protobuf-go "feature/gotags" + google.golang.org/protobuf v0.0.0 => github.com/hacksomecn/protobuf-go "v1.31.0_gotags" ) From 6924ef7fbf58cd2e30b9fcc6f4e415c36a284067 Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:10:49 +0800 Subject: [PATCH 09/19] modify --- go.mod | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index ce0898346..f68cd8bf7 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,8 @@ -module github.com/hacksomecn/protobuf-go +module google.golang.org/protobuf go 1.11 require ( github.com/golang/protobuf v1.5.0 github.com/google/go-cmp v0.5.5 - google.golang.org/protobuf v0.0.0 -) - -replace ( - google.golang.org/protobuf v0.0.0 => github.com/hacksomecn/protobuf-go "v1.31.0_gotags" -) +) \ No newline at end of file From 0dd17ea15f66b36b6c1fdd30ede38a2d8166d87c Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:22:31 +0800 Subject: [PATCH 10/19] modify --- go.mod | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/go.mod b/go.mod index f68cd8bf7..f81769f49 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,8 @@ go 1.11 require ( github.com/golang/protobuf v1.5.0 github.com/google/go-cmp v0.5.5 +) + +replace ( + google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v100.0.0 ) \ No newline at end of file From 08c6035ebbc8f9eb8665d50cef787698b667c488 Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:25:44 +0800 Subject: [PATCH 11/19] modify --- go.mod | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index f81769f49..eb5dd6750 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module google.golang.org/protobuf +module github.com/hacksomecn/protobuf-go go 1.11 @@ -8,5 +8,5 @@ require ( ) replace ( - google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v100.0.0 + google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v10.0.0 ) \ No newline at end of file From 1fcb9548bb6cda5874806b0c0c9c0ac864fb1307 Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:28:15 +0800 Subject: [PATCH 12/19] modify --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index eb5dd6750..6e1b228cf 100644 --- a/go.mod +++ b/go.mod @@ -8,5 +8,5 @@ require ( ) replace ( - google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v10.0.0 + google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v1.1000.0 ) \ No newline at end of file From ad53af6b01431aea8813af4636edec10a66948e2 Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:29:25 +0800 Subject: [PATCH 13/19] modify --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index 6e1b228cf..7a2dcf3b1 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.11 require ( github.com/golang/protobuf v1.5.0 github.com/google/go-cmp v0.5.5 + google.golang.org/protobuf v0.0.0 ) replace ( From 09de045aac147af4d5c0f63c4d3e3a70cd33939d Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:29:43 +0800 Subject: [PATCH 14/19] modify --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 7a2dcf3b1..1fe914098 100644 --- a/go.mod +++ b/go.mod @@ -9,5 +9,5 @@ require ( ) replace ( - google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v1.1000.0 + google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v1.1000.1 ) \ No newline at end of file From 2dcb26bd4dce2808a33ce9dd2d05d151fdff0c0f Mon Sep 17 00:00:00 2001 From: haozi Date: Tue, 7 Nov 2023 22:36:03 +0800 Subject: [PATCH 15/19] modify --- go.mod | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 1fe914098..bd5c0dbd3 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,13 @@ -module github.com/hacksomecn/protobuf-go +module google.golang.org/protobuf go 1.11 require ( github.com/golang/protobuf v1.5.0 github.com/google/go-cmp v0.5.5 - google.golang.org/protobuf v0.0.0 + //google.golang.org/protobuf v0.0.0 ) -replace ( - google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v1.1000.1 -) \ No newline at end of file +//replace ( +// google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v1.1000.1 +//) \ No newline at end of file From 477f19955d501a40fb2d54a9f31211eb511aea9f Mon Sep 17 00:00:00 2001 From: haozi Date: Wed, 8 Nov 2023 14:44:26 +0800 Subject: [PATCH 16/19] modify --- go.mod | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/go.mod b/go.mod index bd5c0dbd3..f68cd8bf7 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,4 @@ go 1.11 require ( github.com/golang/protobuf v1.5.0 github.com/google/go-cmp v0.5.5 - //google.golang.org/protobuf v0.0.0 -) - -//replace ( -// google.golang.org/protobuf => github.com/hacksomecn/protobuf-go v1.1000.1 -//) \ No newline at end of file +) \ No newline at end of file From 23878c1713057cbcb766467250b6798b7ee8545f Mon Sep 17 00:00:00 2001 From: hao Date: Mon, 26 Feb 2024 20:57:36 +0800 Subject: [PATCH 17/19] modify --- cmd/protoc-gen-go/internal_gengo/gotags.go | 4 ++-- cmd/protoc-gen-go/internal_gengo/gotags_test.go | 2 +- cmd/protoc-gen-go/internal_gengo/main.go | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/protoc-gen-go/internal_gengo/gotags.go b/cmd/protoc-gen-go/internal_gengo/gotags.go index e4b45acd5..11448607b 100644 --- a/cmd/protoc-gen-go/internal_gengo/gotags.go +++ b/cmd/protoc-gen-go/internal_gengo/gotags.go @@ -40,9 +40,9 @@ func init() { } } -// AppendGoTagsFromTailingComment append extra tags parsed from tailing comment +// AppendGoTagsFromFieldComment append extra tags parsed from tailing comment // tag with same name will be replaced except protobuf tags like "protobuf"、"protobuf_key"、"protobuf_val" -func AppendGoTagsFromTailingComment( +func AppendGoTagsFromFieldComment( existsTags structTags, tailComment protogen.Comments, ) ( diff --git a/cmd/protoc-gen-go/internal_gengo/gotags_test.go b/cmd/protoc-gen-go/internal_gengo/gotags_test.go index 03d2a7c2d..719ed2c2a 100644 --- a/cmd/protoc-gen-go/internal_gengo/gotags_test.go +++ b/cmd/protoc-gen-go/internal_gengo/gotags_test.go @@ -36,7 +36,7 @@ func TestAppendGoTagsFromTailingComment(t *testing.T) { {"protobuf_val", "string"}, } str := " @go_tags(`json:\"name,omitempty\" bson:\"name\"`) abc" - newTags, newTailing := AppendGoTagsFromTailingComment(tags, protogen.Comments(str)) + newTags, newTailing := AppendGoTagsFromFieldComment(tags, protogen.Comments(str)) fmt.Println(newTailing) fmt.Println(newTags) } diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go index cbf5cfc62..411ea881e 100644 --- a/cmd/protoc-gen-go/internal_gengo/main.go +++ b/cmd/protoc-gen-go/internal_gengo/main.go @@ -419,7 +419,8 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie } var leftTailing protogen.Comments - tags, leftTailing = AppendGoTagsFromTailingComment(tags, field.Comments.Trailing) + tags, _ = AppendGoTagsFromFieldComment(tags, field.Comments.Leading) + tags, leftTailing = AppendGoTagsFromFieldComment(tags, field.Comments.Trailing) name := field.GoName if field.Desc.IsWeak() { From ccc86117b92d35e825bc738179809ae4419b4727 Mon Sep 17 00:00:00 2001 From: hao Date: Mon, 26 Feb 2024 21:02:33 +0800 Subject: [PATCH 18/19] modify --- cmd/protoc-gen-go/internal_gengo/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go index 411ea881e..f34f47bba 100644 --- a/cmd/protoc-gen-go/internal_gengo/main.go +++ b/cmd/protoc-gen-go/internal_gengo/main.go @@ -418,8 +418,9 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie tags = append(tags, gotrackTags...) } + var leftLeading protogen.Comments var leftTailing protogen.Comments - tags, _ = AppendGoTagsFromFieldComment(tags, field.Comments.Leading) + tags, leftLeading = AppendGoTagsFromFieldComment(tags, field.Comments.Leading) tags, leftTailing = AppendGoTagsFromFieldComment(tags, field.Comments.Trailing) name := field.GoName @@ -427,7 +428,7 @@ func genMessageField(g *protogen.GeneratedFile, f *fileInfo, m *messageInfo, fie name = genid.WeakFieldPrefix_goname + name } g.Annotate(m.GoIdent.GoName+"."+name, field.Location) - leadingComments := appendDeprecationSuffix(field.Comments.Leading, + leadingComments := appendDeprecationSuffix(leftLeading, field.Desc.ParentFile(), field.Desc.Options().(*descriptorpb.FieldOptions).GetDeprecated()) g.P(leadingComments, From 1f51ab07ea6ae05f03f0b800a83843c1b6838eb9 Mon Sep 17 00:00:00 2001 From: hao Date: Fri, 12 Apr 2024 19:08:33 +0800 Subject: [PATCH 19/19] fix tags seq --- cmd/protoc-gen-go/internal_gengo/gotags.go | 25 +++++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/cmd/protoc-gen-go/internal_gengo/gotags.go b/cmd/protoc-gen-go/internal_gengo/gotags.go index 11448607b..27a137e67 100644 --- a/cmd/protoc-gen-go/internal_gengo/gotags.go +++ b/cmd/protoc-gen-go/internal_gengo/gotags.go @@ -9,11 +9,11 @@ // Protoc parse field tags from field's tailing comment, declare extra tags like: // message Example { // ... -// string name = 1; // @go_tags(`bson:"name" yaml:"name"`) example nam +// string name = 1; // @go_tags(`bson:"name" yaml:"name"`) FORM 1 support comment tail // ... // } // -// Go tags regexp: `(\s?)@go_tags\(` + "(`.*`)" + `\)\s` +// FORM 1: Go tags regexp: `(\s?)@go_tags\(` + "(`.*`)" + `\)\s` package internal_gengo import ( @@ -62,17 +62,19 @@ func AppendGoTagsFromFieldComment( } tailTags, newTailing := ParseGoTagsFromTailingComment(tailComment) - for key, tailTag := range tailTags { + for _, tailTag := range tailTags { + key := tailTag.Key + value := tailTag.Value if tailingGoTagsExcludeKeys[key] { continue } _, exists := tagsMap[key] - if !exists { + if !exists { // keep sequence seqKeys = append(seqKeys, key) } - tagsMap[key] = tailTag + tagsMap[key] = value } newTags = make([][2]string, 0) @@ -84,12 +86,16 @@ func AppendGoTagsFromFieldComment( return } +type GoTag struct { + Key string + Value string +} + // ParseGoTagsFromTailingComment parse go tags from comment func ParseGoTagsFromTailingComment(tailing protogen.Comments) ( - tags map[string]string, + tags []GoTag, newTailing protogen.Comments, ) { - tags = make(map[string]string) newTailing = tailing matched := commentGoTagsRe.FindStringSubmatch(string(tailing)) @@ -121,7 +127,10 @@ func ParseGoTagsFromTailingComment(tailing protogen.Comments) ( value := pair[separateIndex+1:] value = strings.Trim(value, "\"") - tags[key] = value + tags = append(tags, GoTag{ + Key: key, + Value: value, + }) } return