Skip to content

Commit

Permalink
Configuration of local features are completely rewritten.
Browse files Browse the repository at this point in the history
The .proto files now only contain build in features that will compile using protoc.
Local features are now read and written to the accompanying .csproto files.
  • Loading branch information
Peter Hultqvist committed Jun 10, 2012
1 parent af816cb commit 1728984
Show file tree
Hide file tree
Showing 44 changed files with 2,243 additions and 3,498 deletions.
52 changes: 52 additions & 0 deletions CodeGenerator.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeGenerator", "CodeGenerator\CodeGenerator.csproj", "{108DF716-5E86-4515-836F-1AC211CA648B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{108DF716-5E86-4515-836F-1AC211CA648B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{108DF716-5E86-4515-836F-1AC211CA648B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{108DF716-5E86-4515-836F-1AC211CA648B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{108DF716-5E86-4515-836F-1AC211CA648B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = CodeGenerator\CodeGenerator.csproj
Policies = $0
$0.TextStylePolicy = $1
$1.inheritsSet = VisualStudio
$1.inheritsScope = text/plain
$1.scope = text/x-csharp
$0.CSharpFormattingPolicy = $2
$2.IndentSwitchBody = True
$2.AnonymousMethodBraceStyle = NextLine
$2.PropertyBraceStyle = NextLine
$2.PropertyGetBraceStyle = NextLine
$2.PropertySetBraceStyle = NextLine
$2.EventBraceStyle = NextLine
$2.EventAddBraceStyle = NextLine
$2.EventRemoveBraceStyle = NextLine
$2.StatementBraceStyle = NextLine
$2.ArrayInitializerBraceStyle = NextLine
$2.BeforeMethodDeclarationParentheses = False
$2.BeforeMethodCallParentheses = False
$2.BeforeConstructorDeclarationParentheses = False
$2.BeforeDelegateDeclarationParentheses = False
$2.NewParentheses = False
$2.inheritsSet = Mono
$2.inheritsScope = text/x-csharp
$2.scope = text/x-csharp
$0.TextStylePolicy = $3
$3.inheritsSet = VisualStudio
$3.inheritsScope = text/plain
$3.scope = text/plain
$0.TextStylePolicy = $4
$4.inheritsSet = VisualStudio
$4.inheritsScope = text/plain
$4.scope = text/x-csrc
EndGlobalSection
EndGlobal
33 changes: 26 additions & 7 deletions CodeGenerator/CodeGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<PlatformTarget>x86</PlatformTarget>
<Commandlineparameters>../../../Test/ExampleA.proto ../../../Test/Generated/ExampleA.cs ../../../Test/TestB/ExampleB.proto ../../../Test/TestB/Generated/ExampleB.cs</Commandlineparameters>
<Commandlineparameters>../../../Test/AddressBook.proto ../../../Test/LocalFeatures.proto ../../../Test/ProtoFeatures.proto ../../../Test/Generated/Generated.cs</Commandlineparameters>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
Expand Down Expand Up @@ -49,12 +49,15 @@
<Compile Include="CodeGenerator\CodeWriter.cs" />
<Compile Include="Proto\ProtoMessage.cs" />
<Compile Include="Proto\ProtoEnum.cs" />
<Compile Include="Proto\ProtoFile.cs" />
<Compile Include="Proto\ProtoType.cs" />
<Compile Include="Proto\ProtoPacked.cs" />
<Compile Include="Proto\ProtoBuiltin.cs" />
<Compile Include="CodeGenerator\FieldSerializer.cs" />
<Compile Include="CodeGenerator\MessageSerializer.cs" />
<Compile Include="CsProtoParser.cs" />
<Compile Include="CsProtoReader.cs" />
<Compile Include="Proto\ProtoCollection.cs" />
<Compile Include="CsProtoWriter.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand All @@ -75,11 +78,7 @@
<Folder Include="Proto\" />
<Folder Include="ProtocolParser\" />
<Folder Include="CodeGenerator\" />
</ItemGroup>
<ItemGroup>
<None Include="..\csharpgen.proto">
<Link>csharpgen.proto</Link>
</None>
<Folder Include="ProtoSpec\" />
</ItemGroup>
<ProjectExtensions>
<MonoDevelop>
Expand All @@ -93,4 +92,24 @@
</Properties>
</MonoDevelop>
</ProjectExtensions>
<ItemGroup>
<None Include="..\Test\AddressBook.csproto">
<Link>ProtoSpec\AddressBook.csproto</Link>
</None>
<None Include="..\Test\AddressBook.proto">
<Link>ProtoSpec\AddressBook.proto</Link>
</None>
<None Include="..\Test\LocalFeatures.csproto">
<Link>ProtoSpec\LocalFeatures.csproto</Link>
</None>
<None Include="..\Test\LocalFeatures.proto">
<Link>ProtoSpec\LocalFeatures.proto</Link>
</None>
<None Include="..\Test\ProtoFeatures.csproto">
<Link>ProtoSpec\ProtoFeatures.csproto</Link>
</None>
<None Include="..\Test\ProtoFeatures.proto">
<Link>ProtoSpec\ProtoFeatures.proto</Link>
</None>
</ItemGroup>
</Project>
40 changes: 20 additions & 20 deletions CodeGenerator/CodeGenerator/FieldSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public static void GenerateFieldReader(Field f, CodeWriter cw)
//TODO: read without buffering
cw.Using("MemoryStream ms" + f.ID + " = new MemoryStream(ProtocolParser.ReadBytes(stream))");
cw.WriteLine("while(ms" + f.ID + ".Position < ms" + f.ID + ".Length)");
cw.WriteIndent("instance." + f.Name + ".Add(" + GenerateFieldTypeReader(f, "ms" + f.ID, "br", null) + ");");
cw.WriteIndent("instance." + f.CsName + ".Add(" + GenerateFieldTypeReader(f, "ms" + f.ID, "br", null) + ");");
cw.EndBracket();
} else
{
cw.WriteLine("instance." + f.Name + ".Add(" + GenerateFieldTypeReader(f, "stream", "br", null) + ");");
cw.WriteLine("instance." + f.CsName + ".Add(" + GenerateFieldTypeReader(f, "stream", "br", null) + ");");
}
} else
{
Expand All @@ -36,7 +36,7 @@ public static void GenerateFieldReader(Field f, CodeWriter cw)
//We could possibly support bytes primitive too but it would require the incoming length to match the wire length
if (f.ProtoType is ProtoMessage)
{
cw.WriteLine(GenerateFieldTypeReader(f, "stream", "br", "instance." + f.Name) + ";");
cw.WriteLine(GenerateFieldTypeReader(f, "stream", "br", "instance." + f.CsName) + ";");
return;
}
cw.WriteIndent("throw new InvalidOperationException(\"Can't deserialize into a readonly primitive field\");");
Expand All @@ -47,21 +47,21 @@ public static void GenerateFieldReader(Field f, CodeWriter cw)
{
if (f.ProtoType.OptionType == "struct")
{
cw.WriteLine(GenerateFieldTypeReader(f, "stream", "br", "ref instance." + f.Name) + ";");
cw.WriteLine(GenerateFieldTypeReader(f, "stream", "br", "ref instance." + f.CsName) + ";");
return;
}

cw.WriteLine("if (instance." + f.Name + " == null)");
cw.WriteLine("if (instance." + f.CsName + " == null)");
if (f.ProtoType.OptionType == "interface")
cw.WriteIndent("throw new InvalidOperationException(\"Can't deserialize into a interfaces null pointer\");");
else
cw.WriteIndent("instance." + f.Name + " = " + GenerateFieldTypeReader(f, "stream", "br", null) + ";");
cw.WriteIndent("instance." + f.CsName + " = " + GenerateFieldTypeReader(f, "stream", "br", null) + ";");
cw.WriteLine("else");
cw.WriteIndent(GenerateFieldTypeReader(f, "stream", "br", "instance." + f.Name) + ";");
cw.WriteIndent(GenerateFieldTypeReader(f, "stream", "br", "instance." + f.CsName) + ";");
return;
}

cw.WriteLine("instance." + f.Name + " = " + GenerateFieldTypeReader(f, "stream", "br", "instance." + f.Name) + ";");
cw.WriteLine("instance." + f.CsName + " = " + GenerateFieldTypeReader(f, "stream", "br", "instance." + f.CsName) + ";");
}
}

Expand Down Expand Up @@ -207,7 +207,7 @@ public static void GenerateFieldWriter(ProtoMessage m, Field f, CodeWriter cw)
{
if (f.OptionPacked == true)
{
cw.IfBracket("instance." + f.Name + " != null");
cw.IfBracket("instance." + f.CsName + " != null");
GenerateKeyWriter("stream", f.ID, Wire.LengthDelimited, cw);
cw.Using("MemoryStream ms" + f.ID + " = new MemoryStream()");
if (f.ProtoType is ProtoBuiltin)
Expand All @@ -224,16 +224,16 @@ public static void GenerateFieldWriter(ProtoMessage m, Field f, CodeWriter cw)
break;
}
}
cw.ForeachBracket("var i" + f.ID + " in instance." + f.Name);
cw.ForeachBracket("var i" + f.ID + " in instance." + f.CsName);
cw.WriteLine(GenerateFieldTypeWriter(f, "ms" + f.ID, "bw" + f.ID, "i" + f.ID));
cw.EndBracket();
GenerateBytesWriter("stream", "ms" + f.ID, cw);
cw.EndBracket();
cw.EndBracket();
} else
{
cw.IfBracket("instance." + f.Name + " != null");
cw.ForeachBracket("var i" + f.ID + " in instance." + f.Name);
cw.IfBracket("instance." + f.CsName + " != null");
cw.ForeachBracket("var i" + f.ID + " in instance." + f.CsName);
GenerateKeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
cw.WriteLine(GenerateFieldTypeWriter(f, "stream", "bw", "i" + f.ID));
cw.EndBracket();
Expand All @@ -247,35 +247,35 @@ public static void GenerateFieldWriter(ProtoMessage m, Field f, CodeWriter cw)
f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
{
if (f.ProtoType.Nullable) //Struct always exist, not optional
cw.IfBracket("instance." + f.Name + " != null");
cw.IfBracket("instance." + f.CsName + " != null");
GenerateKeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
cw.WriteLine(GenerateFieldTypeWriter(f, "stream", "bw", "instance." + f.Name));
cw.WriteLine(GenerateFieldTypeWriter(f, "stream", "bw", "instance." + f.CsName));
if (f.ProtoType.Nullable) //Struct always exist, not optional
cw.EndBracket();
return;
}
if (f.ProtoType is ProtoEnum)
{
cw.IfBracket("instance." + f.Name + " != " + f.ProtoType.CsType + "." + f.OptionDefault);
cw.IfBracket("instance." + f.CsName + " != " + f.ProtoType.CsType + "." + f.OptionDefault);
GenerateKeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
cw.WriteLine(GenerateFieldTypeWriter(f, "stream", "bw", "instance." + f.Name));
cw.WriteLine(GenerateFieldTypeWriter(f, "stream", "bw", "instance." + f.CsName));
cw.EndBracket();
return;
}
GenerateKeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
cw.WriteLine(GenerateFieldTypeWriter(f, "stream", "bw", "instance." + f.Name));
cw.WriteLine(GenerateFieldTypeWriter(f, "stream", "bw", "instance." + f.CsName));
return;
} else if (f.Rule == FieldRule.Required)
{
if (f.ProtoType is ProtoMessage && f.ProtoType.OptionType != "struct" ||
f.ProtoType.ProtoName == ProtoBuiltin.String ||
f.ProtoType.ProtoName == ProtoBuiltin.Bytes)
{
cw.WriteLine("if (instance." + f.Name + " == null)");
cw.WriteIndent("throw new ArgumentNullException(\"" + f.Name + "\", \"Required by proto specification.\");");
cw.WriteLine("if (instance." + f.CsName + " == null)");
cw.WriteIndent("throw new ArgumentNullException(\"" + f.CsName + "\", \"Required by proto specification.\");");
}
GenerateKeyWriter("stream", f.ID, f.ProtoType.WireType, cw);
cw.WriteLine(GenerateFieldTypeWriter(f, "stream", "bw", "instance." + f.Name));
cw.WriteLine(GenerateFieldTypeWriter(f, "stream", "bw", "instance." + f.CsName));
return;
}
throw new NotImplementedException("Unknown rule: " + f.Rule);
Expand Down
26 changes: 13 additions & 13 deletions CodeGenerator/CodeGenerator/MessageCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static class MessageCode
public static void GenerateClass(ProtoMessage m, CodeWriter cw)
{
//Do not generate class code for external classes
if (m.OptionExternal || m.OptionImported)
if (m.OptionExternal)
{
cw.Comment("Written elsewhere");
cw.Comment(m.OptionAccess + " " + m.OptionType + " " + m.CsType + " {}");
Expand Down Expand Up @@ -36,7 +36,7 @@ public static void GenerateClass(ProtoMessage m, CodeWriter cw)
cw.WriteLine();
}

foreach (ProtoMessage sub in m.Messages)
foreach (ProtoMessage sub in m.Messages.Values)
{
GenerateClass(sub, cw);
cw.WriteLine();
Expand All @@ -47,7 +47,7 @@ public static void GenerateClass(ProtoMessage m, CodeWriter cw)

static void GenerateEnums(ProtoMessage m, CodeWriter cw)
{
foreach (ProtoEnum me in m.Enums)
foreach (ProtoEnum me in m.Enums.Values)
{
cw.Bracket("public enum " + me.CsType);
foreach (var epair in me.Enums)
Expand All @@ -71,33 +71,33 @@ static void GenerateProperties(ProtoMessage m, CodeWriter cw)
{
foreach (Field f in m.Fields.Values)
{
if (f.OptionGenerate)
if (f.OptionExternal)
cw.WriteLine("//" + GenerateProperty(f) + " // Implemented by user elsewhere");
else
{
if (f.Comments != null)
cw.Summary(f.Comments);
cw.WriteLine(GenerateProperty(f));
cw.WriteLine();
} else
{
cw.WriteLine("//" + GenerateProperty(f) + " //Implemented by user elsewhere");
}
}

}
}

static string GenerateProperty(Field f)
{
string type = f.ProtoType.FullCsType;
if(f.OptionCodeType != null)
if (f.OptionCodeType != null)
type = f.OptionCodeType;
if (f.Rule == FieldRule.Repeated)
type = "List<" + f.ProtoType.FullCsType + ">";
type = "List<" + type + ">";

if (f.OptionReadOnly)
return f.OptionAccess + " readonly " + type + " " + f.Name + " = new " + type + "();";
return f.OptionAccess + " readonly " + type + " " + f.CsName + " = new " + type + "();";
else if (f.ProtoType is ProtoMessage && f.ProtoType.OptionType == "struct")
return f.OptionAccess + " " + type + " " + f.Name + ";";
return f.OptionAccess + " " + type + " " + f.CsName + ";";
else
return f.OptionAccess + " " + type + " " + f.Name + " { get; set; }";
return f.OptionAccess + " " + type + " " + f.CsName + " { get; set; }";
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions CodeGenerator/CodeGenerator/MessageSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void GenerateClassSerializer(ProtoMessage m, CodeWriter cw)
GenerateReader(m, cw);

GenerateWriter(m, cw);
foreach (ProtoMessage sub in m.Messages)
foreach (ProtoMessage sub in m.Messages.Values)
{
cw.WriteLine();
GenerateClassSerializer(sub, cw);
Expand Down Expand Up @@ -77,14 +77,14 @@ static void GenerateReader(ProtoMessage m, CodeWriter cw)
{
if (f.Rule == FieldRule.Repeated)
{
cw.WriteLine("if (instance." + f.Name + " == null)");
cw.WriteIndent("instance." + f.Name + " = new List<" + f.ProtoType.FullCsType + ">();");
cw.WriteLine("if (instance." + f.CsName + " == null)");
cw.WriteIndent("instance." + f.CsName + " = new List<" + f.ProtoType.FullCsType + ">();");
} else if (f.OptionDefault != null)
{
if (f.ProtoType is ProtoEnum)
cw.WriteLine("instance." + f.Name + " = " + f.ProtoType.FullCsType + "." + f.OptionDefault + ";");
cw.WriteLine("instance." + f.CsName + " = " + f.ProtoType.FullCsType + "." + f.OptionDefault + ";");
else
cw.WriteLine("instance." + f.Name + " = " + f.OptionDefault + ";");
cw.WriteLine("instance." + f.CsName + " = " + f.OptionDefault + ";");
} else if (f.Rule == FieldRule.Optional)
{
if (f.ProtoType is ProtoEnum)
Expand All @@ -93,7 +93,7 @@ static void GenerateReader(ProtoMessage m, CodeWriter cw)
//the default value is the first value listed in the enum's type definition
foreach (var kvp in pe.Enums)
{
cw.WriteLine("instance." + f.Name + " = " + kvp.Key + ";");
cw.WriteLine("instance." + f.CsName + " = " + kvp.Key + ";");
break;
}
}
Expand Down
10 changes: 3 additions & 7 deletions CodeGenerator/CodeGenerator/ProtoCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static class ProtoCode
/// <summary>
/// Generate code for reading and writing protocol buffer messages
/// </summary>
public static void Save(ProtoFile proto, string csPath)
public static void Save(ProtoCollection file, string csPath)
{
string ext = Path.GetExtension(csPath);
string prefix = csPath.Substring(0, csPath.Length - ext.Length);
Expand All @@ -36,7 +36,7 @@ Generated by ProtocolBuffer
cw.WriteLine("using System.Collections.Generic;");
cw.WriteLine();

foreach (ProtoMessage m in proto.Messages)
foreach (ProtoMessage m in file.Messages.Values)
{
cw.Bracket("namespace " + m.CsNamespace);
MessageCode.GenerateClass(m, cw);
Expand All @@ -63,12 +63,8 @@ Generated by ProtocolBuffer
cw.WriteLine("using ProtocolBuffers;");
cw.WriteLine();

foreach (ProtoMessage m in proto.Messages)
foreach (ProtoMessage m in file.Messages.Values)
{
//Do not generate serializer code for imported messages
if (m.OptionImported)
continue;

cw.Bracket("namespace " + m.CsNamespace);
MessageSerializer.GenerateClassSerializer(m, cw);
cw.EndBracket();
Expand Down
Loading

0 comments on commit 1728984

Please sign in to comment.