python-betterproto icon indicating copy to clipboard operation
python-betterproto copied to clipboard

default value in oneof content

Open wildfire810 opened this issue 5 years ago • 1 comments

oneof op {
    FixedGate fixedGate = 1;
    RotationGate rotationGate = 2;
    CustomizedGate customizedGate = 3;
    CompositeGate compositeGate = 4;
    string procedureName = 5;
    Measure measure = 6;
    bool barrier = 7;
}

enum FixedGate { // 1 bit ID = 0; X = 1; Y = 2; Z = 3; H = 4; S = 5; SDG = 6; T = 7; TDG = 8; // 2 bit CX = 9; CY = 10; CZ = 11; CH = 12; SWAP = 13; // 3 bit CCX = 14; CSWAP = 15; }

enum RotationGate { // 1 bit U = 0; // Universal rotation RX = 1; RY = 2; RZ = 3; // 2 bit CU = 4; CRX = 5; CRY = 6; CRZ = 7; }

message CustomizedGate { Matrix matrix = 1; }

I only set fixed_gate = 'ID'

betterproto: json.dumps(msg.to_dict(betterproto.Casing.SNAKE, False)) {# nothing "q_reg_list": [ 1 ] }, json.dumps(msg.to_dict(betterproto.Casing.SNAKE, True)) { "fixed_gate": "ID", # oneof default value "rotation_gate": "U", # oneof default value "customized_gate": {, # oneof default value "matrix": { "shape": [], "array": [] } }, "composite_gate": "RZZ", # oneof default value "procedure_name": "", # oneof default value "measure": { # oneof default value "type": "X", "c_reg_list": [] }, "barrier": false, # oneof default value "q_reg_list": [ 1 ], "argument_value_list": [], "argument_id_list": [] },

official MessageToJson(pbObj, preserving_proto_field_name=False) { "fixedGate": "ID", # oneof default value "qRegList": [ 1 ], "argumentValueList": [], "argumentIdList": [] }, MessageToJson(pbObj, preserving_proto_field_name=True) { "fixedGate": "ID", # oneof default value "qRegList": [ 1 ] },

The official is right

wildfire810 avatar Oct 21 '20 11:10 wildfire810

If I understand you correctly, I had the same problem. One has to use betterproto.which_one_of(message, group_name) (more details in the readme) to check which one of them is acutally set, Ignore the defaults in the other fields.

moreApi avatar Jan 09 '25 19:01 moreApi