Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Fixing cloning issue #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.protoc
plugin/protoc-gen-zsharp*

Expand Down
5 changes: 5 additions & 0 deletions plugin/src/lib/csharp_field_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ void FieldGeneratorBase::GenerateCodecCode(io::Printer* printer) {
// Could fail if we get called here though...
}

void FieldGeneratorBase::GenerateConstructor(io::Printer* printer, bool isEventSourced) {
// No-op: expect this to be overridden by appropriate types.
// Could fail if we get called here though...
}

void FieldGeneratorBase::AddDeprecatedFlag(io::Printer* printer) {
if (descriptor_->options().deprecated()) {
printer->Print("[global::System.ObsoleteAttribute]\n");
Expand Down
1 change: 1 addition & 0 deletions plugin/src/lib/csharp_field_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class FieldGeneratorBase : public SourceGeneratorBase {
virtual void GenerateCodecCode(io::Printer* printer);

/// The following code is Copyright 2018, Zynga
virtual void GenerateConstructor(io::Printer* printer, bool isEventSourced);
virtual void GenerateMembers(io::Printer* printer, bool isEventSource) = 0;
virtual void GenerateEventSource(io::Printer* printer) = 0;
virtual void GenerateEventAdd(io::Printer* printer, bool isMap = false) = 0;
Expand Down
48 changes: 32 additions & 16 deletions plugin/src/lib/csharp_map_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,33 @@ MapFieldGenerator::MapFieldGenerator(const FieldDescriptor* descriptor,
MapFieldGenerator::~MapFieldGenerator() {
}

void MapFieldGenerator::GenerateMembers(io::Printer* printer, bool isEventSourced) {
void MapFieldGenerator::GenerateConstructor(io::Printer* printer, bool isEventSourced) {
if(isEventSourced) {
const FieldDescriptor* key_descriptor =
descriptor_->message_type()->FindFieldByName("key");
const FieldDescriptor* value_descriptor =
descriptor_->message_type()->FindFieldByName("value");
std::map<string, string> vars;
vars["name"] = variables_["name"];
vars["type_name"] = variables_["type_name"];
vars["property_name"] = variables_["property_name"];
vars["number"] = variables_["number"];
vars["key_type_name"] = type_name(key_descriptor);
vars["value_type_name"] = type_name(value_descriptor);
vars["field_name"] = UnderscoresToCamelCase(GetFieldName(descriptor_), false);

if (value_descriptor->type() == FieldDescriptor::TYPE_MESSAGE) {
printer->Print(vars,
"$name$_ = new EventMapField<$key_type_name$, $value_type_name$>($name$MapConverter, Context, $number$, true);\n");
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we creating a dependencies now on the EventSource code even when the message is not sourced ?

printer->Print(vars,
"$name$_ = new EventMapField<$key_type_name$, $value_type_name$>($name$MapConverter, Context, $number$);\n");
}
}
}

void MapFieldGenerator::GenerateMembers(io::Printer* printer, bool isEventSourced) {
const FieldDescriptor* key_descriptor =
descriptor_->message_type()->FindFieldByName("key");
const FieldDescriptor* value_descriptor =
Expand Down Expand Up @@ -159,16 +185,9 @@ void MapFieldGenerator::GenerateMembers(io::Printer* printer, bool isEventSource
"}\n"
"private static readonly EventMapConverter<$key_type_name$, $value_type_name$> $name$MapConverter = new $property_name$MapConverter();\n");

if (value_descriptor->type() == FieldDescriptor::TYPE_MESSAGE) {
printer->Print(
variables_,
"private readonly EventMapField<$key_type_name$, $value_type_name$> $name$_ = new EventMapField<$key_type_name$, $value_type_name$>($name$MapConverter, true);\n");
}
else {
printer->Print(
variables_,
"private readonly EventMapField<$key_type_name$, $value_type_name$> $name$_ = new EventMapField<$key_type_name$, $value_type_name$>($name$MapConverter);\n");
}
printer->Print(
variables_,
"private readonly EventMapField<$key_type_name$, $value_type_name$> $name$_;\n");
}
else {
printer->Print(
Expand Down Expand Up @@ -340,15 +359,12 @@ void MapFieldGenerator::GenerateCloningCode(io::Printer* printer, bool isEventSo

if (value_descriptor->type() == FieldDescriptor::TYPE_MESSAGE) {
printer->Print(vars,
"$name$_ = new EventMapField<$key_type_name$, $value_type_name$>($name$MapConverter, other.$name$_.Clone(), true);\n");
"$name$_ = new EventMapField<$key_type_name$, $value_type_name$>($name$MapConverter, Context, $number$, other.$name$_.Clone(), true);\n");
}
else {
printer->Print(vars,
"$name$_ = new EventMapField<$key_type_name$, $value_type_name$>($name$MapConverter, other.$name$_.Clone());\n");
"$name$_ = new EventMapField<$key_type_name$, $value_type_name$>($name$MapConverter, Context, $number$, other.$name$_.Clone());\n");
}

printer->Print(vars,
"$name$_.SetContext(Context, $number$);\n");
}
else {
printer->Print(variables_,
Expand Down
3 changes: 2 additions & 1 deletion plugin/src/lib/csharp_map_field.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class MapFieldGenerator : public FieldGeneratorBase {
const Options* options);
~MapFieldGenerator();

virtual void GenerateConstructor(io::Printer* printer, bool isEventSourced);
virtual void GenerateCloningCode(io::Printer* printer, bool isEventSourced);
virtual void GenerateFreezingCode(io::Printer* printer);
/// The following code is Copyright 2018, Zynga
Expand All @@ -59,7 +60,7 @@ class MapFieldGenerator : public FieldGeneratorBase {
virtual void GenerateEventAddEvent(io::Printer* printer);
virtual void GenerateCheckSum(io::Printer* printer);
///

virtual void GenerateMergingCode(io::Printer* printer, bool isEventSourced);
virtual void GenerateParsingCode(io::Printer* printer, bool isEventSourced);
virtual void GenerateSerializationCode(io::Printer* printer);
Expand Down
54 changes: 12 additions & 42 deletions plugin/src/lib/csharp_message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ MessageGenerator::MessageGenerator(const Descriptor* descriptor,
}
std::sort(fields_by_number_.begin(), fields_by_number_.end(),
CompareFieldNumbers);

is_event_sourced = HasFileEventSource(descriptor->file());

// only use this override if the file level check has not been
Expand Down Expand Up @@ -130,7 +130,7 @@ void MessageGenerator::Generate(io::Printer* printer, bool isEventSourced) {

WriteMessageDocComment(printer, descriptor_);
AddDeprecatedFlag(printer);

printer->Print(
vars,
"$access_level$ sealed partial class $class_name$ :");
Expand All @@ -139,7 +139,7 @@ void MessageGenerator::Generate(io::Printer* printer, bool isEventSourced) {
if (IsEventSourced()) {
printer->Print(
vars,
" zpr::EventRegistry<$class_name$>,");
" zpr::EventRegistry<$class_name$>,");
}
///

Expand Down Expand Up @@ -196,17 +196,10 @@ void MessageGenerator::Generate(io::Printer* printer, bool isEventSourced) {
"public $class_name$() {\n"
" OnConstruction();\n");

if (IsEventSourced()) {
for (int i = 0; i < descriptor_->field_count(); i++) {
bool isFieldSourced = false;
const FieldDescriptor* fieldDescriptor = descriptor_->field(i);
if(fieldDescriptor->is_map() || fieldDescriptor->is_repeated()) {
printer->Print(
" $field_name$_.SetContext(Context, $field_num$);\n",
"field_name", UnderscoresToCamelCase(GetFieldName(fieldDescriptor), false),
"field_num", SimpleItoa(fieldDescriptor->number()));
}
}
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* fieldDescriptor = descriptor_->field(i);
scoped_ptr<FieldGeneratorBase> generator(CreateFieldGeneratorInternal(fieldDescriptor));
generator->GenerateConstructor(printer, IsEventSourced());
}

printer->Print(
Expand All @@ -219,7 +212,7 @@ void MessageGenerator::Generate(io::Printer* printer, bool isEventSourced) {

/// The following code is Copyright 2018, Zynga
// we add this to all files just as an easy way to know if this is a DeltaFile
// without having to do some sort of cast check
// without having to do some sort of cast check
printer->Print(
"public static bool IsEventSourced = $sourced$;\n\n",
"sourced", IsEventSourced() ? "true" : "false");
Expand All @@ -230,30 +223,7 @@ void MessageGenerator::Generate(io::Printer* printer, bool isEventSourced) {
vars,
"protected override $class_name$ Message { get{ return this; } }\n\n");
}

if (IsEventSourced()) {
printer->Print(
vars,
"public override void SetParent(EventContext parent, int field) {\n"
" base.SetParent(parent, field);\n");

for (int i = 0; i < descriptor_->field_count(); i++) {
bool isFieldSourced = false;
const FieldDescriptor* fieldDescriptor = descriptor_->field(i);
if(fieldDescriptor->is_map() || fieldDescriptor->is_repeated()) {
printer->Print(
" $field_name$_.SetContext(Context, $field_num$);\n",
"field_name", UnderscoresToCamelCase(GetFieldName(fieldDescriptor), false),
"field_num", SimpleItoa(fieldDescriptor->number()));
}
}

printer->Print(
vars,
"}\n");
}
///

// Fields/properties
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* fieldDescriptor = descriptor_->field(i);
Expand All @@ -267,7 +237,7 @@ void MessageGenerator::Generate(io::Printer* printer, bool isEventSourced) {
"index", SimpleItoa(fieldDescriptor->number()));
scoped_ptr<FieldGeneratorBase> generator(
CreateFieldGeneratorInternal(fieldDescriptor));

generator->GenerateMembers(printer, IsEventSourced());
printer->Print("\n");
}
Expand Down Expand Up @@ -370,7 +340,7 @@ void MessageGenerator::Generate(io::Printer* printer, bool isEventSourced) {
"field_number", SimpleItoa(fieldDescriptor->number()));
scoped_ptr<FieldGeneratorBase> generator(
CreateFieldGeneratorInternal(fieldDescriptor));

generator->GenerateEventSource(printer);
printer->Print(" }\n");
printer->Print(" break;\n");
Expand Down Expand Up @@ -402,7 +372,7 @@ void MessageGenerator::Generate(io::Printer* printer, bool isEventSourced) {
"}\n\n");
}
///


printer->Outdent();
printer->Print("}\n");
Expand Down Expand Up @@ -614,7 +584,7 @@ void MessageGenerator::GenerateMergingMethods(io::Printer* printer, bool isEvent
"}\n");
// Merge non-oneof fields
for (int i = 0; i < descriptor_->field_count(); i++) {
if (!descriptor_->field(i)->containing_oneof()) {
if (!descriptor_->field(i)->containing_oneof()) {
scoped_ptr<FieldGeneratorBase> generator(
CreateFieldGeneratorInternal(descriptor_->field(i)));
generator->GenerateMergingCode(printer, isEventSourced);
Expand Down
38 changes: 24 additions & 14 deletions plugin/src/lib/csharp_message_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void MessageFieldGenerator::GenerateMembers(io::Printer* printer, bool isEventSo
"$access_level$ $type_name$ $property_name$ {\n"
" get { return $name$_; }\n"
" set {\n");

if (isEventSourced) {
if (isEnternalSourced) {
printer->Print(
Expand All @@ -98,7 +98,7 @@ void MessageFieldGenerator::GenerateMembers(io::Printer* printer, bool isEventSo
variables_,
" #endif\n");
}

printer->Print(
variables_,
" $name$_ = value;\n"
Expand All @@ -122,7 +122,7 @@ void MessageFieldGenerator::GenerateEventSource(io::Printer* printer) {
" }\n");
}
else {
printer->Print(variables_,
printer->Print(variables_,
" $name$_ = $type_name$.Parser.ParseFrom(e.Set.ByteData);\n");
}
}
Expand Down Expand Up @@ -154,7 +154,7 @@ void MessageFieldGenerator::GenerateEventAddEvent(io::Printer* printer) {
" e.Path.AddRange(this.Path.$field_name$Path._path);\n",
"field_name", GetPropertyName(descriptor_));
}

}

void MessageFieldGenerator::GenerateCheckSum(io::Printer* printer) {
Expand Down Expand Up @@ -238,8 +238,18 @@ void MessageFieldGenerator::WriteToString(io::Printer* printer) {
}

void MessageFieldGenerator::GenerateCloningCode(io::Printer* printer, bool isEventSourced) {
printer->Print(variables_,
"$name$_ = other.$has_property_check$ ? other.$property_name$.Clone() : null;\n");
bool isInternalEventSourced = IsInternalEventSourced();
if(isEventSourced && isInternalEventSourced) {
printer->Print(variables_,
"if(other.$has_property_check$) {\n"
" $name$_ = other.$property_name$.Clone();\n"
" $name$_.SetParent(Context, $number$);\n"
"}\n");
}
else {
printer->Print(variables_,
"$name$_ = other.$has_property_check$ ? other.$property_name$.Clone() : null;\n");
}
}

void MessageFieldGenerator::GenerateFreezingCode(io::Printer* printer) {
Expand All @@ -253,34 +263,34 @@ void MessageFieldGenerator::GenerateCodecCode(io::Printer* printer) {

// $AS TODO: Make this function a little less crazy!
bool MessageFieldGenerator::IsInternalEventSourced() {
bool isInternalSourced = !IsGoogleMessage(descriptor_->message_type())
bool isInternalSourced = !IsGoogleMessage(descriptor_->message_type())
&& HasFileEventSource(descriptor_->file());

if (!isInternalSourced) {
const MessageOptions& op = descriptor_->message_type()->options();
isInternalSourced = op.HasExtension(com::zynga::runtime::protobuf::event_sourced);
isInternalSourced = op.HasExtension(com::zynga::runtime::protobuf::event_sourced);

// we check and see if you are a nested type of the field owner if so
// then we can assume
// then we can assume
const Descriptor* parentObject = descriptor_->containing_type();
if (parentObject && parentObject->nested_type_count() != 0) {
const MessageOptions& parentOptions = parentObject->options();
if (parentOptions.HasExtension(com::zynga::runtime::protobuf::event_sourced) &&
parentObject->FindNestedTypeByName(descriptor_->message_type()->name()) != NULL) {
isInternalSourced = true;
isInternalSourced = true;
}
}
}
else {
// we need to check and see if the field is from this current file and if its not
// then we need to check its Sourced case.
// then we need to check its Sourced case.
if (descriptor_->message_type()->file() != descriptor_->file()) {
isInternalSourced = !IsGoogleMessage(descriptor_->message_type())
isInternalSourced = !IsGoogleMessage(descriptor_->message_type())
&& HasFileEventSource(descriptor_->message_type()->file());

if (!isInternalSourced) {
const MessageOptions& op = descriptor_->message_type()->options();
isInternalSourced = op.HasExtension(com::zynga::runtime::protobuf::event_sourced);
isInternalSourced = op.HasExtension(com::zynga::runtime::protobuf::event_sourced);
}
}
}
Expand Down Expand Up @@ -386,7 +396,7 @@ void MessageOneofFieldGenerator::GenerateEventAddEvent(io::Printer* printer) {

void MessageOneofFieldGenerator::GenerateMergingCode(io::Printer* printer, bool isEventSourced) {
bool isInternalEventSourced = IsInternalEventSourced();
printer->Print(variables_,
printer->Print(variables_,
"if ($property_name$ == null) {\n"
" $property_name$ = new $type_name$();\n");
if(isEventSourced && isInternalEventSourced) {
Expand Down
14 changes: 10 additions & 4 deletions plugin/src/lib/csharp_repeated_enum_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {

}

void RepeatedEnumFieldGenerator::GenerateConstructor(io::Printer* printer, bool isEventSourced) {
if(isEventSourced) {
printer->Print(variables_,
"$name$_ = new EventRepeatedField<$type_name$>($name$DataConverter, Context, $number$);\n");
}
}

void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer, bool isEventSourced) {
printer->Print(
variables_,
Expand All @@ -82,7 +89,7 @@ void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer, bool isEv

printer->Print(
variables_,
"private readonly EventRepeatedField<$type_name$> $name$_ = new EventRepeatedField<$type_name$>($name$DataConverter);\n");
"private readonly EventRepeatedField<$type_name$> $name$_;\n");
}
else {
printer->Print(
Expand Down Expand Up @@ -158,7 +165,7 @@ void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer)
"$name$_.WriteTo(output, _repeated_$name$_codec);\n");
}

void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {
printer->Print(
variables_,
"size += $name$_.CalculateSize(_repeated_$name$_codec);\n");
Expand All @@ -184,8 +191,7 @@ void RepeatedEnumFieldGenerator::WriteToString(io::Printer* printer) {
void RepeatedEnumFieldGenerator::GenerateCloningCode(io::Printer* printer, bool isEventSourced) {
if(isEventSourced) {
printer->Print(variables_,
"$name$_ = new EventRepeatedField<$type_name$>($name$DataConverter, other.$property_name$.Clone());\n"
"$name$_.SetContext(Context, $number$);\n");
"$name$_ = new EventRepeatedField<$type_name$>($name$DataConverter, Context, $number$, other.$property_name$.Clone());\n");
}
else {
printer->Print(variables_,
Expand Down
Loading