Skip to content

Commit 39d0aa6

Browse files
#39: Validate first enum value for proto3 (must be zero)
1 parent 8706666 commit 39d0aa6

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

src/main/java/io/protostuff/jetbrains/plugin/annotator/ProtoErrorsAnnotator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
8080
List<EnumConstantNode> constants = anEnum.getConstants();
8181
checkDuplicateEnumConstantNames(constants);
8282
checkDuplicateEnumConstantValues(anEnum, constants);
83+
checkFirstEnumConstantValueIsZero(anEnum, constants, syntax);
8384
} else if (element instanceof ServiceNode) {
8485
ServiceNode service = (ServiceNode) element;
8586
List<RpcMethodNode> rpcMethods = service.getRpcMethods();
@@ -90,6 +91,20 @@ public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder hold
9091
}
9192
}
9293

94+
private void checkFirstEnumConstantValueIsZero(EnumNode anEnum, List<EnumConstantNode> constants, Syntax syntax) {
95+
if (syntax != Syntax.PROTO3) {
96+
return;
97+
}
98+
if (constants.isEmpty()) {
99+
return;
100+
}
101+
EnumConstantNode first = constants.get(0);
102+
if (first.getConstantValue() != 0) {
103+
String message = message("error.first.enum.value.should.be.zero");
104+
markError(first.getConstantValueNode(), null, message);
105+
}
106+
}
107+
93108
private void checkExtendNodeDeprecated(ExtendNode element, Syntax syntax) {
94109
if (syntax != Syntax.PROTO3) {
95110
return;

src/main/resources/io/protostuff/protobuf-jetbrains-plugin/messages/ProtostuffBundle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ error.illegal.field.label=Field label "{0}" is not allowed in proto3
2626
error.default.value.not.supported=Default values are not supported in proto3
2727
error.groups.not.supported=Groups are not supported in proto3
2828
error.extensions.not.supported=Extensions are not supported in proto3
29+
error.first.enum.value.should.be.zero=First enum value must be zero in proto3

src/test/java/io/protostuff/jetbrains/plugin/annotator/ProtoErrorsAnnotatorTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public void testProto3IllegalExtend() {
9090
check();
9191
}
9292

93+
public void testProto3IllegalFirstEnumValue() {
94+
check();
95+
}
96+
9397
private void check() {
9498
String file = getTestName(false) + ".proto";
9599
myFixture.configureByFiles(file);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
package annotator;
4+
5+
enum TestEnum {
6+
UNKNOWN = <error descr="First enum value must be zero in proto3">1</error>;
7+
}

0 commit comments

Comments
 (0)