Skip to content

Commit cb3b646

Browse files
#49: Improved error message for oneof field label
Labels are not allowed for oneof fields.
1 parent fbffbc7 commit cb3b646

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.protostuff.jetbrains.plugin.psi.GroupNode;
2020
import io.protostuff.jetbrains.plugin.psi.MessageField;
2121
import io.protostuff.jetbrains.plugin.psi.MessageNode;
22+
import io.protostuff.jetbrains.plugin.psi.OneOfNode;
2223
import io.protostuff.jetbrains.plugin.psi.OptionNode;
2324
import io.protostuff.jetbrains.plugin.psi.ProtoRootNode;
2425
import io.protostuff.jetbrains.plugin.psi.RangeNode;
@@ -140,15 +141,27 @@ private void checkDefaultValue(@NotNull OptionNode option, Syntax syntax) {
140141

141142

142143
private void checkFieldLabel(FieldNode field, Syntax syntax) {
143-
switch (syntax) {
144-
case PROTO2:
145-
checkFieldLabelProto2(field);
146-
break;
147-
case PROTO3:
148-
checkFieldLabelProto3(field);
149-
break;
150-
default:
151-
throw new IllegalStateException(String.valueOf(syntax));
144+
if (field.getParent() instanceof OneOfNode) {
145+
checkOneofFieldLabel(field);
146+
} else {
147+
switch (syntax) {
148+
case PROTO2:
149+
checkFieldLabelProto2(field);
150+
break;
151+
case PROTO3:
152+
checkFieldLabelProto3(field);
153+
break;
154+
default:
155+
throw new IllegalStateException(String.valueOf(syntax));
156+
}
157+
}
158+
}
159+
160+
private void checkOneofFieldLabel(FieldNode field) {
161+
ASTNode fieldLabelNode = field.getFieldLabelNode();
162+
if (fieldLabelNode != null) {
163+
String message = message("error.illegal.oneof.field.label");
164+
markError(field.getFieldLabelNode(), null, message);
152165
}
153166
}
154167

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
@@ -27,3 +27,4 @@ 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
2929
error.first.enum.value.should.be.zero=First enum value must be zero in proto3
30+
error.illegal.oneof.field.label=Oneof field cannot have label

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ protected String getTestDataPath() {
1414
return "src/test/resources/annotator";
1515
}
1616

17+
public void testNormalOneof() {
18+
check();
19+
}
20+
21+
public void testIllegalOneofLabel() {
22+
check();
23+
}
24+
1725
public void testInvalidTagValue() {
1826
check();
1927
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
syntax = "proto2";
2+
3+
package annotator;
4+
5+
message TestMessage {
6+
7+
oneof action {
8+
<error descr="Oneof field cannot have label">optional</error> int32 field1 = 1;
9+
<error descr="Oneof field cannot have label">required</error> int32 field2 = 2;
10+
<error descr="Oneof field cannot have label">repeated</error> int32 field3 = 3;
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto2";
2+
3+
package annotator;
4+
5+
message TestMessage {
6+
7+
oneof action {
8+
int32 field1 = 1;
9+
int32 field2 = 2;
10+
}
11+
}

0 commit comments

Comments
 (0)