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
Show 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,
// for a single FlatBuffer value into JSON format.
// The general case for scalars:
template
<
typename
T
>
bool
Print
(
T
val
,
Type
type
,
int
/*indent*/
,
Type
*
/*union_type*/
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
bool
Print
(
T
val
,
Type
type
,
int
/*indent*/
,
const
uint8_t
*
/*prev_val*/
,
soffset_t
/*vector_index*/
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
std
::
string
&
text
=
*
_text
;
if
(
type
.
enum_def
&&
opts
.
output_enum_identifiers
)
{
std
::
vector
<
EnumVal
const
*>
enum_values
;
...
...
@@ -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 "[]".
template
<
typename
T
,
typename
Container
>
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
;
text
+=
"["
;
text
+=
NewLine
(
opts
);
...
...
@@ -96,11 +98,12 @@ bool PrintContainer(const Container &c, size_t size, Type type, int indent,
if
(
IsStruct
(
type
))
{
if
(
!
Print
(
reinterpret_cast
<
const
void
*>
(
c
.
Data
()
+
i
*
type
.
struct_def
->
bytesize
),
type
,
indent
+
Indent
(
opts
),
nullptr
,
opts
,
_text
))
{
type
,
indent
+
Indent
(
opts
),
nullptr
,
-
1
,
opts
,
_text
))
{
return
false
;
}
}
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
;
}
}
...
...
@@ -113,43 +116,51 @@ bool PrintContainer(const Container &c, size_t size, Type type, int indent,
template
<
typename
T
>
bool
PrintVector
(
const
Vector
<
T
>
&
v
,
Type
type
,
int
indent
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
return
PrintContainer
<
T
,
Vector
<
T
>>
(
v
,
v
.
size
(),
type
,
indent
,
opts
,
_text
);
const
uint8_t
*
prev_val
,
const
IDLOptions
&
opts
,
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 "[]".
template
<
typename
T
>
bool
PrintArray
(
const
Array
<
T
,
0xFFFF
>
&
a
,
size_t
size
,
Type
type
,
int
indent
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
return
PrintContainer
<
T
,
Array
<
T
,
0xFFFF
>>
(
a
,
size
,
type
,
indent
,
opts
,
_text
);
return
PrintContainer
<
T
,
Array
<
T
,
0xFFFF
>>
(
a
,
size
,
type
,
indent
,
nullptr
,
opts
,
_text
);
}
// Specialization of Print above for pointer types.
template
<>
bool
Print
<
const
void
*>
(
const
void
*
val
,
Type
type
,
int
indent
,
Type
*
union_type
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
const
uint8_t
*
prev_val
,
soffset_t
vector_index
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
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
// was not present or was out of range.
FLATBUFFERS_ASSERT
(
union_type
);
return
Print
<
const
void
*>
(
val
,
*
union_type
,
indent
,
nullptr
,
opts
,
_text
);
case
BASE_TYPE_STRUCT
:
if
(
!
GenStruct
(
*
type
.
struct_def
,
reinterpret_cast
<
const
Table
*>
(
val
),
indent
,
opts
,
_text
))
{
FLATBUFFERS_ASSERT
(
prev_val
);
auto
union_type_byte
=
*
prev_val
;
// Always a uint8_t.
if
(
vector_index
>=
0
)
{
auto
type_vec
=
reinterpret_cast
<
const
Vector
<
uint8_t
>
*>
(
prev_val
+
ReadScalar
<
uoffset_t
>
(
prev_val
));
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
;
}
break
;
}
case
BASE_TYPE_STRUCT
:
return
GenStruct
(
*
type
.
struct_def
,
reinterpret_cast
<
const
Table
*>
(
val
),
indent
,
opts
,
_text
);
case
BASE_TYPE_STRING
:
{
auto
s
=
reinterpret_cast
<
const
String
*>
(
val
);
if
(
!
EscapeString
(
s
->
c_str
(),
s
->
size
(),
_text
,
opts
.
allow_non_utf8
,
opts
.
natural_utf8
))
{
return
false
;
}
break
;
return
EscapeString
(
s
->
c_str
(),
s
->
size
(),
_text
,
opts
.
allow_non_utf8
,
opts
.
natural_utf8
);
}
case
BASE_TYPE_VECTOR
:
{
const
auto
vec_type
=
type
.
VectorType
();
...
...
@@ -161,7 +172,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
case BASE_TYPE_ ## ENUM: \
if (!PrintVector<CTYPE>( \
*reinterpret_cast<const Vector<CTYPE> *>(val), \
vec_type, indent, opts, _text)) { \
vec_type, indent,
prev_val,
opts, _text)) { \
return false; \
} \
break;
...
...
@@ -169,7 +180,7 @@ bool Print<const void *>(const void *val, Type type, int indent,
#undef FLATBUFFERS_TD
}
// clang-format on
break
;
return
true
;
}
case
BASE_TYPE_ARRAY
:
{
const
auto
vec_type
=
type
.
VectorType
();
...
...
@@ -192,11 +203,12 @@ bool Print<const void *>(const void *val, Type type, int indent,
case
BASE_TYPE_ARRAY
:
FLATBUFFERS_ASSERT
(
0
);
}
// 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
)
{
...
...
@@ -215,7 +227,7 @@ static bool GenField(const FieldDef &fd, const Table *table, bool fixed,
fixed
?
reinterpret_cast
<
const
Struct
*>
(
table
)
->
GetField
<
T
>
(
fd
.
value
.
offset
)
:
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
,
...
...
@@ -223,8 +235,8 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
// Generate text for non-scalar field.
static
bool
GenFieldOffset
(
const
FieldDef
&
fd
,
const
Table
*
table
,
bool
fixed
,
int
indent
,
Type
*
union_type
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
int
indent
,
const
uint8_t
*
prev_val
,
const
IDLOptions
&
opts
,
std
::
string
*
_text
)
{
const
void
*
val
=
nullptr
;
if
(
fixed
)
{
// 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,
?
table
->
GetStruct
<
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,
...
...
@@ -255,7 +267,7 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
std
::
string
&
text
=
*
_text
;
text
+=
"{"
;
int
fieldout
=
0
;
Type
*
union_type
=
nullptr
;
const
uint8_t
*
prev_val
=
nullptr
;
for
(
auto
it
=
struct_def
.
fields
.
vec
.
begin
();
it
!=
struct_def
.
fields
.
vec
.
end
();
++
it
)
{
FieldDef
&
fd
=
**
it
;
...
...
@@ -294,16 +306,17 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
FLATBUFFERS_GEN_TYPE_ARRAY
(
FLATBUFFERS_TD
)
#undef FLATBUFFERS_TD
if
(
!
GenFieldOffset
(
fd
,
table
,
struct_def
.
fixed
,
indent
+
Indent
(
opts
),
union_type
,
opts
,
_text
))
{
prev_val
,
opts
,
_text
))
{
return
false
;
}
break
;
// clang-format on
}
if
(
fd
.
value
.
type
.
base_type
==
BASE_TYPE_UTYPE
)
{
auto
enum_val
=
fd
.
value
.
type
.
enum_def
->
ReverseLookup
(
table
->
GetField
<
uint8_t
>
(
fd
.
value
.
offset
,
0
),
true
);
union_type
=
enum_val
?
&
enum_val
->
union_type
:
nullptr
;
// Track prev val for use with union types.
if
(
struct_def
.
fixed
)
{
prev_val
=
reinterpret_cast
<
const
uint8_t
*>
(
table
)
+
fd
.
value
.
offset
;
}
else
{
prev_val
=
table
->
GetAddressOf
(
fd
.
value
.
offset
);
}
}
}
...
...
tests/test.cpp
View file @
f9277e69
...
...
@@ -2410,6 +2410,7 @@ void UnionVectorTest() {
TestMovie
(
repacked_movie
);
// Generate text using mini-reflection.
auto
s
=
flatbuffers
::
FlatBufferToString
(
fbb
.
GetBufferPointer
(),
MovieTypeTable
());
TEST_EQ_STR
(
...
...
@@ -2451,6 +2452,39 @@ void UnionVectorTest() {
" ]
\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
);
TEST_EQ
(
parser2
.
Parse
(
"struct Bool { b: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