Protobuf the configuration of the original file. Proto, the differences between Java files generated using modifiers repeated, required, optional

Source: Internet
Author: User
Tags control characters modifiers testq

Recently working on a project based on the Netty framework.

The project uses Google's PROTOBUF as the codec framework. (XML or JSON is not used for better efficiency).

After a period of use I have a simple summary of how to use Protobuf's original files.

I'm mainly here to illustrate the original file. Proto, modifier repeated, required, optional difference.

0.windows Environment, Java Engineering

1. After installing PROTOBUF, locate Protoc.exe

2. Write a configuration file for batch processing (. bat) as shown in



3. Write a proto file, such as:


Package Trade;message testq{required string deal_no=1;required string client_id=2;}

(Note: After this, the repeated, required, and optional are compared, so the proto file is constantly modified.) )


4. Start the comparison.


4.1 The difference between using repeated and not using repeated.

Repeated means that the field is an array of the specified type. In Java is a list, it is said, in the call can call a list collection, do not need a single call, this is very convenient. Let's take a look at the diagram for a detailed explanation:


4.1.1 Configuration file comparison,


Here's a look at the differences in Java:


First we'll look at the addition of this code in Java to the public interface Testsorbuilder, and then we'll see, Java.util.list<trade. Test.testq> gettestslist (), the code that explains the addition of the repeated modifier to the generated Java code is the list type.




We will then find the TESTQ code in 697 rows, so adding more than 600 lines of code will be added to the repeated modifier. See:



Then you will find, in the below, the difference, see:

For example, add the Internal_static_trade_tests_descriptor code snippet to the file with the repeated



A simple description of the list is also added.



4.2 about the differences between Requird and optional modifiers


Look at the configuration file first, such as:



Then look at the difference between the generated Java code:


The explanatory notes are adjusted first, such as




The key code will then be adjusted, for example: different code required and OPTIONAL-1




Not the same code required and OPTIONAL-2




Not the same code required and optional-3





From the code we can see that the required option should indicate that the field must be selected, and optional indicates that it can be first.


Finally the difference between the \001 and the \002, I found out and then add it.

This is on the Internet to find a section on the \002 of the instructions, have the understanding of people to 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 ^@ (Null byte)
\001 1 SOH \x01 ^a (Start of heading)
\002 2 STX \x02 ^b (Start of text)









Protobuf the configuration of the original file. Proto, the differences between Java files generated using modifiers repeated, required, optional

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.