Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
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
protobuf
Commits
2ac8946e
Commit
2ac8946e
authored
Nov 04, 2019
by
Chris Bacon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow extra fields in wrapper messages, more tests.
parent
e305e56c
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
93 additions
and
13 deletions
+93
-13
WrappersTest.cs
...p/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs
+79
-2
CodedInputStream.cs
csharp/src/Google.Protobuf/CodedInputStream.cs
+0
-0
FieldCodec.cs
csharp/src/Google.Protobuf/FieldCodec.cs
+14
-11
No files found.
csharp/src/Google.Protobuf.Test/WellKnownTypes/WrappersTest.cs
View file @
2ac8946e
...
...
@@ -386,7 +386,7 @@ namespace Google.Protobuf.WellKnownTypes
}
[
Test
]
public
void
UnknownFieldInWrapper
()
public
void
UnknownFieldInWrapper
Int32FastPath
()
{
var
stream
=
new
MemoryStream
();
var
output
=
new
CodedOutputStream
(
stream
);
...
...
@@ -395,19 +395,96 @@ namespace Google.Protobuf.WellKnownTypes
var
valueTag
=
WireFormat
.
MakeTag
(
Int32Value
.
ValueFieldNumber
,
WireFormat
.
WireType
.
Varint
);
output
.
WriteTag
(
wrapperTag
);
output
.
WriteLength
(
4
);
// unknownTag + value 5 + valueType + value 6, each 1 byte
// Wrapper message is just long enough - 6 bytes - to use the wrapper fast-path.
output
.
WriteLength
(
6
);
// unknownTag + value 5 + valueType, each 1 byte, + value 65536, 3 bytes
output
.
WriteTag
(
unknownTag
);
output
.
WriteInt32
((
int
)
valueTag
);
// Sneakily "pretend" it's a tag when it's really a value
output
.
WriteTag
(
valueTag
);
output
.
WriteInt32
(
65536
);
output
.
Flush
();
Assert
.
AreEqual
(
8
,
stream
.
Length
);
// tag (1 byte) + length (1 byte) + message (6 bytes)
stream
.
Position
=
0
;
var
message
=
TestWellKnownTypes
.
Parser
.
ParseFrom
(
stream
);
Assert
.
AreEqual
(
65536
,
message
.
Int32Field
);
}
[
Test
]
public
void
UnknownFieldInWrapperInt32SlowPath
()
{
var
stream
=
new
MemoryStream
();
var
output
=
new
CodedOutputStream
(
stream
);
var
wrapperTag
=
WireFormat
.
MakeTag
(
TestWellKnownTypes
.
Int32FieldFieldNumber
,
WireFormat
.
WireType
.
LengthDelimited
);
var
unknownTag
=
WireFormat
.
MakeTag
(
15
,
WireFormat
.
WireType
.
Varint
);
var
valueTag
=
WireFormat
.
MakeTag
(
Int32Value
.
ValueFieldNumber
,
WireFormat
.
WireType
.
Varint
);
output
.
WriteTag
(
wrapperTag
);
// Wrapper message is too short to be used on the wrapper fast-path.
output
.
WriteLength
(
4
);
// unknownTag + value 5 + valueType + value 6, each 1 byte
output
.
WriteTag
(
unknownTag
);
output
.
WriteInt32
((
int
)
valueTag
);
// Sneakily "pretend" it's a tag when it's really a value
output
.
WriteTag
(
valueTag
);
output
.
WriteInt32
(
6
);
output
.
Flush
();
Assert
.
Less
(
stream
.
Length
,
8
);
// tag (1 byte) + length (1 byte) + message
stream
.
Position
=
0
;
var
message
=
TestWellKnownTypes
.
Parser
.
ParseFrom
(
stream
);
Assert
.
AreEqual
(
6
,
message
.
Int32Field
);
}
[
Test
]
public
void
UnknownFieldInWrapperInt64FastPath
()
{
var
stream
=
new
MemoryStream
();
var
output
=
new
CodedOutputStream
(
stream
);
var
wrapperTag
=
WireFormat
.
MakeTag
(
TestWellKnownTypes
.
Int64FieldFieldNumber
,
WireFormat
.
WireType
.
LengthDelimited
);
var
unknownTag
=
WireFormat
.
MakeTag
(
15
,
WireFormat
.
WireType
.
Varint
);
var
valueTag
=
WireFormat
.
MakeTag
(
Int64Value
.
ValueFieldNumber
,
WireFormat
.
WireType
.
Varint
);
output
.
WriteTag
(
wrapperTag
);
// Wrapper message is just long enough - 10 bytes - to use the wrapper fast-path.
output
.
WriteLength
(
11
);
// unknownTag + value 5 + valueType, each 1 byte, + value 0xfffffffffffff, 8 bytes
output
.
WriteTag
(
unknownTag
);
output
.
WriteInt64
((
int
)
valueTag
);
// Sneakily "pretend" it's a tag when it's really a value
output
.
WriteTag
(
valueTag
);
output
.
WriteInt64
(
0xfffffffffffffL
);
output
.
Flush
();
Assert
.
AreEqual
(
13
,
stream
.
Length
);
// tag (1 byte) + length (1 byte) + message (11 bytes)
stream
.
Position
=
0
;
var
message
=
TestWellKnownTypes
.
Parser
.
ParseFrom
(
stream
);
Assert
.
AreEqual
(
0xfffffffffffffL
,
message
.
Int64Field
);
}
[
Test
]
public
void
UnknownFieldInWrapperInt64SlowPath
()
{
var
stream
=
new
MemoryStream
();
var
output
=
new
CodedOutputStream
(
stream
);
var
wrapperTag
=
WireFormat
.
MakeTag
(
TestWellKnownTypes
.
Int64FieldFieldNumber
,
WireFormat
.
WireType
.
LengthDelimited
);
var
unknownTag
=
WireFormat
.
MakeTag
(
15
,
WireFormat
.
WireType
.
Varint
);
var
valueTag
=
WireFormat
.
MakeTag
(
Int64Value
.
ValueFieldNumber
,
WireFormat
.
WireType
.
Varint
);
output
.
WriteTag
(
wrapperTag
);
// Wrapper message is too short to be used on the wrapper fast-path.
output
.
WriteLength
(
4
);
// unknownTag + value 5 + valueType + value 6, each 1 byte
output
.
WriteTag
(
unknownTag
);
output
.
WriteInt64
((
int
)
valueTag
);
// Sneakily "pretend" it's a tag when it's really a value
output
.
WriteTag
(
valueTag
);
output
.
WriteInt64
(
6
);
output
.
Flush
();
Assert
.
Less
(
stream
.
Length
,
12
);
// tag (1 byte) + length (1 byte) + message
stream
.
Position
=
0
;
var
message
=
TestWellKnownTypes
.
Parser
.
ParseFrom
(
stream
);
Assert
.
AreEqual
(
6L
,
message
.
Int64Field
);
}
[
Test
]
public
void
ClearWithReflection
()
{
...
...
csharp/src/Google.Protobuf/CodedInputStream.cs
View file @
2ac8946e
This diff is collapsed.
Click to expand it.
csharp/src/Google.Protobuf/FieldCodec.cs
View file @
2ac8946e
...
...
@@ -539,18 +539,21 @@ namespace Google.Protobuf
{
typeof
(
ByteString
),
ForBytes
(
WireFormat
.
MakeTag
(
WrappersReflection
.
WrapperValueFieldNumber
,
WireFormat
.
WireType
.
LengthDelimited
))
}
};
private
static
readonly
Dictionary
<
System
.
Type
,
Func
<
object
>>
Readers
=
new
Dictionary
<
System
.
Type
,
Func
<
object
>
>
private
static
readonly
Dictionary
<
System
.
Type
,
object
>
Readers
=
new
Dictionary
<
System
.
Type
,
object
>
{
// TODO: Provide more optimized readers.
{
typeof
(
bool
),
null
},
{
typeof
(
int
),
null
},
{
typeof
(
long
),
()
=>
(
Func
<
CodedInputStream
,
long
?>)
CodedInputStream
.
ReadInt64Wrapper
},
{
typeof
(
uint
),
null
},
{
typeof
(
ulong
),
null
},
{
typeof
(
float
),
null
},
{
typeof
(
double
),
()
=>
BitConverter
.
IsLittleEndian
?
{
typeof
(
bool
),
(
Func
<
CodedInputStream
,
bool
?>)
CodedInputStream
.
ReadBoolWrapper
},
{
typeof
(
int
),
(
Func
<
CodedInputStream
,
int
?>)
CodedInputStream
.
ReadInt32Wrapper
},
{
typeof
(
long
),
(
Func
<
CodedInputStream
,
long
?>)
CodedInputStream
.
ReadInt64Wrapper
},
{
typeof
(
uint
),
(
Func
<
CodedInputStream
,
uint
?>)
CodedInputStream
.
ReadUInt32Wrapper
},
{
typeof
(
ulong
),
(
Func
<
CodedInputStream
,
ulong
?>)
CodedInputStream
.
ReadUInt64Wrapper
},
{
typeof
(
float
),
BitConverter
.
IsLittleEndian
?
(
Func
<
CodedInputStream
,
float
?>)
CodedInputStream
.
ReadFloatWrapperLittleEndian
:
(
Func
<
CodedInputStream
,
float
?>)
CodedInputStream
.
ReadFloatWrapperSlow
},
{
typeof
(
double
),
BitConverter
.
IsLittleEndian
?
(
Func
<
CodedInputStream
,
double
?>)
CodedInputStream
.
ReadDoubleWrapperLittleEndian
:
(
Func
<
CodedInputStream
,
double
?>)
CodedInputStream
.
ReadDoubleWrapperBigEndian
},
(
Func
<
CodedInputStream
,
double
?>)
CodedInputStream
.
ReadDoubleWrapperSlow
},
// `string` and `ByteString` less performance-sensitive. Do not implement for now.
{
typeof
(
string
),
null
},
{
typeof
(
ByteString
),
null
},
};
...
...
@@ -571,7 +574,7 @@ namespace Google.Protobuf
internal
static
Func
<
CodedInputStream
,
T
?>
GetReader
<
T
>()
where
T
:
struct
{
Func
<
object
>
value
;
object
value
;
if
(!
Readers
.
TryGetValue
(
typeof
(
T
),
out
value
))
{
throw
new
InvalidOperationException
(
"Invalid type argument requested for wrapper reader: "
+
typeof
(
T
));
...
...
@@ -583,7 +586,7 @@ namespace Google.Protobuf
return
input
=>
Read
<
T
>(
input
,
nestedCoded
);
}
// Return optimized read for the wrapper type.
return
(
Func
<
CodedInputStream
,
T
?>)
value
()
;
return
(
Func
<
CodedInputStream
,
T
?>)
value
;
}
internal
static
T
Read
<
T
>(
CodedInputStream
input
,
FieldCodec
<
T
>
codec
)
...
...
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