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
856ced5d
Commit
856ced5d
authored
Apr 27, 2015
by
Jie Luo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Field Presence: remove has methods for optional non-message fields in proto3
parent
7646eec9
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
115 additions
and
4 deletions
+115
-4
field_presence_test.proto
csharp/protos/google/protobuf/field_presence_test.proto
+24
-0
FieldPresenceTestProtoFile.cs
...ocolBuffers.Test/TestProtos/FieldPresenceTestProtoFile.cs
+0
-0
csharp_enum_field.cc
src/google/protobuf/compiler/csharp/csharp_enum_field.cc
+41
-2
csharp_helpers.h
src/google/protobuf/compiler/csharp/csharp_helpers.h
+4
-0
csharp_message.cc
src/google/protobuf/compiler/csharp/csharp_message.cc
+2
-0
csharp_primitive_field.cc
...google/protobuf/compiler/csharp/csharp_primitive_field.cc
+44
-2
No files found.
csharp/protos/google/protobuf/field_presence_test.proto
0 → 100644
View file @
856ced5d
syntax
=
"proto3"
;
package
field_presence_test
;
option
java_package
=
"com.google.protobuf"
;
option
java_outer_classname
=
"FieldPresenceTestProto"
;
option
java_generate_equals_and_hash
=
true
;
message
TestAllTypes
{
enum
NestedEnum
{
FOO
=
0
;
BAR
=
1
;
BAZ
=
2
;
}
message
NestedMessage
{
optional
int32
value
=
1
;
}
optional
int32
optional_int32
=
1
;
optional
string
optional_string
=
2
;
optional
bytes
optional_bytes
=
3
;
optional
NestedEnum
optional_nested_enum
=
4
;
optional
NestedMessage
optional_nested_message
=
5
;
}
csharp/src/ProtocolBuffers.Test/TestProtos/FieldPresenceTestProtoFile.cs
0 → 100644
View file @
856ced5d
This diff is collapsed.
Click to expand it.
src/google/protobuf/compiler/csharp/csharp_enum_field.cc
View file @
856ced5d
...
...
@@ -56,13 +56,17 @@ EnumFieldGenerator::~EnumFieldGenerator() {
}
void
EnumFieldGenerator
::
GenerateMembers
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"private bool has$0$;"
,
property_name
());
}
writer
->
WriteLine
(
"private $0$ $1$_ = $2$;"
,
type_name
(),
name
(),
default_value
());
AddDeprecatedFlag
(
writer
);
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"public bool Has$0$ {"
,
property_name
());
writer
->
WriteLine
(
" get { return has$0$; }"
,
property_name
());
writer
->
WriteLine
(
"}"
);
}
AddPublicMemberAttributes
(
writer
);
writer
->
WriteLine
(
"public $0$ $1$ {"
,
type_name
(),
property_name
());
writer
->
WriteLine
(
" get { return $0$_; }"
,
name
());
...
...
@@ -71,9 +75,11 @@ void EnumFieldGenerator::GenerateMembers(Writer* writer) {
void
EnumFieldGenerator
::
GenerateBuilderMembers
(
Writer
*
writer
)
{
AddDeprecatedFlag
(
writer
);
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"public bool Has$0$ {"
,
property_name
());
writer
->
WriteLine
(
" get { return result.has$0$; }"
,
property_name
());
writer
->
WriteLine
(
"}"
);
}
AddPublicMemberAttributes
(
writer
);
writer
->
WriteLine
(
"public $0$ $1$ {"
,
type_name
(),
property_name
());
writer
->
WriteLine
(
" get { return result.$0$; }"
,
property_name
());
...
...
@@ -83,21 +89,29 @@ void EnumFieldGenerator::GenerateBuilderMembers(Writer* writer) {
writer
->
WriteLine
(
"public Builder Set$0$($1$ value) {"
,
property_name
(),
type_name
());
writer
->
WriteLine
(
" PrepareBuilder();"
);
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
" result.has$0$ = true;"
,
property_name
());
}
writer
->
WriteLine
(
" result.$0$_ = value;"
,
name
());
writer
->
WriteLine
(
" return this;"
);
writer
->
WriteLine
(
"}"
);
AddDeprecatedFlag
(
writer
);
writer
->
WriteLine
(
"public Builder Clear$0$() {"
,
property_name
());
writer
->
WriteLine
(
" PrepareBuilder();"
);
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
" result.has$0$ = false;"
,
property_name
());
}
writer
->
WriteLine
(
" result.$0$_ = $1$;"
,
name
(),
default_value
());
writer
->
WriteLine
(
" return this;"
);
writer
->
WriteLine
(
"}"
);
}
void
EnumFieldGenerator
::
GenerateMergingCode
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (other.Has$0$) {"
,
property_name
());
}
else
{
writer
->
WriteLine
(
"if (other.$0$ != $1$) {"
,
property_name
(),
default_value
());
}
writer
->
WriteLine
(
" $0$ = other.$0$;"
,
property_name
());
writer
->
WriteLine
(
"}"
);
}
...
...
@@ -110,7 +124,9 @@ void EnumFieldGenerator::GenerateParsingCode(Writer* writer) {
writer
->
WriteLine
(
"object unknown;"
);
writer
->
WriteLine
(
"if(input.ReadEnum(ref result.$0$_, out unknown)) {"
,
name
());
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
" result.has$0$ = true;"
,
property_name
());
}
writer
->
WriteLine
(
"} else if(unknown is int) {"
);
if
(
!
use_lite_runtime
())
{
writer
->
WriteLine
(
" if (unknownFields == null) {"
);
// First unknown field - create builder now
...
...
@@ -125,7 +141,11 @@ void EnumFieldGenerator::GenerateParsingCode(Writer* writer) {
}
void
EnumFieldGenerator
::
GenerateSerializationCode
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (has$0$) {"
,
property_name
());
}
else
{
writer
->
WriteLine
(
"if ($0$ != $1$) {"
,
property_name
(),
default_value
());
}
writer
->
WriteLine
(
" output.WriteEnum($0$, field_names[$2$], (int) $1$, $1$);"
,
number
(),
property_name
(),
field_ordinal
());
...
...
@@ -133,7 +153,11 @@ void EnumFieldGenerator::GenerateSerializationCode(Writer* writer) {
}
void
EnumFieldGenerator
::
GenerateSerializedSizeCode
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (has$0$) {"
,
property_name
());
}
else
{
writer
->
WriteLine
(
"if ($0$ != $1$) {"
,
property_name
(),
default_value
());
}
writer
->
WriteLine
(
" size += pb::CodedOutputStream.ComputeEnumSize($0$, (int) $1$);"
,
number
(),
property_name
());
...
...
@@ -141,17 +165,32 @@ void EnumFieldGenerator::GenerateSerializedSizeCode(Writer* writer) {
}
void
EnumFieldGenerator
::
WriteHash
(
Writer
*
writer
)
{
writer
->
WriteLine
(
"if (has$0$) hash ^= $1$_.GetHashCode();"
,
property_name
(),
name
());
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (has$0$) {"
,
property_name
());
}
else
{
writer
->
WriteLine
(
"if ($0$ != $1$) {"
,
property_name
(),
default_value
());
}
writer
->
WriteLine
(
" hash ^= $1$_.GetHashCode();"
,
name
());
writer
->
WriteLine
(
"}"
);
}
void
EnumFieldGenerator
::
WriteEquals
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;"
,
property_name
(),
name
());
}
else
{
writer
->
WriteLine
(
"if (!$0$_.Equals(other.$0$_)) return false;"
,
name
());
}
}
void
EnumFieldGenerator
::
WriteToString
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"PrintField(
\"
$0$
\"
, has$1$, $2$_, writer);"
,
descriptor_
->
name
(),
property_name
(),
name
());
}
else
{
writer
->
WriteLine
(
"PrintField(
\"
$0$
\"
, $1$_, writer);"
,
descriptor_
->
name
(),
name
());
}
}
}
// namespace csharp
...
...
src/google/protobuf/compiler/csharp/csharp_helpers.h
View file @
856ced5d
...
...
@@ -101,6 +101,10 @@ FieldGeneratorBase* CreateFieldGenerator(const FieldDescriptor* descriptor, int
bool
HasRequiredFields
(
const
Descriptor
*
descriptor
);
inline
bool
SupportFieldPresence
(
const
FileDescriptor
*
file
)
{
return
file
->
syntax
()
!=
FileDescriptor
::
SYNTAX_PROTO3
;
}
}
// namespace csharp
}
// namespace compiler
}
// namespace protobuf
...
...
src/google/protobuf/compiler/csharp/csharp_message.cc
View file @
856ced5d
...
...
@@ -279,7 +279,9 @@ void MessageGenerator::Generate(Writer* writer) {
}
if
(
optimize_speed
())
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
GenerateIsInitialized
(
writer
);
}
GenerateMessageSerializationMethods
(
writer
);
}
if
(
use_lite_runtime
())
{
...
...
src/google/protobuf/compiler/csharp/csharp_primitive_field.cc
View file @
856ced5d
...
...
@@ -57,13 +57,17 @@ PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {
}
void
PrimitiveFieldGenerator
::
GenerateMembers
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"private bool has$0$;"
,
property_name
());
}
writer
->
WriteLine
(
"private $0$ $1$_$2$;"
,
type_name
(),
name
(),
has_default_value
()
?
" = "
+
default_value
()
:
""
);
AddDeprecatedFlag
(
writer
);
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"public bool Has$0$ {"
,
property_name
());
writer
->
WriteLine
(
" get { return has$0$; }"
,
property_name
());
writer
->
WriteLine
(
"}"
);
}
AddPublicMemberAttributes
(
writer
);
writer
->
WriteLine
(
"public $0$ $1$ {"
,
type_name
(),
property_name
());
writer
->
WriteLine
(
" get { return $0$_; }"
,
name
());
...
...
@@ -72,9 +76,11 @@ void PrimitiveFieldGenerator::GenerateMembers(Writer* writer) {
void
PrimitiveFieldGenerator
::
GenerateBuilderMembers
(
Writer
*
writer
)
{
AddDeprecatedFlag
(
writer
);
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"public bool Has$0$ {"
,
property_name
());
writer
->
WriteLine
(
" get { return result.has$0$; }"
,
property_name
());
writer
->
WriteLine
(
"}"
);
}
AddPublicMemberAttributes
(
writer
);
writer
->
WriteLine
(
"public $0$ $1$ {"
,
type_name
(),
property_name
());
writer
->
WriteLine
(
" get { return result.$0$; }"
,
property_name
());
...
...
@@ -85,21 +91,29 @@ void PrimitiveFieldGenerator::GenerateBuilderMembers(Writer* writer) {
type_name
());
AddNullCheck
(
writer
);
writer
->
WriteLine
(
" PrepareBuilder();"
);
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
" result.has$0$ = true;"
,
property_name
());
}
writer
->
WriteLine
(
" result.$0$_ = value;"
,
name
());
writer
->
WriteLine
(
" return this;"
);
writer
->
WriteLine
(
"}"
);
AddDeprecatedFlag
(
writer
);
writer
->
WriteLine
(
"public Builder Clear$0$() {"
,
property_name
());
writer
->
WriteLine
(
" PrepareBuilder();"
);
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
" result.has$0$ = false;"
,
property_name
());
}
writer
->
WriteLine
(
" result.$0$_ = $1$;"
,
name
(),
default_value
());
writer
->
WriteLine
(
" return this;"
);
writer
->
WriteLine
(
"}"
);
}
void
PrimitiveFieldGenerator
::
GenerateMergingCode
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (other.Has$0$) {"
,
property_name
());
}
else
{
writer
->
WriteLine
(
"if (other.$0$ != $1$) {"
,
property_name
(),
default_value
());
}
writer
->
WriteLine
(
" $0$ = other.$0$;"
,
property_name
());
writer
->
WriteLine
(
"}"
);
}
...
...
@@ -109,12 +123,21 @@ void PrimitiveFieldGenerator::GenerateBuildingCode(Writer* writer) {
}
void
PrimitiveFieldGenerator
::
GenerateParsingCode
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"result.has$0$ = input.Read$1$(ref result.$2$_);"
,
property_name
(),
capitalized_type_name
(),
name
());
}
else
{
writer
->
WriteLine
(
"input.Read$0$(ref result.$1$_);"
,
capitalized_type_name
(),
name
());
}
}
void
PrimitiveFieldGenerator
::
GenerateSerializationCode
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (has$0$) {"
,
property_name
());
}
else
{
writer
->
WriteLine
(
"if ($0$ != $1$) {"
,
property_name
(),
default_value
());
}
writer
->
WriteLine
(
" output.Write$0$($1$, field_names[$3$], $2$);"
,
capitalized_type_name
(),
number
(),
property_name
(),
field_ordinal
());
...
...
@@ -122,24 +145,43 @@ void PrimitiveFieldGenerator::GenerateSerializationCode(Writer* writer) {
}
void
PrimitiveFieldGenerator
::
GenerateSerializedSizeCode
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (has$0$) {"
,
property_name
());
}
else
{
writer
->
WriteLine
(
"if ($0$ != $1$) {"
,
property_name
(),
default_value
());
}
writer
->
WriteLine
(
" size += pb::CodedOutputStream.Compute$0$Size($1$, $2$);"
,
capitalized_type_name
(),
number
(),
property_name
());
writer
->
WriteLine
(
"}"
);
}
void
PrimitiveFieldGenerator
::
WriteHash
(
Writer
*
writer
)
{
writer
->
WriteLine
(
"if (has$0$) hash ^= $1$_.GetHashCode();"
,
property_name
(),
name
());
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (has$0$) {"
,
property_name
());
}
else
{
writer
->
WriteLine
(
"if ($0$ != $1$) {"
,
property_name
(),
default_value
());
}
writer
->
WriteLine
(
" hash ^= $1$_.GetHashCode();"
,
name
());
writer
->
WriteLine
(
"}"
);
}
void
PrimitiveFieldGenerator
::
WriteEquals
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"if (has$0$ != other.has$0$ || (has$0$ && !$1$_.Equals(other.$1$_))) return false;"
,
property_name
(),
name
());
}
else
{
writer
->
WriteLine
(
"if (!$0$_.Equals(other.$0$_)) return false;"
,
name
());
}
}
void
PrimitiveFieldGenerator
::
WriteToString
(
Writer
*
writer
)
{
if
(
SupportFieldPresence
(
descriptor_
->
file
()))
{
writer
->
WriteLine
(
"PrintField(
\"
$0$
\"
, has$1$, $2$_, writer);"
,
descriptor_
->
name
(),
property_name
(),
name
());
}
else
{
writer
->
WriteLine
(
"PrintField(
\"
$0$
\"
, $1$_, writer);"
,
descriptor_
->
name
(),
name
());
}
}
}
// namespace csharp
...
...
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