Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotated Binaries emit field names instead of type names #7763

Merged
merged 1 commit into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions src/binary_annotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <vector>

#include "flatbuffers/reflection.h"
#include "flatbuffers/util.h"
#include "flatbuffers/verifier.h"

namespace flatbuffers {
Expand Down Expand Up @@ -679,7 +680,8 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,

if (next_object->is_struct()) {
// Structs are stored inline.
BuildStruct(field_offset, regions, next_object);
BuildStruct(field_offset, regions, field->name()->c_str(),
next_object);
} else {
offset_field_comment.default_value = "(table)";

Expand Down Expand Up @@ -780,6 +782,7 @@ void BinaryAnnotator::BuildTable(const uint64_t table_offset,

uint64_t BinaryAnnotator::BuildStruct(const uint64_t struct_offset,
std::vector<BinaryRegion> &regions,
const std::string referring_field_name,
const reflection::Object *const object) {
if (!object->is_struct()) { return struct_offset; }
uint64_t offset = struct_offset;
Expand All @@ -794,9 +797,8 @@ uint64_t BinaryAnnotator::BuildStruct(const uint64_t struct_offset,

BinaryRegionComment comment;
comment.type = BinaryRegionCommentType::StructField;
comment.name =
std::string(object->name()->c_str()) + "." + field->name()->c_str();
comment.default_value = "(" +
comment.name = referring_field_name + "." + field->name()->str();
comment.default_value = "of '" + object->name()->str() + "' (" +
std::string(reflection::EnumNameBaseType(
field->type()->base_type())) +
")";
Expand All @@ -821,6 +823,7 @@ uint64_t BinaryAnnotator::BuildStruct(const uint64_t struct_offset,
} else if (field->type()->base_type() == reflection::BaseType::Obj) {
// Structs are stored inline, even when nested.
offset = BuildStruct(offset, regions,
referring_field_name + "." + field->name()->str(),
schema_->objects()->Get(field->type()->index()));
} else if (field->type()->base_type() == reflection::BaseType::Array) {
const bool is_scalar = IsScalar(field->type()->element());
Expand All @@ -833,11 +836,11 @@ uint64_t BinaryAnnotator::BuildStruct(const uint64_t struct_offset,
if (is_scalar) {
BinaryRegionComment array_comment;
array_comment.type = BinaryRegionCommentType::ArrayField;
array_comment.name = std::string(object->name()->c_str()) + "." +
field->name()->c_str();
array_comment.name =
referring_field_name + "." + field->name()->str();
array_comment.index = i;
array_comment.default_value =
"(" +
"of '" + object->name()->str() + "' (" +
std::string(
reflection::EnumNameBaseType(field->type()->element())) +
")";
Expand Down Expand Up @@ -869,8 +872,10 @@ uint64_t BinaryAnnotator::BuildStruct(const uint64_t struct_offset,
// TODO(dbaileychess): This works, but the comments on the fields lose
// some context. Need to figure a way how to plumb the nested arrays
// comments together that isn't too confusing.
offset = BuildStruct(offset, regions,
schema_->objects()->Get(field->type()->index()));
offset =
BuildStruct(offset, regions,
referring_field_name + "." + field->name()->str(),
schema_->objects()->Get(field->type()->index()));
}
}
}
Expand Down Expand Up @@ -1018,7 +1023,8 @@ void BinaryAnnotator::BuildVector(const uint64_t vector_offset,
// Vector of structs
for (size_t i = 0; i < vector_length.value(); ++i) {
// Structs are inline to the vector.
const uint64_t next_offset = BuildStruct(offset, regions, object);
const uint64_t next_offset =
BuildStruct(offset, regions, "[" + NumToString(i) + "]", object);
if (next_offset == offset) { break; }
offset = next_offset;
}
Expand Down Expand Up @@ -1301,7 +1307,7 @@ std::string BinaryAnnotator::BuildUnion(const uint64_t union_offset,
// Union of vectors point to a new Binary section
std::vector<BinaryRegion> regions;

BuildStruct(union_offset, regions, object);
BuildStruct(union_offset, regions, field->name()->c_str(), object);

AddSection(
union_offset,
Expand Down
1 change: 1 addition & 0 deletions src/binary_annotator.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class BinaryAnnotator {
const reflection::Object *table);

uint64_t BuildStruct(uint64_t offset, std::vector<BinaryRegion> &regions,
const std::string referring_field_name,
const reflection::Object *structure);

void BuildString(uint64_t offset, const reflection::Object *table,
Expand Down
32 changes: 16 additions & 16 deletions tests/annotated_binary/annotated_binary.afb
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ root_table (AnnotatedBinary.Foo):
+0x004F | 01 | UType8 | 0x01 (1) | table field `anything_type` (UType)
+0x0050 | D2 04 00 00 | uint32_t | 0x000004D2 (1234) | table field `counter` (Int)
+0x0054 | 28 02 00 00 | UOffset32 | 0x00000228 (552) Loc: +0x027C | offset to field `bar` (table)
+0x0058 | 01 00 00 00 | uint32_t | 0x00000001 (1) | struct field `AnnotatedBinary.Building.floors` (Int)
+0x005C | 02 00 00 00 | uint32_t | 0x00000002 (2) | struct field `AnnotatedBinary.Building.doors` (Int)
+0x0060 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | struct field `AnnotatedBinary.Building.windows` (Int)
+0x0064 | 0A 00 00 00 | uint32_t | 0x0000000A (10) | array field `AnnotatedBinary.Dimension.values`[0] (Int)
+0x0068 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | array field `AnnotatedBinary.Dimension.values`[1] (Int)
+0x006C | 14 00 00 00 | uint32_t | 0x00000014 (20) | array field `AnnotatedBinary.Dimension.values`[2] (Int)
+0x0070 | 01 | uint8_t | 0x01 (1) | struct field `AnnotatedBinary.Tolerance.width` (UByte)
+0x0071 | 02 | uint8_t | 0x02 (2) | struct field `AnnotatedBinary.Tolerance.width` (UByte)
+0x0072 | 03 | uint8_t | 0x03 (3) | struct field `AnnotatedBinary.Tolerance.width` (UByte)
+0x0058 | 01 00 00 00 | uint32_t | 0x00000001 (1) | struct field `home.floors` of 'AnnotatedBinary.Building' (Int)
+0x005C | 02 00 00 00 | uint32_t | 0x00000002 (2) | struct field `home.doors` of 'AnnotatedBinary.Building' (Int)
+0x0060 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | struct field `home.windows` of 'AnnotatedBinary.Building' (Int)
+0x0064 | 0A 00 00 00 | uint32_t | 0x0000000A (10) | array field `home.dimensions.values`[0] of 'AnnotatedBinary.Dimension' (Int)
+0x0068 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | array field `home.dimensions.values`[1] of 'AnnotatedBinary.Dimension' (Int)
+0x006C | 14 00 00 00 | uint32_t | 0x00000014 (20) | array field `home.dimensions.values`[2] of 'AnnotatedBinary.Dimension' (Int)
+0x0070 | 01 | uint8_t | 0x01 (1) | struct field `home.dimensions.tolerances.width` of 'AnnotatedBinary.Tolerance' (UByte)
+0x0071 | 02 | uint8_t | 0x02 (2) | struct field `home.dimensions.tolerances.width` of 'AnnotatedBinary.Tolerance' (UByte)
+0x0072 | 03 | uint8_t | 0x03 (3) | struct field `home.dimensions.tolerances.width` of 'AnnotatedBinary.Tolerance' (UByte)
+0x0073 | 00 | uint8_t[1] | . | padding
+0x0074 | C8 01 00 00 | UOffset32 | 0x000001C8 (456) Loc: +0x023C | offset to field `name` (string)
+0x0078 | 5C 01 00 00 | UOffset32 | 0x0000015C (348) Loc: +0x01D4 | offset to field `bars` (vector)
Expand Down Expand Up @@ -97,7 +97,7 @@ table (AnnotatedBinary.Bar):
+0x00D0 | 00 00 00 | uint8_t[3] | ... | padding

union (AnnotatedBinary.Tolerance.measurement):
+0x00D3 | 05 | uint8_t | 0x05 (5) | struct field `AnnotatedBinary.Tolerance.width` (UByte)
+0x00D3 | 05 | uint8_t | 0x05 (5) | struct field `measurement.width` of 'AnnotatedBinary.Tolerance' (UByte)

vector (AnnotatedBinary.Foo.foobars):
+0x00D4 | 03 00 00 00 | uint32_t | 0x00000003 (3) | length of vector (# items)
Expand Down Expand Up @@ -144,12 +144,12 @@ vector (AnnotatedBinary.Foo.foobars_type):

vector (AnnotatedBinary.Foo.points_of_interest):
+0x0134 | 03 00 00 00 | uint32_t | 0x00000003 (3) | length of vector (# items)
+0x0138 | 33 33 33 33 33 A3 45 40 | double | 0x4045A33333333333 (43.275) | struct field `AnnotatedBinary.Location.latitude` (Double)
+0x0140 | 7E 57 04 FF 5B 87 53 C0 | double | 0xC053875BFF04577E (-78.115) | struct field `AnnotatedBinary.Location.longitude` (Double)
+0x0148 | 8D F0 F6 20 04 B6 42 40 | double | 0x4042B60420F6F08D (37.422) | struct field `AnnotatedBinary.Location.latitude` (Double)
+0x0150 | 9F 77 63 41 61 85 5E C0 | double | 0xC05E85614163779F (-122.084) | struct field `AnnotatedBinary.Location.longitude` (Double)
+0x0158 | 8F 35 23 83 DC 35 4B C0 | double | 0xC04B35DC8323358F (-54.4208) | struct field `AnnotatedBinary.Location.latitude` (Double)
+0x0160 | F6 97 DD 93 87 C5 0A 40 | double | 0x400AC58793DD97F6 (3.34645) | struct field `AnnotatedBinary.Location.longitude` (Double)
+0x0138 | 33 33 33 33 33 A3 45 40 | double | 0x4045A33333333333 (43.275) | struct field `[0].latitude` of 'AnnotatedBinary.Location' (Double)
+0x0140 | 7E 57 04 FF 5B 87 53 C0 | double | 0xC053875BFF04577E (-78.115) | struct field `[0].longitude` of 'AnnotatedBinary.Location' (Double)
+0x0148 | 8D F0 F6 20 04 B6 42 40 | double | 0x4042B60420F6F08D (37.422) | struct field `[1].latitude` of 'AnnotatedBinary.Location' (Double)
+0x0150 | 9F 77 63 41 61 85 5E C0 | double | 0xC05E85614163779F (-122.084) | struct field `[1].longitude` of 'AnnotatedBinary.Location' (Double)
+0x0158 | 8F 35 23 83 DC 35 4B C0 | double | 0xC04B35DC8323358F (-54.4208) | struct field `[2].latitude` of 'AnnotatedBinary.Location' (Double)
+0x0160 | F6 97 DD 93 87 C5 0A 40 | double | 0x400AC58793DD97F6 (3.34645) | struct field `[2].longitude` of 'AnnotatedBinary.Location' (Double)

padding:
+0x0168 | 00 00 00 00 | uint8_t[4] | .... | padding
Expand Down
32 changes: 16 additions & 16 deletions tests/annotated_binary/tests/invalid_string_length.afb
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ root_table (AnnotatedBinary.Foo):
+0x004F | 01 | UType8 | 0x01 (1) | table field `anything_type` (UType)
+0x0050 | D2 04 00 00 | uint32_t | 0x000004D2 (1234) | table field `counter` (Int)
+0x0054 | 28 02 00 00 | UOffset32 | 0x00000228 (552) Loc: +0x027C | offset to field `bar` (table)
+0x0058 | 01 00 00 00 | uint32_t | 0x00000001 (1) | struct field `AnnotatedBinary.Building.floors` (Int)
+0x005C | 02 00 00 00 | uint32_t | 0x00000002 (2) | struct field `AnnotatedBinary.Building.doors` (Int)
+0x0060 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | struct field `AnnotatedBinary.Building.windows` (Int)
+0x0064 | 0A 00 00 00 | uint32_t | 0x0000000A (10) | array field `AnnotatedBinary.Dimension.values`[0] (Int)
+0x0068 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | array field `AnnotatedBinary.Dimension.values`[1] (Int)
+0x006C | 14 00 00 00 | uint32_t | 0x00000014 (20) | array field `AnnotatedBinary.Dimension.values`[2] (Int)
+0x0070 | 01 | uint8_t | 0x01 (1) | struct field `AnnotatedBinary.Tolerance.width` (UByte)
+0x0071 | 02 | uint8_t | 0x02 (2) | struct field `AnnotatedBinary.Tolerance.width` (UByte)
+0x0072 | 03 | uint8_t | 0x03 (3) | struct field `AnnotatedBinary.Tolerance.width` (UByte)
+0x0058 | 01 00 00 00 | uint32_t | 0x00000001 (1) | struct field `home.floors` of 'AnnotatedBinary.Building' (Int)
+0x005C | 02 00 00 00 | uint32_t | 0x00000002 (2) | struct field `home.doors` of 'AnnotatedBinary.Building' (Int)
+0x0060 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | struct field `home.windows` of 'AnnotatedBinary.Building' (Int)
+0x0064 | 0A 00 00 00 | uint32_t | 0x0000000A (10) | array field `home.dimensions.values`[0] of 'AnnotatedBinary.Dimension' (Int)
+0x0068 | 0C 00 00 00 | uint32_t | 0x0000000C (12) | array field `home.dimensions.values`[1] of 'AnnotatedBinary.Dimension' (Int)
+0x006C | 14 00 00 00 | uint32_t | 0x00000014 (20) | array field `home.dimensions.values`[2] of 'AnnotatedBinary.Dimension' (Int)
+0x0070 | 01 | uint8_t | 0x01 (1) | struct field `home.dimensions.tolerances.width` of 'AnnotatedBinary.Tolerance' (UByte)
+0x0071 | 02 | uint8_t | 0x02 (2) | struct field `home.dimensions.tolerances.width` of 'AnnotatedBinary.Tolerance' (UByte)
+0x0072 | 03 | uint8_t | 0x03 (3) | struct field `home.dimensions.tolerances.width` of 'AnnotatedBinary.Tolerance' (UByte)
+0x0073 | 00 | uint8_t[1] | . | padding
+0x0074 | C8 01 00 00 | UOffset32 | 0x000001C8 (456) Loc: +0x023C | offset to field `name` (string)
+0x0078 | 5C 01 00 00 | UOffset32 | 0x0000015C (348) Loc: +0x01D4 | offset to field `bars` (vector)
Expand Down Expand Up @@ -95,7 +95,7 @@ table (AnnotatedBinary.Bar):
+0x00D0 | 00 00 00 | uint8_t[3] | ... | padding

union (AnnotatedBinary.Tolerance.measurement):
+0x00D3 | 05 | uint8_t | 0x05 (5) | struct field `AnnotatedBinary.Tolerance.width` (UByte)
+0x00D3 | 05 | uint8_t | 0x05 (5) | struct field `measurement.width` of 'AnnotatedBinary.Tolerance' (UByte)

vector (AnnotatedBinary.Foo.foobars):
+0x00D4 | 03 00 00 00 | uint32_t | 0x00000003 (3) | length of vector (# items)
Expand Down Expand Up @@ -142,12 +142,12 @@ vector (AnnotatedBinary.Foo.foobars_type):

vector (AnnotatedBinary.Foo.points_of_interest):
+0x0134 | 03 00 00 00 | uint32_t | 0x00000003 (3) | length of vector (# items)
+0x0138 | 33 33 33 33 33 A3 45 40 | double | 0x4045A33333333333 (43.275) | struct field `AnnotatedBinary.Location.latitude` (Double)
+0x0140 | 7E 57 04 FF 5B 87 53 C0 | double | 0xC053875BFF04577E (-78.115) | struct field `AnnotatedBinary.Location.longitude` (Double)
+0x0148 | 8D F0 F6 20 04 B6 42 40 | double | 0x4042B60420F6F08D (37.422) | struct field `AnnotatedBinary.Location.latitude` (Double)
+0x0150 | 9F 77 63 41 61 85 5E C0 | double | 0xC05E85614163779F (-122.084) | struct field `AnnotatedBinary.Location.longitude` (Double)
+0x0158 | 8F 35 23 83 DC 35 4B C0 | double | 0xC04B35DC8323358F (-54.4208) | struct field `AnnotatedBinary.Location.latitude` (Double)
+0x0160 | F6 97 DD 93 87 C5 0A 40 | double | 0x400AC58793DD97F6 (3.34645) | struct field `AnnotatedBinary.Location.longitude` (Double)
+0x0138 | 33 33 33 33 33 A3 45 40 | double | 0x4045A33333333333 (43.275) | struct field `[0].latitude` of 'AnnotatedBinary.Location' (Double)
+0x0140 | 7E 57 04 FF 5B 87 53 C0 | double | 0xC053875BFF04577E (-78.115) | struct field `[0].longitude` of 'AnnotatedBinary.Location' (Double)
+0x0148 | 8D F0 F6 20 04 B6 42 40 | double | 0x4042B60420F6F08D (37.422) | struct field `[1].latitude` of 'AnnotatedBinary.Location' (Double)
+0x0150 | 9F 77 63 41 61 85 5E C0 | double | 0xC05E85614163779F (-122.084) | struct field `[1].longitude` of 'AnnotatedBinary.Location' (Double)
+0x0158 | 8F 35 23 83 DC 35 4B C0 | double | 0xC04B35DC8323358F (-54.4208) | struct field `[2].latitude` of 'AnnotatedBinary.Location' (Double)
+0x0160 | F6 97 DD 93 87 C5 0A 40 | double | 0x400AC58793DD97F6 (3.34645) | struct field `[2].longitude` of 'AnnotatedBinary.Location' (Double)

padding:
+0x0168 | 00 00 00 00 | uint8_t[4] | .... | padding
Expand Down
Loading