Skip to content

Commit

Permalink
chore!: improve decoding of required fields
Browse files Browse the repository at this point in the history
A required field, confusingly, should probably be marked *optional* in
the protobuf. That way, we can throw errors when decoding. See [#252]
for more details.

This change:

- Updates `.proto` files to mark all required fields with our custom
  `required` tag
- Updates `.proto` files to mark most required fields as `optional`, so
  we can throw errors when decoding
- Throw errors when decoding if a field is missing

I tested this end-to-end.

[#252]: https://github.com/digidem/mapeo-schema/issues/252
  • Loading branch information
EvanHahn committed Sep 11, 2024
1 parent 9f9c344 commit 2649d78
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 107 deletions.
6 changes: 3 additions & 3 deletions proto/common/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import "versionId/v1.proto";

message Common_1 {
// 32-byte random generated number
bytes docId = 1 [(required) = true];
optional bytes docId = 1 [(required) = true];
repeated VersionId_1 links = 2;
google.protobuf.Timestamp createdAt = 3 [(required) = true];
google.protobuf.Timestamp updatedAt = 4 [(required) = true];
optional google.protobuf.Timestamp createdAt = 3 [(required) = true];
optional google.protobuf.Timestamp updatedAt = 4 [(required) = true];
VersionId_1 originalVersionId = 5;
bool deleted = 6 [(required) = true];
}
Expand Down
12 changes: 6 additions & 6 deletions proto/coreOwnership/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ message CoreOwnership_1 {

Common_1 common = 1;

bytes authCoreId = 5;
bytes configCoreId = 6;
bytes dataCoreId = 7;
bytes blobCoreId = 8;
bytes blobIndexCoreId = 9;
CoreSignatures coreSignatures = 10;
optional bytes authCoreId = 5 [(required) = true];
optional bytes configCoreId = 6 [(required) = true];
optional bytes dataCoreId = 7 [(required) = true];
optional bytes blobCoreId = 8 [(required) = true];
optional bytes blobIndexCoreId = 9 [(required) = true];
CoreSignatures coreSignatures = 10 [(required) = true];
bytes identitySignature = 11;

message CoreSignatures {
Expand Down
2 changes: 1 addition & 1 deletion proto/deviceInfo/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ message DeviceInfo_1 {
selfHostedServer = 4;
}

string name = 5;
optional string name = 5 [(required) = true];
optional DeviceType deviceType = 6;
}
6 changes: 3 additions & 3 deletions proto/field/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ message Field_1 {

Common_1 common = 1;

string tagKey = 5 [(required) = true];
optional string tagKey = 5 [(required) = true];
enum Type {
type_unspecified = 0;
text = 1;
number = 2;
selectOne = 3;
selectMultiple = 4;
}
Type type = 6 [(required) = true];
string label = 7 [(required) = true];
optional Type type = 6 [(required) = true];
optional string label = 7 [(required) = true];
enum Appearance {
appearance_unspecified = 0;
multiline = 1;
Expand Down
8 changes: 4 additions & 4 deletions proto/icon/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ message Icon_1 {
option (schemaName) = "icon";

Common_1 common = 1;
string name = 2 [(required) = true];
optional string name = 2 [(required) = true];

message IconVariantSvg {}

Expand All @@ -22,7 +22,7 @@ message Icon_1 {
x3 = 3;
}

PixelDensity pixelDensity = 1 [(required) = true];
optional PixelDensity pixelDensity = 1 [(required) = true];
}

message IconVariant {
Expand All @@ -43,8 +43,8 @@ message Icon_1 {
int32 index = 2;
}

Size size = 3 [(required) = true];
BlobVersionId blobVersionId = 4 [(required) = true];
optional Size size = 3 [(required) = true];
optional BlobVersionId blobVersionId = 4 [(required) = true];
}
repeated IconVariant variants = 3;
}
4 changes: 2 additions & 2 deletions proto/preset/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ message Preset_1 {

Common_1 common = 1;

string name = 5;
optional string name = 5 [(required) = true];
repeated Geometry geometry = 6;
map<string, TagValue_1> tags = 7;
map<string, TagValue_1> addTags = 8;
map<string, TagValue_1> removeTags = 9;
repeated FieldRef fieldRefs = 10;
optional IconRef iconRef = 11;
repeated string terms = 12;
string color = 13;
optional string color = 13 [(required) = true];

enum Geometry {
geometry_unspecified = 0;
Expand Down
4 changes: 2 additions & 2 deletions proto/role/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ message Role_1 {

Common_1 common = 1;

bytes roleId = 5;
uint32 fromIndex = 6;
optional bytes roleId = 5 [(required) = true];
optional uint32 fromIndex = 6 [(required) = true];
}
8 changes: 4 additions & 4 deletions proto/translation/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ message Translation_1 {
Common_1 common = 1;
DocRef docRef = 2;

string propertyRef = 3;
string languageCode = 4;
string regionCode = 5;
string message = 6;
optional string propertyRef = 3 [(required) = true];
optional string languageCode = 4 [(required) = true];
optional string regionCode = 5 [(required) = true];
optional string message = 6 [(required) = true];

message DocRef {
bytes docId = 1;
Expand Down
Loading

0 comments on commit 2649d78

Please sign in to comment.