can .proto file fields be without values? - protocol-buffers

I received "missing field number" error. Is it possible to declare fields without value? since i do not have a default value to the fields.
syntax = "proto3";
package tutorial;
message Person {
required string name;
required string email;
}

The field number is not refer to a default value but each field in the message definition has a unique number. These numbers are used to identify your fields in the message binary format, and should not be changed once your message type is in use.
More info here in the doc

Related

how we can parse proto file if it having "." and "-" sign

I create one proto file using java and another using python when I put "." and "-" sign in proto file message field name it shows error that is "missing field number". I m not able to parse that file. and if proto file not accept this character please can share list of character which proto file not accept.
Please find the protobuf documentation on proper syntax for defining messages in protos.
. is used for selecting an attribute of an object in python, so obviously one shouldn't use it in message fields of proto. Instead You can use _ as character for separating words in a single field name.
A small Sample for you :
syntax = "proto3";
message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
}

How to access field value in a JSR message with spring boot 2?

I would like to customize my error message in the following way:
Assume following declaration of a class Person:
#Size(min=10, max=250, message="{size.name}")
private String name;
Within the declared error message in ValidationMessages.properties I would like to output the field value as well, i.e. I would like to do something like this:
size.name = The name '${validatedvalue}' is invalid, its size must be between {min} and {max}
Assume the content of the field 'name' is “xyz”. Then the error message should look like this:
The name 'xyz' is invalid, its size must be between 10 and 250
The substitution for min and max works, but filed value i am getting as '' ,how can I do this for the field value?

Go XML suppression of automatically generated tags?

I'm trying to implement an XML format under Go that was originally written in Fortran. The format is already specified so I'm not free to make changes to the standard. Unfortunately, the format includes data that is not enclosed by an XML tag, thus I would like to suppress the automatic tag creation provided by xml.Marshal.
I've investigated all the standard option associated with marshalling,as documented at : https://golang.org/pkg/encoding/xml/
By default marshalling will use the structure variable name, which can be overridden by the xml: definition. As far as I can tell there is no definition that suppresses the tag name.
type SAO_FREQUENCY_LIST struct {
Type string `xml:",attr"`
SigFig int `xml:",attr"`
Units string `xml:",attr"`
Description string `xml:",attr"`
Frequencies string `xml:""`
}
I want the XML output to be as follows:
<FrequencyList Type="float" SigFig="5" Units="MHz" Description="Nominal Frequency">
3.7 3.8
</FrequencyList>"
By default xml.MarshalIndent(..) yields:
<FrequencyList Type="float" SigFig="5" Units="MHz" Description="”Nominal Frequency">
<Frequencies>3.7 3.8</Frequencies>
</FrequencyList>
You can use the ,chardata modifier to indicate that the value of a struct member should be used as character data for the XML element. For your example, this would be:
type FrequencyList struct {
...
Frequencies string `xml:",chardata"`
}
You can experiment with an example using this change here: https://play.golang.org/p/oBa8HuE-57d

How do I make protobuf case-insensitive?

I have a protobuf contract like this,
message Car{
string carId = 1;
}
I generate java classes from this contract and use it to parse JSON request.
Now if my JSON has "CarID" or "carid" then protobuf generated java classes don't recognize that field. How do I make it case-insensitive?
The protobuff descriptor (.proto) are case insensitive. If you try to compile:
message Car{
string carId = 1;
string carid =2;
}
You will have the compilation error:
CARID_FIELD_NUMBER is already defined in ...
Also you have to know that for proto3, the JSON parser are dealing with lowerCamelCase. As stated on reference guide:
https://developers.google.com/protocol-buffers/docs/proto3#json
Use proto field name instead of lowerCamelCase name: By default proto3
JSON printer should convert the field name to lowerCamelCase and use
that as the JSON name. An implementation may provide an option to use
proto field name as the JSON name instead. Proto3 JSON parsers are
required to accept both the converted lowerCamelCase name and the
proto field name.
From your parser point of view "carID" and "CarID" are the same, because it will automatically convert "CarID" to "carID". But "carId" and "carid" will always be different.

Passing Field Symbol value to Return parameter of method

I have the below code which uses a method. When I try to assign the Field Symbol value [Type ANY] to the return parameter RO_TAB [Type Ref to Data], I am getting an error message OBJECTS_MOVE_NOT SUPPORTED [Conversion of type "l" to type "g" not supported.].
The issue is happening after a BW system upgrade along with which we also moved to ABAP objects. The code executes perfectly in the older version of ABAP.
The dump occurs in the below line:
RO_TAB = <lf_storage>.
I have no idea why.
method GET_LU_STORAGE_FOR_ODS.
* IMPORTS
* IF_ODS TYPE RSODSTECH
* IF_ODS_TABLE_TYPE TYPE ZODS_TAB_TYPE
* RETURNS
* RO_TAB TYPE REF TO DATA
FIELD-SYMBOLS:
<lf_storage> TYPE ANY.
DATA:
lf_index TYPE SY-TABIX,
lf_sindex TYPE STRING,
lf_name TYPE STRING.
lf_index = GET_LU_STORAGE_INDEX(
IF_ODS = IF_ODS
IF_ODS_TABLE_TYPE = IF_ODS_TABLE_TYPE ).
lf_sindex = lf_index.
CONCATENATE
'MO_LU_DATA_'
lf_sindex
INTO lf_name.
ASSIGN lf_name TO <lf_storage>.
RO_TAB = <lf_storage>.
endmethod.
You need to create a data object first, using the CREATE DATA statement. Then you can ASSIGN a field symbol to work with the dynamically created data object. There's an example in the online manual. A field symbol is not a reference, it simply places the variable assigned to it in its position. You're effectively trying to move a string (which is what lf_name is) to a reference variable, and that won't work.
You cannot assign a variable of type STRING to a variable of type REF TO DATA.
The following code snippet shows how it should be done.
DATA: lf_name TYPE string.
DATA: lo_tab TYPE REF TO DATA.
FIELD-SYMBOLS: <lf_name> TYPE string.
lf_name = 'test'.
GET REFERENCE OF lf_name INTO lo_tab.
*lf_name = lo_tab. "this is not allowed
ASSIGN lo_tab->* TO <lf_name>.
So in your case it would be sufficient to define a field symbol.
FIELD-SYMBOLS: <lf_name> TYPE STRING.
then assign the contents referenced by RO_TAB to this field symbol.
ASSIGN ro_tab->* TO <lf_name>.
and finally do the concatenation.
CONCATENATE
'MO_LU_DATA_'
lf_index
INTO <lf_name>.
That's all! No further assignments should be required.
How about just this?
lf_sindex = lf_index.
CONCATENATE
'MO_LU_DATA_'
lf_sindex
INTO RO_TAB.

Resources