Protobuf: removed secondary constructors and private setters (as features, which are not supported yet by translator)
This commit is contained in:
+25
-36
@@ -7,6 +7,7 @@
|
|||||||
#include "kotlin_enum_generator.h"
|
#include "kotlin_enum_generator.h"
|
||||||
#include "kotlin_field_generator.h"
|
#include "kotlin_field_generator.h"
|
||||||
#include "kotlin_name_resolver.h"
|
#include "kotlin_name_resolver.h"
|
||||||
|
#include "UnreachableStateException.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
namespace google {
|
namespace google {
|
||||||
@@ -34,9 +35,6 @@ void ClassGenerator::generateCode(io::Printer *printer, bool isBuilder) const {
|
|||||||
// Generate construction routines
|
// Generate construction routines
|
||||||
generateInitSection(printer);
|
generateInitSection(printer);
|
||||||
printer->Print("\n");
|
printer->Print("\n");
|
||||||
generateConstructor(printer, isBuilder);
|
|
||||||
printer->Print("\n");
|
|
||||||
|
|
||||||
|
|
||||||
// enum declarations and nested classes declarations only for fair classes
|
// enum declarations and nested classes declarations only for fair classes
|
||||||
if (!isBuilder) {
|
if (!isBuilder) {
|
||||||
@@ -106,15 +104,15 @@ ClassGenerator::ClassGenerator(Descriptor const *descriptor, NameResolver * name
|
|||||||
int nested_types_count = descriptor->nested_type_count();
|
int nested_types_count = descriptor->nested_type_count();
|
||||||
for (int i = 0; i < nested_types_count; ++i) {
|
for (int i = 0; i < nested_types_count; ++i) {
|
||||||
Descriptor const * nestedClassDescriptor = descriptor->nested_type(i);
|
Descriptor const * nestedClassDescriptor = descriptor->nested_type(i);
|
||||||
nameResolver->addClass(nestedClassDescriptor->name(), getFullType());
|
|
||||||
classesDeclarations.push_back(new ClassGenerator(nestedClassDescriptor, nameResolver));
|
classesDeclarations.push_back(new ClassGenerator(nestedClassDescriptor, nameResolver));
|
||||||
|
nameResolver->addClass(nestedClassDescriptor->name(), getFullType(), classesDeclarations.back());
|
||||||
}
|
}
|
||||||
|
|
||||||
int enums_declarations_count = descriptor->enum_type_count();
|
int enums_declarations_count = descriptor->enum_type_count();
|
||||||
for (int i = 0; i < enums_declarations_count; ++i) {
|
for (int i = 0; i < enums_declarations_count; ++i) {
|
||||||
EnumDescriptor const * nestedEnumDescriptor = descriptor->enum_type(i);
|
EnumDescriptor const * nestedEnumDescriptor = descriptor->enum_type(i);
|
||||||
nameResolver->addClass(nestedEnumDescriptor->name(), getFullType());
|
|
||||||
enumsDeclaraions.push_back(new EnumGenerator(nestedEnumDescriptor, nameResolver));
|
enumsDeclaraions.push_back(new EnumGenerator(nestedEnumDescriptor, nameResolver));
|
||||||
|
nameResolver->addClass(nestedEnumDescriptor->name(), getFullType(), /* classGenerator = */ nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -185,8 +183,9 @@ void ClassGenerator::generateMergeMethods(io::Printer *printer) const {
|
|||||||
printer->Indent();
|
printer->Indent();
|
||||||
|
|
||||||
vars["builderName"] = getBuilderFullType();
|
vars["builderName"] = getBuilderFullType();
|
||||||
printer->Print(vars, "val builder = $builderName$()\n");
|
vars["initValue"] = getBuilderInitValue();
|
||||||
printer->Print("mergeWith(builder.parseFromWithSize(input, expectedSize).build())");
|
printer->Print(vars, "val builder = $initValue$\n");
|
||||||
|
printer->Print("mergeWith(builder.parseFromWithSize(input, expectedSize).build())\n");
|
||||||
|
|
||||||
printer->Outdent();
|
printer->Outdent();
|
||||||
printer->Print("}\n");
|
printer->Print("}\n");
|
||||||
@@ -197,8 +196,8 @@ void ClassGenerator::generateMergeMethods(io::Printer *printer) const {
|
|||||||
printer->Indent();
|
printer->Indent();
|
||||||
|
|
||||||
vars["builderName"] = getBuilderFullType();
|
vars["builderName"] = getBuilderFullType();
|
||||||
printer->Print(vars, "val builder = $builderName$()\n");
|
printer->Print(vars, "val builder = $initValue$\n");
|
||||||
printer->Print("mergeWith(builder.parseFrom(input).build())");
|
printer->Print("mergeWith(builder.parseFrom(input).build())\n");
|
||||||
|
|
||||||
printer->Outdent();
|
printer->Outdent();
|
||||||
printer->Print("}\n");
|
printer->Print("}\n");
|
||||||
@@ -221,7 +220,7 @@ void ClassGenerator::generateWriteToMethod(io::Printer *printer) const {
|
|||||||
printer->Print("}\n");
|
printer->Print("}\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClassGenerator::generateConstructor(io::Printer * printer, bool isBuilder) const {
|
void ClassGenerator::generateHeader(io::Printer * printer, bool isBuilder) const {
|
||||||
// build list of arguments like 'field1: Type1, field2: Type2, ... '
|
// build list of arguments like 'field1: Type1, field2: Type2, ... '
|
||||||
string argumentList = "";
|
string argumentList = "";
|
||||||
for (int i = 0; i < properties.size(); ++i) {
|
for (int i = 0; i < properties.size(); ++i) {
|
||||||
@@ -231,35 +230,12 @@ void ClassGenerator::generateConstructor(io::Printer * printer, bool isBuilder)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
map <string, string> vars;
|
|
||||||
vars["name"] = isBuilder ? getBuidlerSimpleType() : getSimpleType();
|
|
||||||
vars["private"] = isBuilder ? "" : "private ";
|
|
||||||
vars["arguments"] = argumentList;
|
|
||||||
printer->Print(vars, "$private$constructor ($arguments$) : this() {\n");
|
|
||||||
printer->Indent();
|
|
||||||
|
|
||||||
for (int i = 0; i < properties.size(); ++i) {
|
|
||||||
map <string, string> vars;
|
|
||||||
vars["name"] = properties[i]->simpleName;
|
|
||||||
vars["initVal"] = properties[i]->getInitValue();
|
|
||||||
printer->Print(vars,
|
|
||||||
"this.$name$ = $name$"
|
|
||||||
"\n"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
printer->Outdent();
|
|
||||||
printer->Print("}\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClassGenerator::generateHeader(io::Printer * printer, bool isBuilder) const {
|
|
||||||
|
|
||||||
|
|
||||||
map<string, string> vars;
|
map<string, string> vars;
|
||||||
vars["name"] = isBuilder? getBuidlerSimpleType() : getSimpleType();
|
vars["name"] = isBuilder? getBuidlerSimpleType() : getSimpleType();
|
||||||
vars["maybePrivate"] = isBuilder? "" : " private";
|
vars["maybePrivate"] = isBuilder? "" : " private";
|
||||||
|
vars["args"] = argumentList;
|
||||||
printer->Print(vars,
|
printer->Print(vars,
|
||||||
"class $name$$maybePrivate$ constructor () {"
|
"class $name$$maybePrivate$ constructor ($args$) {"
|
||||||
"\n"
|
"\n"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -294,7 +270,7 @@ void ClassGenerator::generateInitSection(io::Printer * printer) const {
|
|||||||
vars["name"] = properties[i]->simpleName;
|
vars["name"] = properties[i]->simpleName;
|
||||||
vars["initVal"] = properties[i]->getInitValue();
|
vars["initVal"] = properties[i]->getInitValue();
|
||||||
printer->Print(vars,
|
printer->Print(vars,
|
||||||
"this.$name$ = $initVal$"
|
"this.$name$ = $name$"
|
||||||
"\n"
|
"\n"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -441,6 +417,19 @@ string ClassGenerator::getBuidlerSimpleType() const {
|
|||||||
return "Builder" + getSimpleType();
|
return "Builder" + getSimpleType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string ClassGenerator::getBuilderInitValue() const {
|
||||||
|
// build list of arguments like 'field1: Type1, field2: Type2, ... '
|
||||||
|
string argumentList = "";
|
||||||
|
for (int i = 0; i < properties.size(); ++i) {
|
||||||
|
argumentList += properties[i]->simpleName + " = " + properties[i]->getInitValue();
|
||||||
|
if (i + 1 != properties.size()) {
|
||||||
|
argumentList += ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getBuilderFullType() + "(" + argumentList + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace kotlin
|
} // namespace kotlin
|
||||||
} // namespace compiler
|
} // namespace compiler
|
||||||
|
|||||||
+1
-1
@@ -27,6 +27,7 @@ public:
|
|||||||
string getFullType() const;
|
string getFullType() const;
|
||||||
string getBuidlerSimpleType() const;
|
string getBuidlerSimpleType() const;
|
||||||
string getBuilderFullType() const;
|
string getBuilderFullType() const;
|
||||||
|
string getBuilderInitValue() const;
|
||||||
|
|
||||||
vector <FieldGenerator *> properties;
|
vector <FieldGenerator *> properties;
|
||||||
vector <ClassGenerator *> classesDeclarations;
|
vector <ClassGenerator *> classesDeclarations;
|
||||||
@@ -46,7 +47,6 @@ private:
|
|||||||
NameResolver * nameResolver;
|
NameResolver * nameResolver;
|
||||||
|
|
||||||
void generateHeader (io::Printer * printer, bool isBuilder = false) const;
|
void generateHeader (io::Printer * printer, bool isBuilder = false) const;
|
||||||
void generateConstructor (io::Printer * printer, bool isBuilder = false) const;
|
|
||||||
void generateBuilder (io::Printer * printer) const;
|
void generateBuilder (io::Printer * printer) const;
|
||||||
void generateBuildMethod (io::Printer * printer) const;
|
void generateBuildMethod (io::Printer * printer) const;
|
||||||
void generateInitSection (io::Printer * printer) const;
|
void generateInitSection (io::Printer * printer) const;
|
||||||
|
|||||||
+15
-6
@@ -19,15 +19,29 @@ string FieldGenerator::getInitValue() const {
|
|||||||
if (getProtoLabel() == FieldDescriptor::LABEL_REPEATED) {
|
if (getProtoLabel() == FieldDescriptor::LABEL_REPEATED) {
|
||||||
return getFullType() + "(0)";
|
return getFullType() + "(0)";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getProtoType() == FieldDescriptor::TYPE_MESSAGE) {
|
if (getProtoType() == FieldDescriptor::TYPE_MESSAGE) {
|
||||||
return getBuilderFullType() + "().build()";
|
ClassGenerator * cg = nameResolver->getClassGenerator(getSimpleType());
|
||||||
|
// build list of arguments like 'field1: Type1, field2: Type2, ... '
|
||||||
|
string argumentList = "";
|
||||||
|
for (int i = 0; i < cg->properties.size(); ++i) {
|
||||||
|
argumentList += cg->properties[i]->simpleName + " = " + cg->properties[i]->getInitValue();
|
||||||
|
if (i + 1 != cg->properties.size()) {
|
||||||
|
argumentList += ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getBuilderFullType() + "(" + argumentList + ").build()";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getProtoType() == FieldDescriptor::TYPE_ENUM) {
|
if (getProtoType() == FieldDescriptor::TYPE_ENUM) {
|
||||||
return getEnumFromIntConverter() + "(0)";
|
return getEnumFromIntConverter() + "(0)";
|
||||||
}
|
}
|
||||||
return name_resolving::protobufTypeToInitValue(getProtoType());
|
return name_resolving::protobufTypeToInitValue(getProtoType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void FieldGenerator::generateCode(io::Printer *printer, bool isBuilder) const {
|
void FieldGenerator::generateCode(io::Printer *printer, bool isBuilder) const {
|
||||||
map<string, string> vars;
|
map<string, string> vars;
|
||||||
vars["name"] = simpleName;
|
vars["name"] = simpleName;
|
||||||
@@ -38,11 +52,6 @@ void FieldGenerator::generateCode(io::Printer *printer, bool isBuilder) const {
|
|||||||
|
|
||||||
printer->Print(vars, "var $name$ : $field$\n");
|
printer->Print(vars, "var $name$ : $field$\n");
|
||||||
|
|
||||||
// make setter private
|
|
||||||
printer->Indent();
|
|
||||||
printer->Print("private set\n");
|
|
||||||
printer->Outdent();
|
|
||||||
|
|
||||||
// generate setter for builder
|
// generate setter for builder
|
||||||
if (isBuilder) {
|
if (isBuilder) {
|
||||||
generateSetter(printer);
|
generateSetter(printer);
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
string getInitValue() const;
|
string getInitValue() const;
|
||||||
|
|
||||||
|
|
||||||
/* Return string, that is suitable as suffix for corresponding IO methods in ProtoKot runtime.
|
/* Return string, that is suitable as suffix for corresponding IO methods in ProtoKot runtime.
|
||||||
* Example: int64-field -> Int64 (readInt64() and writeInt64() methods exist in ProtoKot runtime)
|
* Example: int64-field -> Int64 (readInt64() and writeInt64() methods exist in ProtoKot runtime)
|
||||||
*/
|
*/
|
||||||
|
|||||||
+1
-1
@@ -37,8 +37,8 @@ bool FileGenerator::Generate(const FileDescriptor *file, const string ¶meter
|
|||||||
for (int i = 0; i < topLevelMessagesCount; ++i) {
|
for (int i = 0; i < topLevelMessagesCount; ++i) {
|
||||||
Descriptor const * descriptor = file->message_type(i);
|
Descriptor const * descriptor = file->message_type(i);
|
||||||
// TODO: think about order of initialization and cross-branches calls. If we don't allow such things, everythign is ok atm
|
// TODO: think about order of initialization and cross-branches calls. If we don't allow such things, everythign is ok atm
|
||||||
nameResolver->addClass(descriptor->name(), "");
|
|
||||||
ClassGenerator * cgen = new ClassGenerator(descriptor, nameResolver);
|
ClassGenerator * cgen = new ClassGenerator(descriptor, nameResolver);
|
||||||
|
nameResolver->addClass(descriptor->name(), /* parentName = */ "", cgen);
|
||||||
classes.push_back(cgen);
|
classes.push_back(cgen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-1
@@ -199,9 +199,12 @@ string protobufTypeToKotlinWireType(FieldDescriptor::Type type) {
|
|||||||
NameResolver::NameResolver() {
|
NameResolver::NameResolver() {
|
||||||
names = map<string, string>();
|
names = map<string, string>();
|
||||||
builders = map<string, string>();
|
builders = map<string, string>();
|
||||||
|
generators = map<string, ClassGenerator *>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NameResolver::addClass(string simpleName, string parentName) {
|
void NameResolver::addClass(string simpleName, string parentName, ClassGenerator * classGenerator) {
|
||||||
|
generators[simpleName] = classGenerator;
|
||||||
|
|
||||||
if (parentName == "") {
|
if (parentName == "") {
|
||||||
names[simpleName] = simpleName;
|
names[simpleName] = simpleName;
|
||||||
builders[simpleName] = simpleName + ".Builder" + simpleName;
|
builders[simpleName] = simpleName + ".Builder" + simpleName;
|
||||||
@@ -221,6 +224,10 @@ string NameResolver::getBuilderName(string classSimpleName) {
|
|||||||
return builders[classSimpleName];
|
return builders[classSimpleName];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ClassGenerator * NameResolver::getClassGenerator(string simpleName) {
|
||||||
|
return generators[simpleName];
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace kotlin
|
} // namespace kotlin
|
||||||
} // namespace compiler
|
} // namespace compiler
|
||||||
} // namespace protobuf
|
} // namespace protobuf
|
||||||
|
|||||||
+4
-1
@@ -9,6 +9,7 @@
|
|||||||
#include <google/protobuf/descriptor.h>
|
#include <google/protobuf/descriptor.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include "kotlin_field_generator.h"
|
#include "kotlin_field_generator.h"
|
||||||
|
#include "kotlin_class_generator.h"
|
||||||
|
|
||||||
namespace google {
|
namespace google {
|
||||||
namespace protobuf {
|
namespace protobuf {
|
||||||
@@ -22,12 +23,14 @@ class NameResolver {
|
|||||||
public:
|
public:
|
||||||
NameResolver();
|
NameResolver();
|
||||||
|
|
||||||
void addClass (string simpleName, string parentName);
|
void addClass (string simpleName, string parentName, ClassGenerator * classGenerator);
|
||||||
string getClassName (string simpleName);
|
string getClassName (string simpleName);
|
||||||
string getBuilderName (string classSimpleName);
|
string getBuilderName (string classSimpleName);
|
||||||
|
ClassGenerator * getClassGenerator (string simpleName);
|
||||||
private:
|
private:
|
||||||
std::map <string, string> names;
|
std::map <string, string> names;
|
||||||
std::map <string, string> builders;
|
std::map <string, string> builders;
|
||||||
|
std::map <string, ClassGenerator *> generators;
|
||||||
};
|
};
|
||||||
namespace name_resolving {
|
namespace name_resolving {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user