Skip to content

Commit f856656

Browse files
committed
feat(protobuf): enhance support for nested messages and enums #31
Add the capability to properly construct nested messages within Protobuf definitions and include a new test case for enum parsing. This update ensures that the `ProtobufFullIdentListener` correctly handles nested structures and the `ProtobufAnalyserTest` reflects these changes with improved test code readability using the `@Language` annotation for Protobuf syntax highlighting.
1 parent dcd14c4 commit f856656

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

chapi-ast-protobuf/src/main/kotlin/chapi/ast/protobuf/ProtobufFullIdentListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ProtobufFullIdentListener(var fileName: String) : Protobuf3BaseListener()
5252
}
5353

5454
is Protobuf3Parser.MessageDefContext -> {
55-
55+
codeDataStruct.InnerStructures += constructMessageDef(child)
5656
}
5757

5858
is Protobuf3Parser.ExtendDefContext -> {

chapi-ast-protobuf/src/test/kotlin/chapi/ast/protobuf/ProtobufAnalyserTest.kt

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package chapi.ast.protobuf
22

3+
import org.intellij.lang.annotations.Language
34
import org.junit.jupiter.api.Test
45
import org.junit.jupiter.api.Assertions.*
56

@@ -8,7 +9,16 @@ class ProtobufAnalyserTest {
89
@Test
910
fun `should parse valid protobuf code and return a CodeContainer`() {
1011
// Given
11-
val protobufCode = "syntax = \"proto3\";\npackage example;\n\nmessage Person {\n string name = 1;\n int32 id = 2;\n}"
12+
@Language("protobuf")
13+
val protobufCode = """
14+
syntax = "proto3";
15+
16+
package example;
17+
18+
message Person {
19+
string name = 1;
20+
int32 id = 2;
21+
}"""
1222
val filePath = "path/to/file.proto"
1323
val analyser = ProtobufAnalyser()
1424

@@ -37,7 +47,17 @@ class ProtobufAnalyserTest {
3747
@Test
3848
fun `should parse valid protobuf code with enum and return a CodeContainer`() {
3949
// Given
40-
val protobufCode = "syntax = \"proto3\";\npackage example;\n\nenum PhoneType {\n MOBILE = 0;\n HOME = 1;\n WORK = 2;\n}"
50+
@Language("protobuf")
51+
val protobufCode = """
52+
syntax = "proto3";
53+
54+
package example;
55+
56+
enum PhoneType {
57+
MOBILE = 0;
58+
HOME = 1;
59+
WORK = 2;
60+
}"""
4161
val filePath = "path/to/file.proto"
4262
val analyser = ProtobufAnalyser()
4363

@@ -61,4 +81,42 @@ class ProtobufAnalyserTest {
6181
assertEquals("MOBILE", field.TypeKey)
6282
assertEquals("0", field.TypeValue)
6383
}
84+
85+
/// should support message in message
86+
@Test
87+
fun `should parse valid protobuf code with message in message and return a CodeContainer`() {
88+
// Given
89+
@Language("protobuf")
90+
val protobufCode = """syntax = "proto3";
91+
package example;
92+
93+
message Person {
94+
string name = 1;
95+
int32 id = 2;
96+
message Address {
97+
string street = 1;
98+
string city = 2;
99+
}
100+
}"""
101+
val filePath = "path/to/file.proto"
102+
val analyser = ProtobufAnalyser()
103+
104+
// When
105+
val codeContainer = analyser.analysis(protobufCode, filePath)
106+
107+
val dataStruct = codeContainer.DataStructures.first()
108+
val nestedDataStruct = dataStruct.InnerStructures.first()
109+
110+
assertEquals("Address", nestedDataStruct.NodeName)
111+
112+
val nestedField1 = nestedDataStruct.Fields.first()
113+
assertEquals("string", nestedField1.TypeType)
114+
assertEquals("street", nestedField1.TypeKey)
115+
assertEquals("1", nestedField1.TypeValue)
116+
117+
val nestedField2 = nestedDataStruct.Fields[1]
118+
assertEquals("string", nestedField2.TypeType)
119+
assertEquals("city", nestedField2.TypeKey)
120+
assertEquals("2", nestedField2.TypeValue)
121+
}
64122
}

0 commit comments

Comments
 (0)