Differences between java files generated by using modifiers repeated, required, and optional in protobuf's original configuration file. proto
Recently, I am working on a Netty-based framework project.
This project uses Google's Protobuf as the codec framework. (XML or json is not used to improve efficiency ).
After a period of time, I made a simple summary of the original Protobuf files.
Here I will mainly explain the differences among modifiers repeated, required, and optional in the original file. proto.
0. windows environment, java Project
1. After protobufis installed, find protoc.exe.
2. Write a configuration file for Batch Processing (. bat), as shown in
3. Write a proto file, such:
package trade;message Testq{required string deal_no=1;required string client_id=2;}
(Note: After that, you must compare the differences among modifiers repeated, required, and optional. Therefore, the proto file will be constantly modified .)
4. Start the comparison.
4.1 The difference between using repeated and not using repeated.
Repeated indicates that this field is an array of the specified type. It is a list in java, that is to say, a list set can be called at the time of calling, and it is convenient to do so without calling one by one. The following figure shows the detailed description:
4.1.1 comparison of configuration files,
Let's take a look at the differences in java:
First, we will see that the public interface TestsOrBuilder code is added to java, and then we will see that java. util. List GetTestsList (); Code. After the repeated modifier is added, the generated java code is of the list type.
Then, we will find the Testq code in row 697. Therefore, after repeated is added, more than 600 lines of code will be added. For details, see:
Then, we will find the differences below. For details, see:
For example, the code snippet of internal_static_trade_Tests_descriptor will be added to a repeated file.
A simple description of list is also added.
4.2 differences between requird and optional modifiers
First look at the configuration file, such:
Let's take a look at the differences between the generated java code:
The description comments will be adjusted first, as shown in figure
Then, the key code will be adjusted, such as: Different codes required and optional-1
Different codes: required and optional-2
Different codes: required and optional-3
From the code, we can see that the required option should indicate that this field is required, and optional indicates that this field can be used first.
The difference between \ 001 and \ 002 at last. I will try again later.
This is a description about this \ 002 on the Internet. Someone who knows this can help confirm or explain it.
Control Characters
CTRL (^ D means to hold the CTRL key and hit d)
Oct Dec Char Hex Key Comments
\ 000 0 NUL \ x00 ^ @ \ 0 (Null byte)
\ 001 1 SOH \ x01 ^ A (Start of heading)
\ 002 2 STX \ x02 ^ B (Start of text)