Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
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
capnproto
Commits
608d7ed7
Commit
608d7ed7
authored
Aug 15, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support parsing values for structs with unnamed unions.
parent
372612cd
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
227 additions
and
140 deletions
+227
-140
grammar.capnp
c++/src/capnp/compiler/grammar.capnp
+7
-4
grammar.capnp.c++
c++/src/capnp/compiler/grammar.capnp.c++
+78
-54
grammar.capnp.h
c++/src/capnp/compiler/grammar.capnp.h
+0
-0
node-translator.c++
c++/src/capnp/compiler/node-translator.c++
+39
-10
parser.c++
c++/src/capnp/compiler/parser.c++
+39
-29
encoding-test.c++
c++/src/capnp/encoding-test.c++
+48
-32
test.capnp
c++/src/capnp/test.capnp
+16
-11
No files found.
c++/src/capnp/compiler/grammar.capnp
View file @
608d7ed7
...
...
@@ -101,16 +101,19 @@ struct ValueExpression {
name @6 :DeclName;
list @7 :List(ValueExpression);
structValue @8 :List(FieldAssignment);
unionValue @9 :FieldAssignment;
}
struct FieldAssignment {
fieldName @0 :LocatedText;
value @1 :ValueExpression;
union {
notUnion @1 :Void;
union @2 :LocatedText; # Name of union member being assigned.
}
value @3 :ValueExpression;
}
startByte @
10
:UInt32;
endByte @1
1
:UInt32;
startByte @
9
:UInt32;
endByte @1
0
:UInt32;
}
struct Declaration {
...
...
c++/src/capnp/compiler/grammar.capnp.c++
View file @
608d7ed7
...
...
@@ -456,7 +456,7 @@ const ::capnp::_::RawSchema s_8751968764a2e298 = {
0x8751968764a2e298
,
b_8751968764a2e298
.
words
,
78
,
d_8751968764a2e298
,
m_8751968764a2e298
,
2
,
4
,
nullptr
,
nullptr
};
static
const
::
capnp
::
_
::
AlignedData
<
1
95
>
b_9ca8b2acb16fc545
=
{
static
const
::
capnp
::
_
::
AlignedData
<
1
80
>
b_9ca8b2acb16fc545
=
{
{
0
,
0
,
0
,
0
,
3
,
0
,
4
,
0
,
69
,
197
,
111
,
177
,
172
,
178
,
168
,
156
,
198
,
195
,
187
,
220
,
104
,
225
,
107
,
197
,
...
...
@@ -483,54 +483,50 @@ static const ::capnp::_::AlignedData<195> b_9ca8b2acb16fc545 = {
41
,
0
,
0
,
0
,
42
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
36
,
0
,
0
,
0
,
1
,
0
,
1
,
0
,
10
,
0
,
1
,
0
,
0
,
0
,
0
,
0
,
7
3
,
2
,
0
,
0
,
82
,
0
,
0
,
0
,
9
,
0
,
1
,
0
,
0
,
0
,
0
,
0
,
1
3
,
2
,
0
,
0
,
82
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
7
2
,
2
,
0
,
0
,
1
,
0
,
2
,
0
,
1
1
,
0
,
2
,
0
,
0
,
0
,
0
,
0
,
10
1
,
2
,
0
,
0
,
66
,
0
,
0
,
0
,
1
2
,
2
,
0
,
0
,
1
,
0
,
2
,
0
,
1
0
,
0
,
2
,
0
,
0
,
0
,
0
,
0
,
4
1
,
2
,
0
,
0
,
66
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
9
6
,
2
,
0
,
0
,
1
,
0
,
2
,
0
,
3
6
,
2
,
0
,
0
,
1
,
0
,
2
,
0
,
98
,
111
,
100
,
121
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
0
,
0
,
0
,
39
,
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0
,
0
,
0
,
117
,
110
,
105
,
111
,
110
,
86
,
97
,
108
,
117
,
101
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
4
,
0
,
0
,
0
,
2
,
0
,
1
,
0
,
12
,
0
,
0
,
0
,
2
,
0
,
1
,
0
,
16
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
14
,
186
,
127
,
178
,
248
,
124
,
181
,
182
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
16
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
115
,
116
,
97
,
114
,
116
,
66
,
121
,
116
,
101
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
...
...
@@ -669,14 +654,13 @@ static const ::capnp::_::RawSchema::MemberInfo m_9ca8b2acb16fc545[] = {
{
1
,
1
},
{
1
,
4
},
{
1
,
7
},
{
1
,
8
},
{
1
,
0
},
};
const
::
capnp
::
_
::
RawSchema
s_9ca8b2acb16fc545
=
{
0x9ca8b2acb16fc545
,
b_9ca8b2acb16fc545
.
words
,
1
95
,
d_9ca8b2acb16fc545
,
m_9ca8b2acb16fc545
,
3
,
1
2
,
nullptr
,
nullptr
0x9ca8b2acb16fc545
,
b_9ca8b2acb16fc545
.
words
,
1
80
,
d_9ca8b2acb16fc545
,
m_9ca8b2acb16fc545
,
3
,
1
1
,
nullptr
,
nullptr
};
static
const
::
capnp
::
_
::
AlignedData
<
49
>
b_b6b57cf8b27fba0e
=
{
static
const
::
capnp
::
_
::
AlignedData
<
86
>
b_b6b57cf8b27fba0e
=
{
{
0
,
0
,
0
,
0
,
3
,
0
,
4
,
0
,
14
,
186
,
127
,
178
,
248
,
124
,
181
,
182
,
69
,
197
,
111
,
177
,
172
,
178
,
168
,
156
,
...
...
@@ -694,20 +678,57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0
,
0
,
...
...
@@ -717,7 +738,7 @@ static const ::capnp::_::AlignedData<49> b_b6b57cf8b27fba0e = {
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
118
,
97
,
108
,
117
,
101
,
0
,
0
,
0
,
1
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
2
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
4
,
0
,
0
,
0
,
2
,
0
,
1
,
0
,
12
,
0
,
0
,
0
,
2
,
0
,
1
,
0
,
16
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
...
...
@@ -732,12 +753,15 @@ static const ::capnp::_::RawSchema* const d_b6b57cf8b27fba0e[] = {
&
s_e75816b56529d464
,
};
static
const
::
capnp
::
_
::
RawSchema
::
MemberInfo
m_b6b57cf8b27fba0e
[]
=
{
{
0
,
0
},
{
0
,
1
},
{
0
,
0
},
{
0
,
3
},
{
0
,
4
},
{
0
,
2
},
};
const
::
capnp
::
_
::
RawSchema
s_b6b57cf8b27fba0e
=
{
0xb6b57cf8b27fba0e
,
b_b6b57cf8b27fba0e
.
words
,
49
,
d_b6b57cf8b27fba0e
,
m_b6b57cf8b27fba0e
,
2
,
2
,
nullptr
,
nullptr
0xb6b57cf8b27fba0e
,
b_b6b57cf8b27fba0e
.
words
,
86
,
d_b6b57cf8b27fba0e
,
m_b6b57cf8b27fba0e
,
2
,
5
,
nullptr
,
nullptr
};
static
const
::
capnp
::
_
::
AlignedData
<
664
>
b_96efe787c17e83bb
=
{
{
0
,
0
,
0
,
0
,
3
,
0
,
4
,
0
,
...
...
c++/src/capnp/compiler/grammar.capnp.h
View file @
608d7ed7
This diff is collapsed.
Click to expand it.
c++/src/capnp/compiler/node-translator.c++
View file @
608d7ed7
...
...
@@ -1647,27 +1647,56 @@ void NodeTranslator::compileValueInner(
auto
dstSchema
=
dstStruct
.
getSchema
();
for
(
auto
assignment
:
srcStruct
)
{
auto
fieldName
=
assignment
.
getFieldName
();
switch
(
assignment
.
which
())
{
case
ValueExpression
:
:
FieldAssignment
::
NOT_UNION
:
KJ_IF_MAYBE
(
member
,
dstSchema
.
findMemberByName
(
fieldName
.
getValue
()))
{
DynamicSlot
slot
(
dstStruct
,
*
member
);
compileValue
(
assignment
.
getValue
(),
slot
,
isBootstrap
);
}
else
{
errorReporter
.
addErrorOn
(
fieldName
,
kj
::
str
(
"Value
has no field named '"
,
fieldName
.
getValue
(),
"'."
));
"Struct
has no field named '"
,
fieldName
.
getValue
(),
"'."
));
}
break
;
case
ValueExpression
:
:
FieldAssignment
::
UNION
:
{
StructSchema
::
Union
unionMember
;
if
(
fieldName
.
getValue
().
size
()
==
0
)
{
// Unnamed.
KJ_IF_MAYBE
(
unnamedUnion
,
dstSchema
.
getUnnamedUnion
())
{
unionMember
=
*
unnamedUnion
;
}
else
{
errorReporter
.
addErrorOn
(
assignment
.
getUnion
(),
kj
::
str
(
"Struct type '"
,
dstSchema
.
getProto
().
getDisplayName
(),
"' does not contain an unnamed union."
));
break
;
}
}
else
{
KJ_IF_MAYBE
(
member
,
dstSchema
.
findMemberByName
(
fieldName
.
getValue
()))
{
if
(
member
->
getProto
().
getBody
().
which
()
==
schema
::
StructNode
::
Member
::
Body
::
UNION_MEMBER
)
{
unionMember
=
member
->
asUnion
();
}
else
{
errorReporter
.
addErrorOn
(
fieldName
,
kj
::
str
(
"'"
,
fieldName
.
getValue
(),
"' is not a union."
));
break
;
}
case
ValueExpression
:
:
Body
::
UNION_VALUE
:
{
auto
srcUnion
=
src
.
getBody
().
getUnionValue
();
auto
dstUnion
=
dst
.
getUnion
();
auto
fieldName
=
srcUnion
.
getFieldName
();
KJ_IF_MAYBE
(
member
,
dstUnion
.
getSchema
().
findMemberByName
(
fieldName
.
getValue
()))
{
DynamicSlot
slot
(
dstUnion
,
*
member
);
compileValue
(
srcUnion
.
getValue
(),
slot
,
isBootstrap
);
}
else
{
errorReporter
.
addErrorOn
(
fieldName
,
kj
::
str
(
"Union has no field named '"
,
fieldName
.
getValue
(),
"'."
));
"Struct has no field named '"
,
fieldName
.
getValue
(),
"'."
));
break
;
}
}
KJ_IF_MAYBE
(
member
,
unionMember
.
findMemberByName
(
assignment
.
getUnion
().
getValue
()))
{
DynamicSlot
slot
(
dstStruct
,
*
member
);
compileValue
(
assignment
.
getValue
(),
slot
,
isBootstrap
);
}
else
{
errorReporter
.
addErrorOn
(
assignment
.
getUnion
(),
kj
::
str
(
"Union has no member named '"
,
assignment
.
getUnion
().
getValue
(),
"'."
));
}
}
}
}
break
;
}
...
...
c++/src/capnp/compiler/parser.c++
View file @
608d7ed7
...
...
@@ -372,19 +372,40 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
return
result
;
}));
auto
&
fieldAssignment
=
arena
.
copy
(
p
::
transform
(
p
::
sequence
(
p
::
optional
(
p
::
sequence
(
identifier
,
op
(
"="
))),
parsers
.
valueExpression
),
[
this
](
kj
::
Maybe
<
Located
<
Text
::
Reader
>>&&
fieldName
,
Orphan
<
ValueExpression
>&&
value
)
// Matches "value" or "unionName(value)".
auto
&
fieldValue
=
arena
.
copy
(
p
::
oneOf
(
p
::
transform
(
p
::
sequence
(
identifier
,
parsers
.
parenthesizedValueExpression
),
[
this
](
Located
<
Text
::
Reader
>&&
name
,
Orphan
<
ValueExpression
>&&
value
)
->
Orphan
<
ValueExpression
::
FieldAssignment
>
{
auto
result
=
orphanage
.
newOrphan
<
ValueExpression
::
FieldAssignment
>
();
auto
builder
=
result
.
get
();
name
.
copyTo
(
builder
.
initUnion
());
builder
.
adoptValue
(
kj
::
mv
(
value
));
return
result
;
}),
p
::
transform
(
parsers
.
valueExpression
,
[
this
](
Orphan
<
ValueExpression
>&&
value
)
->
Orphan
<
ValueExpression
::
FieldAssignment
>
{
auto
result
=
orphanage
.
newOrphan
<
ValueExpression
::
FieldAssignment
>
();
auto
builder
=
result
.
get
();
// The field name is optional for now because this makes it easier for us to parse unions
// later. We'll produce an error later if the name is missing when required.
builder
.
setNotUnion
();
builder
.
adoptValue
(
kj
::
mv
(
value
));
return
result
;
})));
// Parser for a "name = value" pair. Also matches "name = unionMember(value)",
// "unionMember(value)" (unnamed union), and just "value" (which is not actually a valid field
// assigment, but simplifies the parser for parenthesizedValueExpression).
auto
&
fieldAssignment
=
arena
.
copy
(
p
::
transform
(
p
::
sequence
(
p
::
optional
(
p
::
sequence
(
identifier
,
op
(
"="
))),
fieldValue
),
[
this
](
kj
::
Maybe
<
Located
<
Text
::
Reader
>>&&
fieldName
,
Orphan
<
ValueExpression
::
FieldAssignment
>&&
assignment
)
->
Orphan
<
ValueExpression
::
FieldAssignment
>
{
auto
builder
=
assignment
.
get
();
KJ_IF_MAYBE
(
fn
,
fieldName
)
{
fn
->
copyTo
(
builder
.
initFieldName
());
}
builder
.
adoptValue
(
kj
::
mv
(
value
));
return
result
;
return
kj
::
mv
(
assignment
);
}));
parsers
.
parenthesizedValueExpression
=
arena
.
copy
(
p
::
transform
(
...
...
@@ -393,8 +414,10 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
->
Orphan
<
ValueExpression
>
{
if
(
value
.
value
.
size
()
==
1
)
{
KJ_IF_MAYBE
(
firstVal
,
value
.
value
[
0
])
{
if
(
!
firstVal
->
get
().
hasFieldName
())
{
// There is only one value and it isn't an assignment, therefore the value is
auto
reader
=
firstVal
->
getReader
();
if
(
reader
.
getFieldName
().
getValue
().
size
()
==
0
&&
reader
.
which
()
==
ValueExpression
::
FieldAssignment
::
NOT_UNION
)
{
// There is only one value and it isn't an assignment or union, therefore the value is
// not a struct.
return
firstVal
->
get
().
disownValue
();
}
...
...
@@ -418,12 +441,12 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
auto
structBuilder
=
builder
.
getBody
().
initStructValue
(
value
.
value
.
size
());
for
(
uint
i
=
0
;
i
<
value
.
value
.
size
();
i
++
)
{
KJ_IF_MAYBE
(
field
,
value
.
value
[
i
])
{
if
(
field
->
get
().
hasFieldName
())
{
auto
reader
=
field
->
getReader
();
if
(
reader
.
getFieldName
().
getValue
().
size
()
>
0
||
reader
.
which
()
==
ValueExpression
::
FieldAssignment
::
UNION
)
{
structBuilder
.
adoptWithCaveats
(
i
,
kj
::
mv
(
*
field
));
}
else
{
auto
fieldValue
=
field
->
get
().
getValue
();
errorReporter
.
addError
(
fieldValue
.
getStartByte
(),
fieldValue
.
getEndByte
(),
"Missing field name."
);
errorReporter
.
addErrorOn
(
reader
.
getValue
(),
"Missing field name."
);
}
}
}
...
...
@@ -479,21 +502,6 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
auto
builder
=
result
.
get
();
builder
.
getBody
().
setString
(
value
.
value
);
value
.
copyLocationTo
(
builder
);
return
result
;
}),
p
::
transform
(
p
::
sequence
(
identifier
,
parsers
.
parenthesizedValueExpression
),
[
this
](
Located
<
Text
::
Reader
>&&
fieldName
,
Orphan
<
ValueExpression
>&&
value
)
->
Orphan
<
ValueExpression
>
{
auto
result
=
orphanage
.
newOrphan
<
ValueExpression
>
();
auto
builder
=
result
.
get
();
builder
.
setStartByte
(
fieldName
.
startByte
);
builder
.
setEndByte
(
value
.
get
().
getEndByte
());
auto
unionBuilder
=
builder
.
getBody
().
initUnionValue
();
fieldName
.
copyTo
(
unionBuilder
.
initFieldName
());
unionBuilder
.
adoptValue
(
kj
::
mv
(
value
));
return
result
;
}),
p
::
transformWithLocation
(
parsers
.
declName
,
...
...
@@ -527,7 +535,9 @@ CapnpParser::CapnpParser(Orphanage orphanageParam, const ErrorReporter& errorRep
auto
structBuilder
=
builder
.
getBody
().
initStructValue
(
value
.
value
.
size
());
for
(
uint
i
=
0
;
i
<
value
.
value
.
size
();
i
++
)
{
KJ_IF_MAYBE
(
field
,
value
.
value
[
i
])
{
if
(
field
->
get
().
hasFieldName
())
{
auto
reader
=
field
->
get
();
if
(
reader
.
getFieldName
().
getValue
().
size
()
>
0
||
reader
.
which
()
==
ValueExpression
::
FieldAssignment
::
UNION
)
{
structBuilder
.
adoptWithCaveats
(
i
,
kj
::
mv
(
*
field
));
}
else
{
auto
fieldValue
=
field
->
get
().
getValue
();
...
...
c++/src/capnp/encoding-test.c++
View file @
608d7ed7
...
...
@@ -367,35 +367,6 @@ TEST(Encoding, UnionLayout) {
#undef INIT_UNION
}
TEST
(
Encoding
,
UnionDefault
)
{
MallocMessageBuilder
builder
;
TestUnionDefaults
::
Reader
reader
=
builder
.
getRoot
<
TestUnionDefaults
>
().
asReader
();
{
auto
field
=
reader
.
getS16s8s64s8Set
();
EXPECT_EQ
(
TestUnion
::
Union0
::
U0F0S16
,
field
.
getUnion0
().
which
());
EXPECT_EQ
(
TestUnion
::
Union1
::
U1F0S8
,
field
.
getUnion1
().
which
());
EXPECT_EQ
(
TestUnion
::
Union2
::
U2F0S64
,
field
.
getUnion2
().
which
());
EXPECT_EQ
(
TestUnion
::
Union3
::
U3F0S8
,
field
.
getUnion3
().
which
());
EXPECT_EQ
(
321
,
field
.
getUnion0
().
getU0f0s16
());
EXPECT_EQ
(
123
,
field
.
getUnion1
().
getU1f0s8
());
EXPECT_EQ
(
12345678901234567ll
,
field
.
getUnion2
().
getU2f0s64
());
EXPECT_EQ
(
55
,
field
.
getUnion3
().
getU3f0s8
());
}
{
auto
field
=
reader
.
getS0sps1s32Set
();
EXPECT_EQ
(
TestUnion
::
Union0
::
U0F1S0
,
field
.
getUnion0
().
which
());
EXPECT_EQ
(
TestUnion
::
Union1
::
U1F0SP
,
field
.
getUnion1
().
which
());
EXPECT_EQ
(
TestUnion
::
Union2
::
U2F0S1
,
field
.
getUnion2
().
which
());
EXPECT_EQ
(
TestUnion
::
Union3
::
U3F0S32
,
field
.
getUnion3
().
which
());
EXPECT_EQ
(
Void
::
VOID
,
field
.
getUnion0
().
getU0f1s0
());
EXPECT_EQ
(
"foo"
,
field
.
getUnion1
().
getU1f0sp
());
EXPECT_EQ
(
true
,
field
.
getUnion2
().
getU2f0s1
());
EXPECT_EQ
(
12345678
,
field
.
getUnion3
().
getU3f0s32
());
}
}
TEST
(
Encoding
,
UnnamedUnion
)
{
MallocMessageBuilder
builder
;
auto
root
=
builder
.
initRoot
<
test
::
TestUnnamedUnion
>
();
...
...
@@ -419,10 +390,10 @@ TEST(Encoding, UnnamedUnion) {
StructSchema
schema
=
Schema
::
from
<
test
::
TestUnnamedUnion
>
();
KJ_IF_MAYBE
(
u
,
schema
.
getUnnamedUnion
())
{
// The discriminant is allocated
between allocating the first and second members
.
EXPECT_EQ
(
1
,
u
->
getProto
().
getBody
().
getUnionMember
().
getDiscriminantOffset
());
// The discriminant is allocated
just before allocating "bar"
.
EXPECT_EQ
(
2
,
u
->
getProto
().
getBody
().
getUnionMember
().
getDiscriminantOffset
());
EXPECT_EQ
(
0
,
u
->
getMemberByName
(
"foo"
).
getProto
().
getBody
().
getFieldMember
().
getOffset
());
EXPECT_EQ
(
1
,
u
->
getMemberByName
(
"bar"
).
getProto
().
getBody
().
getFieldMember
().
getOffset
());
EXPECT_EQ
(
2
,
u
->
getMemberByName
(
"bar"
).
getProto
().
getBody
().
getFieldMember
().
getOffset
());
// The union receives the ordinal of its first member, since it does not explicitly declare one.
EXPECT_EQ
(
1
,
u
->
getProto
().
getOrdinal
());
...
...
@@ -431,6 +402,51 @@ TEST(Encoding, UnnamedUnion) {
}
}
TEST
(
Encoding
,
UnionDefault
)
{
MallocMessageBuilder
builder
;
TestUnionDefaults
::
Reader
reader
=
builder
.
getRoot
<
TestUnionDefaults
>
().
asReader
();
{
auto
field
=
reader
.
getS16s8s64s8Set
();
EXPECT_EQ
(
TestUnion
::
Union0
::
U0F0S16
,
field
.
getUnion0
().
which
());
EXPECT_EQ
(
TestUnion
::
Union1
::
U1F0S8
,
field
.
getUnion1
().
which
());
EXPECT_EQ
(
TestUnion
::
Union2
::
U2F0S64
,
field
.
getUnion2
().
which
());
EXPECT_EQ
(
TestUnion
::
Union3
::
U3F0S8
,
field
.
getUnion3
().
which
());
EXPECT_EQ
(
321
,
field
.
getUnion0
().
getU0f0s16
());
EXPECT_EQ
(
123
,
field
.
getUnion1
().
getU1f0s8
());
EXPECT_EQ
(
12345678901234567ll
,
field
.
getUnion2
().
getU2f0s64
());
EXPECT_EQ
(
55
,
field
.
getUnion3
().
getU3f0s8
());
}
{
auto
field
=
reader
.
getS0sps1s32Set
();
EXPECT_EQ
(
TestUnion
::
Union0
::
U0F1S0
,
field
.
getUnion0
().
which
());
EXPECT_EQ
(
TestUnion
::
Union1
::
U1F0SP
,
field
.
getUnion1
().
which
());
EXPECT_EQ
(
TestUnion
::
Union2
::
U2F0S1
,
field
.
getUnion2
().
which
());
EXPECT_EQ
(
TestUnion
::
Union3
::
U3F0S32
,
field
.
getUnion3
().
which
());
EXPECT_EQ
(
Void
::
VOID
,
field
.
getUnion0
().
getU0f1s0
());
EXPECT_EQ
(
"foo"
,
field
.
getUnion1
().
getU1f0sp
());
EXPECT_EQ
(
true
,
field
.
getUnion2
().
getU2f0s1
());
EXPECT_EQ
(
12345678
,
field
.
getUnion3
().
getU3f0s32
());
}
{
auto
field
=
reader
.
getUnnamed1
();
EXPECT_EQ
(
test
::
TestUnnamedUnion
::
FOO
,
field
.
which
());
EXPECT_EQ
(
123
,
field
.
getFoo
());
EXPECT_FALSE
(
field
.
hasBefore
());
EXPECT_FALSE
(
field
.
hasAfter
());
}
{
auto
field
=
reader
.
getUnnamed2
();
EXPECT_EQ
(
test
::
TestUnnamedUnion
::
BAR
,
field
.
which
());
EXPECT_EQ
(
321
,
field
.
getBar
());
EXPECT_EQ
(
"foo"
,
field
.
getBefore
());
EXPECT_EQ
(
"bar"
,
field
.
getAfter
());
}
}
// =======================================================================================
TEST
(
Encoding
,
ListDefaults
)
{
...
...
c++/src/capnp/test.capnp
View file @
608d7ed7
...
...
@@ -256,6 +256,19 @@ struct TestUnion {
byte0 @49: UInt8;
}
struct TestUnnamedUnion {
before @0 :Text;
union {
foo @1 :UInt16;
bar @3 :UInt32;
}
middle @2 :UInt16;
after @4 :Text;
}
struct TestUnionDefaults {
s16s8s64s8Set @0 :TestUnion =
(union0 = u0f0s16(321), union1 = u1f0s8(123), union2 = u2f0s64(12345678901234567),
...
...
@@ -263,6 +276,9 @@ struct TestUnionDefaults {
s0sps1s32Set @1 :TestUnion =
(union0 = u0f1s0(void), union1 = u1f0sp("foo"), union2 = u2f0s1(true),
union3 = u3f0s32(12345678));
unnamed1 @2 :TestUnnamedUnion = (foo(123));
unnamed2 @3 :TestUnnamedUnion = (bar(321), before = "foo", after = "bar");
}
struct TestNestedTypes {
...
...
@@ -383,14 +399,3 @@ struct TestStructUnion {
object @2 :TestObject;
}
}
struct TestUnnamedUnion {
before @0 :Text;
union {
foo @1 :UInt16;
bar @2 :UInt32;
}
after @3 :Text;
}
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