Skip to content

Commit 67278bf

Browse files
authored
Merge pull request #498 from yoheimuta/allow-alias/495
fix: Honor option allow_alias=true
2 parents 2ec2413 + 4207f2f commit 67278bf

File tree

4 files changed

+80
-8
lines changed

4 files changed

+80
-8
lines changed

.github/workflows/goreleaser.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ jobs:
6565
-u "${{ secrets.OSSRH_USERNAME }}:${{ secrets.OSSRH_TOKEN }}" \
6666
-H "Content-Type: application/json" \
6767
-d '{}')
68-
68+
6969
http_code="${response: -3}"
7070
response_body="${response%???}"
71-
71+
7272
echo "HTTP Status: $http_code"
7373
echo "Response: $response_body"
74-
74+
7575
if [ "$http_code" -ne 200 ] && [ "$http_code" -ne 201 ] && [ "$http_code" -ne 204 ]; then
7676
echo "Upload failed with HTTP $http_code"
7777
echo "Response: $response_body"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ See AutoDisable columns below.
343343
| No | _ | - | ENUM_FIELDS_HAVE_COMMENT | Verifies that all enum fields have a comment. You can configure to enforce Golang Style comments with `.protolint.yaml`. |
344344
| No | _ | - | FILE_HAS_COMMENT | Verifies that a file starts with a doc comment. |
345345
| No | _ | - | SYNTAX_CONSISTENT | Verifies that syntax is a specified version. The default is proto3. You can configure the version with `.protolint.yaml`. |
346-
| No | _ | - | FIELD_NUMBERS_ORDER_ASCENDING | Verifies the order of fields is ascending. |
346+
| No | _ | - | FIELD_NUMBERS_ORDER_ASCENDING | Verifies the order of fields is ascending. For enums, honors `option allow_alias = true;` by allowing adjacent equal numbers (non-decreasing). |
347347

348348
I recommend that you add `all_default: true` in `.protolint.yaml`, because all linters above are automatically enabled so that you can always enjoy maximum benefits whenever protolint is updated.
349349

@@ -610,4 +610,4 @@ bash release.sh <version> [message]
610610

611611
## License
612612

613-
The MIT License (MIT)
613+
The MIT License (MIT)

internal/addon/rules/fieldNumbersOrderAscendingRule.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (v *fieldNumbersOrderAscendingVisitor) VisitMessage(message *parser.Message
8585
continue
8686
}
8787

88-
number, isError := v.isAscending(field.Meta.Pos, field.FieldName, number, lastName, lastNumber)
88+
number, isError := v.isAscending(field.Meta.Pos, field.FieldName, number, lastName, lastNumber, false)
8989
if isError {
9090
hasError = true
9191
}
@@ -105,6 +105,19 @@ func (v *fieldNumbersOrderAscendingVisitor) VisitEnum(enum *parser.Enum) bool {
105105
hasError bool
106106
)
107107

108+
// Detect enum option: allow_alias = true;
109+
allowAlias := false
110+
for _, element := range enum.EnumBody {
111+
opt, ok := element.(*parser.Option)
112+
if !ok {
113+
continue
114+
}
115+
if opt.OptionName == "allow_alias" && opt.Constant == "true" {
116+
allowAlias = true
117+
break
118+
}
119+
}
120+
108121
for _, element := range enum.EnumBody {
109122
field, ok := element.(*parser.EnumField)
110123
if !ok {
@@ -133,7 +146,7 @@ func (v *fieldNumbersOrderAscendingVisitor) VisitEnum(enum *parser.Enum) bool {
133146
continue
134147
}
135148

136-
number, isError := v.isAscending(field.Meta.Pos, field.Ident, number, lastIdent, lastNumber)
149+
number, isError := v.isAscending(field.Meta.Pos, field.Ident, number, lastIdent, lastNumber, allowAlias)
137150
if isError {
138151
hasError = true
139152
}
@@ -146,9 +159,12 @@ func (v *fieldNumbersOrderAscendingVisitor) VisitEnum(enum *parser.Enum) bool {
146159
}
147160

148161
func (v *fieldNumbersOrderAscendingVisitor) isAscending(
149-
pos meta.Position, fieldName string, number int, lastName string, lastNumber int,
162+
pos meta.Position, fieldName string, number int, lastName string, lastNumber int, allowEqual bool,
150163
) (curNumber int, hasError bool) {
151164
if number == lastNumber {
165+
if allowEqual {
166+
return number, false
167+
}
152168
v.AddFailuref(
153169
pos,
154170
"fields %s and %s have the same number %d",

internal/addon/rules/fieldNumbersOrderAscendingRule_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,62 @@ func TestFieldNumbersOrderAscendingRule_Apply(t *testing.T) {
1717
inputProto *parser.Proto
1818
wantFailures []report.Failure
1919
}{
20+
{
21+
name: "no failures for proto enum with allow_alias and adjacent duplicate numbers",
22+
inputProto: &parser.Proto{
23+
ProtoBody: []parser.Visitee{
24+
&parser.Service{},
25+
&parser.Enum{
26+
EnumBody: []parser.Visitee{
27+
&parser.Option{OptionName: "allow_alias", Constant: "true"},
28+
&parser.EnumField{Ident: "FIRST_VALUE", Number: "1"},
29+
&parser.EnumField{Ident: "FIRST_VALUE_ALIAS", Number: "1"},
30+
},
31+
},
32+
},
33+
},
34+
wantFailures: nil,
35+
},
36+
{
37+
name: "failures for proto enum with allow_alias but non-adjacent decrease (1,2,1)",
38+
inputProto: &parser.Proto{
39+
ProtoBody: []parser.Visitee{
40+
&parser.Service{},
41+
&parser.Enum{
42+
EnumBody: []parser.Visitee{
43+
&parser.Option{OptionName: "allow_alias", Constant: "true"},
44+
&parser.EnumField{Ident: "FIRST_VALUE", Number: "1"},
45+
&parser.EnumField{Ident: "SECOND_VALUE", Number: "2"},
46+
&parser.EnumField{Ident: "THIRD_VALUE", Number: "1"},
47+
},
48+
},
49+
},
50+
},
51+
wantFailures: []report.Failure{
52+
report.Failuref(
53+
meta.Position{},
54+
"FIELD_NUMBERS_ORDER_ASCENDING",
55+
string(rule.SeverityError),
56+
"field SECOND_VALUE should be after THIRD_VALUE (ascending order expected)",
57+
),
58+
},
59+
},
60+
{
61+
name: "no failures for proto enum with allow_alias and zero value alias (0,0)",
62+
inputProto: &parser.Proto{
63+
ProtoBody: []parser.Visitee{
64+
&parser.Service{},
65+
&parser.Enum{
66+
EnumBody: []parser.Visitee{
67+
&parser.Option{OptionName: "allow_alias", Constant: "true"},
68+
&parser.EnumField{Ident: "VALUE_UNSPECIFIED", Number: "0"},
69+
&parser.EnumField{Ident: "VALUE_UNSPECIFIED_ALIAS", Number: "0"},
70+
},
71+
},
72+
},
73+
},
74+
wantFailures: nil,
75+
},
2076
{
2177
name: "no failures for proto enum with ascending order numbers",
2278
inputProto: &parser.Proto{

0 commit comments

Comments
 (0)