Skip to content

Commit

Permalink
switch recommended field name to @gotags
Browse files Browse the repository at this point in the history
  • Loading branch information
lrstanley committed Aug 17, 2021
1 parent 3484702 commit 4fcf829
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ injected into the resulting `.pb.go` file. This can be specified above the
field, or trailing the field.

```proto
// @inject_tag: custom_tag:"custom_value"
// @gotags: custom_tag:"custom_value"
```

## Example
Expand All @@ -57,11 +57,11 @@ package pb;
option go_package = "/pb";
message IP {
// @inject_tag: valid:"ip"
// @gotags: valid:"ip"
string Address = 1;
// Or:
string MAC = 2; // @inject_tag: validate:"omitempty"
string MAC = 2; // @gotags: validate:"omitempty"
}
```

Expand All @@ -83,13 +83,22 @@ The custom tags will be injected to `test.pb.go`:

```go
type IP struct {
// @inject_tag: valid:"ip"
// @gotags: valid:"ip"
Address string `protobuf:"bytes,1,opt,name=Address,json=address" json:"Address,omitempty" valid:"ip"`
}
```

To skip the tag for the generated XXX\_\* fields (unknown fields), use the
`-XXX_skip=yaml,xml` flag. Note that this is deprecated, as this functionality
hasn't existed in `protoc-gen-go` since v1.4.x.
## Deprecated functionality

To enable verbose logging, use `-verbose`.
#### Skip `XXX_*` fields

To skip the tag for the generated `XXX_*` fields (unknown fields), use the
`-XXX_skip=yaml,xml` flag. This is deprecated, as this functionality hasn't
existed in `protoc-gen-go` since v1.4.x.

#### `inject_tag` keyword

Since **v1.3.0**, we recommend using `@gotags:` rather than `@inject_tags:`,
as `@gotags` is more indicative of the language the comment is for. We don't
plan on removing `@inject_tags:` support anytime soon, however we strongly
recommend switching to `@gotags`.
7 changes: 6 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
rComment = regexp.MustCompile(`^//.*?@inject_tag:\s*(.*)$`)
rComment = regexp.MustCompile(`^//.*?@(?i:gotags?|inject_tags?):\s*(.*)$`)
rInject = regexp.MustCompile("`.+`$")
rTags = regexp.MustCompile(`[\w_]+:"[^"]+"`)
)
Expand Down Expand Up @@ -105,6 +105,11 @@ func parseFile(inputPath string, xxxSkip []string) (areas []textArea, err error)
if tag == "" {
continue
}

if strings.Contains(comment.Text, "inject_tag") {
logf("warn: deprecated 'inject_tag' used")
}

currentTag := field.Tag.Value
area := textArea{
Start: int(field.Pos()),
Expand Down
14 changes: 7 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ func TestTagFromComment(t *testing.T) {
comment string
tag string
}{
{comment: `//@inject_tag: valid:"abc"`, tag: `valid:"abc"`},
{comment: `// @inject_tag: valid:"abcd"`, tag: `valid:"abcd"`},
{comment: `// @inject_tag: valid:"xyz"`, tag: `valid:"xyz"`},
{comment: `//@gotags: valid:"abc"`, tag: `valid:"abc"`},
{comment: `// @gotags: valid:"abcd"`, tag: `valid:"abcd"`},
{comment: `// @gotags: valid:"xyz"`, tag: `valid:"xyz"`},
{comment: `// fdsafsa`, tag: ""},
{comment: `//@inject_tag:`, tag: ""},
{comment: `// @inject_tag: json:"abc" yaml:"abc`, tag: `json:"abc" yaml:"abc`},
{comment: `// test @inject_tag: json:"abc" yaml:"abc`, tag: `json:"abc" yaml:"abc`},
{comment: `//@gotags:`, tag: ""},
{comment: `// @gotags: json:"abc" yaml:"abc`, tag: `json:"abc" yaml:"abc`},
{comment: `// test @gotags: json:"abc" yaml:"abc`, tag: `json:"abc" yaml:"abc`},
{comment: `// test @inject_tags: json:"abc" yaml:"abc`, tag: `json:"abc" yaml:"abc`},
}
for _, test := range tests {
result := tagFromComment(test.comment)
Expand All @@ -46,7 +47,6 @@ func TestParseWriteFile(t *testing.T) {
t.Fatalf("expected 9 areas to replace, got: %d", len(areas))
}
area := areas[0]
t.Logf("area: %v", area)
if area.InjectTag != expectedTag {
t.Errorf("expected tag: %q, got: %q", expectedTag, area.InjectTag)
}
Expand Down
19 changes: 10 additions & 9 deletions pb/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions pb/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "google/protobuf/any.proto";
import "google/protobuf/empty.proto";

message IP {
string Address = 1; // @inject_tag: valid:"ip" yaml:"ip" json:"overrided"
string Address = 1; // @gotags: valid:"ip" yaml:"ip" json:"overrided"
}

message URL {
Expand All @@ -16,10 +16,11 @@ message URL {
// resulting struct field, you should see `valid:"http|https"` added, not
// `valid:"-"`.
//
// @inject_tag: valid:"-"
string scheme = 1; // @inject_tag: valid:"http|https"
// @gotags: valid:"-"
string scheme = 1; // @gotags: valid:"http|https"
string url = 2;
// @inject_tag: valid:"nonzero"
// Test legacy field name.
// @inject_tags: valid:"nonzero"
int32 port = 3;
}

Expand All @@ -28,26 +29,26 @@ service StreamService {
}

message Params {
// @inject_tag: validate:"omitempty"
// @gotags: validate:"omitempty"
string id = 1;
// gets converted into XXX_Deprecated, for use with testing pre-v1.4.x protoc-gen-go
// functionality that exposes unknown fields as public.
string XXX__deprecated = 2;
}

message Record {
// @inject_tag: validate:"omitempty"
// @gotags: validate:"omitempty"
string id = 1;
google.protobuf.Any test_any = 2;
google.protobuf.Empty test_empty = 3;
}

message OneOfObject {
// @inject_tag: tag:"foo_bar"
// @gotags: tag:"foo_bar"
oneof foo_bar {
// @inject_tag: tag:"foo"
// @gotags: tag:"foo"
string foo = 1;
// @inject_tag: tag:"bar"
// @gotags: tag:"bar"
int64 bar = 2;
}
}

0 comments on commit 4fcf829

Please sign in to comment.