Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
f9277e69
Commit
f9277e69
authored
Sep 16, 2019
by
Wouter van Oortmerssen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed GenerateText not handling vectors of unions.
Change-Id: Ie82abaf178495c4692e7d10be6b4a13f2fa1bee6
parent
2706381e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
39 deletions
+86
-39
idl_gen_text.cpp
src/idl_gen_text.cpp
+52
-39
test.cpp
tests/test.cpp
+34
-0
No files found.
src/idl_gen_text.cpp
View file @
f9277e69
...
@@ -47,8 +47,9 @@ void OutputIdentifier(const std::string &name, const IDLOptions &opts,
...
@@ -47,8 +47,9 @@ void OutputIdentifier(const std::string &name, const IDLOptions &opts,
// for a single FlatBuffer value into JSON format.
// for a single FlatBuffer value into JSON format.
// The general case for scalars:
// The general case for scalars:
template
<
typename
T
>
template
<
typename
T
>
bool
Print
(
T
val
,
Type
type
,
int
/*indent*/
,
Type
*
/*union_type*/
,
bool
Print
(
T
val
,
Type
type
,
int
/*indent*/
,
const
uint8_t
*
/*prev_val*/
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
soffset_t
/*vector_index*/
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
std
::
string
&
text
=
*
_text
;
std
::
string
&
text
=
*
_text
;
if
(
type
.
enum_def
&&
opts
.
output_enum_identifiers
)
{
if
(
type
.
enum_def
&&
opts
.
output_enum_identifiers
)
{
std
::
vector
<
EnumVal
const
*>
enum_values
;
std
::
vector
<
EnumVal
const
*>
enum_values
;
...
@@ -83,7 +84,8 @@ bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/,
...
@@ -83,7 +84,8 @@ bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/,
// Print a vector or an array of JSON values, comma seperated, wrapped in "[]".
// Print a vector or an array of JSON values, comma seperated, wrapped in "[]".
template
<
typename
T
,
typename
Container
>
template
<
typename
T
,
typename
Container
>
bool
PrintContainer
(
const
Container
&
c
,
size_t
size
,
Type
type
,
int
indent
,
bool
PrintContainer
(
const
Container
&
c
,
size_t
size
,
Type
type
,
int
indent
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
const
uint8_t
*
prev_val
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
std
::
string
&
text
=
*
_text
;
std
::
string
&
text
=
*
_text
;
text
+=
"["
;
text
+=
"["
;
text
+=
NewLine
(
opts
);
text
+=
NewLine
(
opts
);
...
@@ -96,11 +98,12 @@ bool PrintContainer(const Container &c, size_t size, Type type, int indent,
...
@@ -96,11 +98,12 @@ bool PrintContainer(const Container &c, size_t size, Type type, int indent,
if
(
IsStruct
(
type
))
{
if
(
IsStruct
(
type
))
{
if
(
!
Print
(
reinterpret_cast
<
const
void
*>
(
c
.
Data
()
+
if
(
!
Print
(
reinterpret_cast
<
const
void
*>
(
c
.
Data
()
+
i
*
type
.
struct_def
->
bytesize
),
i
*
type
.
struct_def
->
bytesize
),
type
,
indent
+
Indent
(
opts
),
nullptr
,
opts
,
_text
))
{
type
,
indent
+
Indent
(
opts
),
nullptr
,
-
1
,
opts
,
_text
))
{
return
false
;
return
false
;
}
}
}
else
{
}
else
{
if
(
!
Print
(
c
[
i
],
type
,
indent
+
Indent
(
opts
),
nullptr
,
opts
,
_text
))
{
if
(
!
Print
(
c
[
i
],
type
,
indent
+
Indent
(
opts
),
prev_val
,
static_cast
<
soffset_t
>
(
i
),
opts
,
_text
))
{
return
false
;
return
false
;
}
}
}
}
...
@@ -113,43 +116,51 @@ bool PrintContainer(const Container &c, size_t size, Type type, int indent,
...
@@ -113,43 +116,51 @@ bool PrintContainer(const Container &c, size_t size, Type type, int indent,
template
<
typename
T
>
template
<
typename
T
>
bool
PrintVector
(
const
Vector
<
T
>
&
v
,
Type
type
,
int
indent
,
bool
PrintVector
(
const
Vector
<
T
>
&
v
,
Type
type
,
int
indent
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
const
uint8_t
*
prev_val
,
const
IDLOptions
&
opts
,
return
PrintContainer
<
T
,
Vector
<
T
>>
(
v
,
v
.
size
(),
type
,
indent
,
opts
,
_text
);
std
::
string
*
_text
)
{
return
PrintContainer
<
T
,
Vector
<
T
>>
(
v
,
v
.
size
(),
type
,
indent
,
prev_val
,
opts
,
_text
);
}
}
// Print an array a sequence of JSON values, comma separated, wrapped in "[]".
// Print an array a sequence of JSON values, comma separated, wrapped in "[]".
template
<
typename
T
>
template
<
typename
T
>
bool
PrintArray
(
const
Array
<
T
,
0xFFFF
>
&
a
,
size_t
size
,
Type
type
,
int
indent
,
bool
PrintArray
(
const
Array
<
T
,
0xFFFF
>
&
a
,
size_t
size
,
Type
type
,
int
indent
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
return
PrintContainer
<
T
,
Array
<
T
,
0xFFFF
>>
(
a
,
size
,
type
,
indent
,
opts
,
return
PrintContainer
<
T
,
Array
<
T
,
0xFFFF
>>
(
a
,
size
,
type
,
indent
,
nullptr
,
_text
);
opts
,
_text
);
}
}
// Specialization of Print above for pointer types.
// Specialization of Print above for pointer types.
template
<>
template
<>
bool
Print
<
const
void
*>
(
const
void
*
val
,
Type
type
,
int
indent
,
bool
Print
<
const
void
*>
(
const
void
*
val
,
Type
type
,
int
indent
,
Type
*
union_type
,
const
IDLOptions
&
opts
,
const
uint8_t
*
prev_val
,
soffset_t
vector_index
,
std
::
string
*
_text
)
{
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
switch
(
type
.
base_type
)
{
switch
(
type
.
base_type
)
{
case
BASE_TYPE_UNION
:
case
BASE_TYPE_UNION
:
{
// If this assert hits, you have an corrupt buffer, a union type field
// If this assert hits, you have an corrupt buffer, a union type field
// was not present or was out of range.
// was not present or was out of range.
FLATBUFFERS_ASSERT
(
union_type
);
FLATBUFFERS_ASSERT
(
prev_val
);
return
Print
<
const
void
*>
(
val
,
*
union_type
,
indent
,
nullptr
,
opts
,
auto
union_type_byte
=
*
prev_val
;
// Always a uint8_t.
_text
);
if
(
vector_index
>=
0
)
{
case
BASE_TYPE_STRUCT
:
auto
type_vec
=
reinterpret_cast
<
const
Vector
<
uint8_t
>
*>
(
prev_val
+
if
(
!
GenStruct
(
*
type
.
struct_def
,
reinterpret_cast
<
const
Table
*>
(
val
),
ReadScalar
<
uoffset_t
>
(
prev_val
));
indent
,
opts
,
_text
))
{
union_type_byte
=
type_vec
->
Get
(
static_cast
<
uoffset_t
>
(
vector_index
));
}
auto
enum_val
=
type
.
enum_def
->
ReverseLookup
(
union_type_byte
,
true
);
if
(
enum_val
)
{
return
Print
<
const
void
*>
(
val
,
enum_val
->
union_type
,
indent
,
nullptr
,
-
1
,
opts
,
_text
);
}
else
{
return
false
;
return
false
;
}
}
break
;
}
case
BASE_TYPE_STRUCT
:
return
GenStruct
(
*
type
.
struct_def
,
reinterpret_cast
<
const
Table
*>
(
val
),
indent
,
opts
,
_text
);
case
BASE_TYPE_STRING
:
{
case
BASE_TYPE_STRING
:
{
auto
s
=
reinterpret_cast
<
const
String
*>
(
val
);
auto
s
=
reinterpret_cast
<
const
String
*>
(
val
);
if
(
!
EscapeString
(
s
->
c_str
(),
s
->
size
(),
_text
,
opts
.
allow_non_utf8
,
return
EscapeString
(
s
->
c_str
(),
s
->
size
(),
_text
,
opts
.
allow_non_utf8
,
opts
.
natural_utf8
))
{
opts
.
natural_utf8
);
return
false
;
}
break
;
}
}
case
BASE_TYPE_VECTOR
:
{
case
BASE_TYPE_VECTOR
:
{
const
auto
vec_type
=
type
.
VectorType
();
const
auto
vec_type
=
type
.
VectorType
();
...
@@ -161,7 +172,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
...
@@ -161,7 +172,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
case BASE_TYPE_ ## ENUM: \
case BASE_TYPE_ ## ENUM: \
if (!PrintVector<CTYPE>( \
if (!PrintVector<CTYPE>( \
*reinterpret_cast<const Vector<CTYPE> *>(val), \
*reinterpret_cast<const Vector<CTYPE> *>(val), \
vec_type, indent, opts, _text)) { \
vec_type, indent,
prev_val,
opts, _text)) { \
return false; \
return false; \
} \
} \
break;
break;
...
@@ -169,7 +180,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
...
@@ -169,7 +180,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
#undef FLATBUFFERS_TD
#undef FLATBUFFERS_TD
}
}
// clang-format on
// clang-format on
break
;
return
true
;
}
}
case
BASE_TYPE_ARRAY
:
{
case
BASE_TYPE_ARRAY
:
{
const
auto
vec_type
=
type
.
VectorType
();
const
auto
vec_type
=
type
.
VectorType
();
...
@@ -192,11 +203,12 @@ bool Print<const void *>(const void *val, Type type, int indent,
...
@@ -192,11 +203,12 @@ bool Print<const void *>(const void *val, Type type, int indent,
case
BASE_TYPE_ARRAY
:
FLATBUFFERS_ASSERT
(
0
);
case
BASE_TYPE_ARRAY
:
FLATBUFFERS_ASSERT
(
0
);
}
}
// clang-format on
// clang-format on
break
;
return
true
;
}
}
default
:
FLATBUFFERS_ASSERT
(
0
);
default
:
FLATBUFFERS_ASSERT
(
0
);
return
false
;
}
}
return
true
;
}
}
template
<
typename
T
>
static
T
GetFieldDefault
(
const
FieldDef
&
fd
)
{
template
<
typename
T
>
static
T
GetFieldDefault
(
const
FieldDef
&
fd
)
{
...
@@ -215,7 +227,7 @@ static bool GenField(const FieldDef &fd, const Table *table, bool fixed,
...
@@ -215,7 +227,7 @@ static bool GenField(const FieldDef &fd, const Table *table, bool fixed,
fixed
?
reinterpret_cast
<
const
Struct
*>
(
table
)
->
GetField
<
T
>
(
fixed
?
reinterpret_cast
<
const
Struct
*>
(
table
)
->
GetField
<
T
>
(
fd
.
value
.
offset
)
fd
.
value
.
offset
)
:
table
->
GetField
<
T
>
(
fd
.
value
.
offset
,
GetFieldDefault
<
T
>
(
fd
)),
:
table
->
GetField
<
T
>
(
fd
.
value
.
offset
,
GetFieldDefault
<
T
>
(
fd
)),
fd
.
value
.
type
,
indent
,
nullptr
,
opts
,
_text
);
fd
.
value
.
type
,
indent
,
nullptr
,
-
1
,
opts
,
_text
);
}
}
static
bool
GenStruct
(
const
StructDef
&
struct_def
,
const
Table
*
table
,
static
bool
GenStruct
(
const
StructDef
&
struct_def
,
const
Table
*
table
,
...
@@ -223,8 +235,8 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
...
@@ -223,8 +235,8 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
// Generate text for non-scalar field.
// Generate text for non-scalar field.
static
bool
GenFieldOffset
(
const
FieldDef
&
fd
,
const
Table
*
table
,
bool
fixed
,
static
bool
GenFieldOffset
(
const
FieldDef
&
fd
,
const
Table
*
table
,
bool
fixed
,
int
indent
,
Type
*
union_type
,
const
IDLOptions
&
opts
,
int
indent
,
const
uint8_t
*
prev_val
,
std
::
string
*
_text
)
{
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
const
void
*
val
=
nullptr
;
const
void
*
val
=
nullptr
;
if
(
fixed
)
{
if
(
fixed
)
{
// The only non-scalar fields in structs are structs or arrays.
// The only non-scalar fields in structs are structs or arrays.
...
@@ -245,7 +257,7 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
...
@@ -245,7 +257,7 @@ static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
?
table
->
GetStruct
<
const
void
*>
(
fd
.
value
.
offset
)
?
table
->
GetStruct
<
const
void
*>
(
fd
.
value
.
offset
)
:
table
->
GetPointer
<
const
void
*>
(
fd
.
value
.
offset
);
:
table
->
GetPointer
<
const
void
*>
(
fd
.
value
.
offset
);
}
}
return
Print
(
val
,
fd
.
value
.
type
,
indent
,
union_type
,
opts
,
_text
);
return
Print
(
val
,
fd
.
value
.
type
,
indent
,
prev_val
,
-
1
,
opts
,
_text
);
}
}
// Generate text for a struct or table, values separated by commas, indented,
// Generate text for a struct or table, values separated by commas, indented,
...
@@ -255,7 +267,7 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
...
@@ -255,7 +267,7 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
std
::
string
&
text
=
*
_text
;
std
::
string
&
text
=
*
_text
;
text
+=
"{"
;
text
+=
"{"
;
int
fieldout
=
0
;
int
fieldout
=
0
;
Type
*
union_type
=
nullptr
;
const
uint8_t
*
prev_val
=
nullptr
;
for
(
auto
it
=
struct_def
.
fields
.
vec
.
begin
();
for
(
auto
it
=
struct_def
.
fields
.
vec
.
begin
();
it
!=
struct_def
.
fields
.
vec
.
end
();
++
it
)
{
it
!=
struct_def
.
fields
.
vec
.
end
();
++
it
)
{
FieldDef
&
fd
=
**
it
;
FieldDef
&
fd
=
**
it
;
...
@@ -294,16 +306,17 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
...
@@ -294,16 +306,17 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
FLATBUFFERS_GEN_TYPE_ARRAY
(
FLATBUFFERS_TD
)
FLATBUFFERS_GEN_TYPE_ARRAY
(
FLATBUFFERS_TD
)
#undef FLATBUFFERS_TD
#undef FLATBUFFERS_TD
if
(
!
GenFieldOffset
(
fd
,
table
,
struct_def
.
fixed
,
indent
+
Indent
(
opts
),
if
(
!
GenFieldOffset
(
fd
,
table
,
struct_def
.
fixed
,
indent
+
Indent
(
opts
),
union_type
,
opts
,
_text
))
{
prev_val
,
opts
,
_text
))
{
return
false
;
return
false
;
}
}
break
;
break
;
// clang-format on
// clang-format on
}
}
if
(
fd
.
value
.
type
.
base_type
==
BASE_TYPE_UTYPE
)
{
// Track prev val for use with union types.
auto
enum_val
=
fd
.
value
.
type
.
enum_def
->
ReverseLookup
(
if
(
struct_def
.
fixed
)
{
table
->
GetField
<
uint8_t
>
(
fd
.
value
.
offset
,
0
),
true
);
prev_val
=
reinterpret_cast
<
const
uint8_t
*>
(
table
)
+
fd
.
value
.
offset
;
union_type
=
enum_val
?
&
enum_val
->
union_type
:
nullptr
;
}
else
{
prev_val
=
table
->
GetAddressOf
(
fd
.
value
.
offset
);
}
}
}
}
}
}
...
...
tests/test.cpp
View file @
f9277e69
...
@@ -2410,6 +2410,7 @@ void UnionVectorTest() {
...
@@ -2410,6 +2410,7 @@ void UnionVectorTest() {
TestMovie
(
repacked_movie
);
TestMovie
(
repacked_movie
);
// Generate text using mini-reflection.
auto
s
=
auto
s
=
flatbuffers
::
FlatBufferToString
(
fbb
.
GetBufferPointer
(),
MovieTypeTable
());
flatbuffers
::
FlatBufferToString
(
fbb
.
GetBufferPointer
(),
MovieTypeTable
());
TEST_EQ_STR
(
TEST_EQ_STR
(
...
@@ -2451,6 +2452,39 @@ void UnionVectorTest() {
...
@@ -2451,6 +2452,39 @@ void UnionVectorTest() {
" ]
\n
"
" ]
\n
"
"}"
);
"}"
);
// Generate text using parsed schema.
std
::
string
jsongen
;
auto
result
=
GenerateText
(
parser
,
fbb
.
GetBufferPointer
(),
&
jsongen
);
TEST_EQ
(
result
,
true
);
TEST_EQ_STR
(
jsongen
.
c_str
(),
"{
\n
"
" main_character_type:
\"
Rapunzel
\"
,
\n
"
" main_character: {
\n
"
" hair_length: 6
\n
"
" },
\n
"
" characters_type: [
\n
"
"
\"
Belle
\"
,
\n
"
"
\"
MuLan
\"
,
\n
"
"
\"
BookFan
\"
,
\n
"
"
\"
Other
\"
,
\n
"
"
\"
Unused
\"\n
"
" ],
\n
"
" characters: [
\n
"
" {
\n
"
" books_read: 7
\n
"
" },
\n
"
" {
\n
"
" sword_attack_damage: 5
\n
"
" },
\n
"
" {
\n
"
" books_read: 2
\n
"
" },
\n
"
"
\"
Other
\"
,
\n
"
"
\"
Unused
\"\n
"
" ]
\n
"
"}
\n
"
);
flatbuffers
::
Parser
parser2
(
idl_opts
);
flatbuffers
::
Parser
parser2
(
idl_opts
);
TEST_EQ
(
parser2
.
Parse
(
"struct Bool { b:bool; }"
TEST_EQ
(
parser2
.
Parse
(
"struct Bool { b:bool; }"
"union Any { Bool }"
"union Any { Bool }"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment