Search code examples
pythonjsonprotocol-buffersproto

protobuf : Optional attributes' values are not cleared


we are currently running a python application that receives json messages, parse it according to the given dict using ParseDict , serialize it SerializeToString and send it.

# encode.py
from google.protobuf.json_format import ParseDict



payload = ParseDict(data, data_t_v1())
raw_payload = payload.SerializeToString()

This what we have in proto file:

//data.proto syntax = "proto2";

message data_t_v1 {
 required uint32 id = 1;
 optional uint32 offset = 2;
 optional uint32 multiplier = 3;
 optional uint32 mask = 4;
 }

However, when I was testing different scenarios, I had an issue concerning the values of optional attributes. Here is what happens:

Test 1- sending the message with the optional values

data = {
"id": 1,
"offset": 2,
"multiplier":10,
"mask": 4294967295 }

Test 2- sending the same message without the optional values

data = {
"id": 1 }

The issue happens here: ParseDict is returning a dict with the optional attributes having the old values that I set in Test 1.

I would like to know if there is a way to clear old optional values when they are not set. Thanks!


Solution

  • Are you create a new instance of your proto message for each test?

    from google.protobuf.json_format import ParseDict
    
    # Create a new msg
    msg = your_packge_pb2.data_t_v1()
    
    data = {
      "id": 1,
      "offset": 2,
      "multiplier":10,
      "mask": 4294967295,
    }
    
    # Parse data into msg
    ParseDict(data, msg)
    
    # Do something with msg
    print(msg)
    
    # Create a new msg
    msg = your_packge_pb2.data_t_v1()
    
    data = {
      "id": 1,
    }
    
    # Parse data into msg
    ParseDict(data, msg)
    
    # Do something with msg
    print(msg)
    

    NOTE The style guide recommends using PascalCase for message names rather than snake_case.