myyang / django-pb-model

Protobuf mixin for django model
Other
112 stars 22 forks source link

Support nested Proto messages in to/from_pb #18

Closed parrotmac closed 4 years ago

parrotmac commented 4 years ago

This change adds support to serializing to a nested proto message by defining a dictionary within pb_2_dj_field_map. For example:

syntax = "proto3";

message Spreads {
  bool mayo = 0;
  bool mustard = 1;
  bool aioli = 2;
}

message Sandwich {
  int32 toastLevel = 1;
  Spreads spreads = 2;
};
class Sandwich(models.Model):
  pb_model = sandwich_pb2.Sandwich
  pb_2_dj_field_map = {
    "toastLevel": "toastLevel",
    "spreads": {
      "mayo": "include_mayo",
      "mustard": "include_mustard",
      "aioli": "include_aioli",
    },
  }
  toast_level = models.IntegerField(default=3)
  include_mayo = models.BooleanField(default=False)
  include_mustard = models.BooleanField(default=False)
  include_aioli = models.BooleanField(default=True)

Still working on from_pb and tests.


This also includes some light formatting, but happy to change that to any preferences.

nicolasmota commented 4 years ago

THANK YOU!