Skip to content
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
20 changes: 10 additions & 10 deletions lang/csharp/src/apache/main/CodeGen/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ protected virtual void processInterface(Protocol protocol)

if (protocol.Messages.Count > 0)
{
builder.AppendLine("switch(messageName)");
builder.AppendLine("\t\t\tswitch(messageName)");
builder.Append("\t\t\t{");

foreach (var a in protocol.Messages)
Expand All @@ -580,11 +580,11 @@ protected virtual void processInterface(Protocol protocol)
}

builder.Append("\t\t\t}");
}

var cseGet = new CodeSnippetExpression(builder.ToString());
var cssRequest = new CodeSnippetStatement(builder.ToString());
requestMethod.Statements.Add(cssRequest);
}

requestMethod.Statements.Add(cseGet);
ctd.Members.Add(requestMethod);

AddMethods(protocol, false, ctd);
Expand Down Expand Up @@ -770,7 +770,7 @@ protected virtual CodeTypeDeclaration processRecord(Schema schema)
cmmGet.Attributes = MemberAttributes.Public;
cmmGet.ReturnType = new CodeTypeReference("System.Object");
cmmGet.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
StringBuilder getFieldStmt = new StringBuilder("switch (fieldPos)")
StringBuilder getFieldStmt = new StringBuilder("\t\t\tswitch (fieldPos)")
.AppendLine().AppendLine("\t\t\t{");

// declare Put() to be used by the Reader classes
Expand All @@ -780,7 +780,7 @@ protected virtual CodeTypeDeclaration processRecord(Schema schema)
cmmPut.ReturnType = new CodeTypeReference(typeof(void));
cmmPut.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
cmmPut.Parameters.Add(new CodeParameterDeclarationExpression("System.Object", "fieldValue"));
var putFieldStmt = new StringBuilder("switch (fieldPos)")
var putFieldStmt = new StringBuilder("\t\t\tswitch (fieldPos)")
.AppendLine().AppendLine("\t\t\t{");

if (isError)
Expand Down Expand Up @@ -874,15 +874,15 @@ protected virtual CodeTypeDeclaration processRecord(Schema schema)
// end switch block for Get()
getFieldStmt.AppendLine("\t\t\tdefault: throw new global::Avro.AvroRuntimeException(\"Bad index \" + fieldPos + \" in Get()\");")
.Append("\t\t\t}");
var cseGet = new CodeSnippetExpression(getFieldStmt.ToString());
cmmGet.Statements.Add(cseGet);
var cssGet = new CodeSnippetStatement(getFieldStmt.ToString());
cmmGet.Statements.Add(cssGet);
ctd.Members.Add(cmmGet);

// end switch block for Put()
putFieldStmt.AppendLine("\t\t\tdefault: throw new global::Avro.AvroRuntimeException(\"Bad index \" + fieldPos + \" in Put()\");")
.Append("\t\t\t}");
var csePut = new CodeSnippetExpression(putFieldStmt.ToString());
cmmPut.Statements.Add(csePut);
var cssPut = new CodeSnippetStatement(putFieldStmt.ToString());
cmmPut.Statements.Add(cssPut);
ctd.Members.Add(cmmPut);

string nspace = recordSchema.Namespace;
Expand Down
74 changes: 74 additions & 0 deletions lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,80 @@ public void GetTypesShouldReturnTypes()
Assert.That(Regex.Matches(planetEnumCode, "public enum PlanetEnum").Count, Is.EqualTo(1));
}

[Test]
public void RecordGetAndPutSwitchesShouldNotEmitUnreachableStatement()
{
AddSchema(@"
{
""name"": ""Sample"",
""namespace"": ""Avro.Test.CodeGen.UnreachableCodeRegression"",
""type"": ""record"",
""fields"": [
{ ""name"": ""value"", ""type"": ""string"" }
]
}
");
GenerateCode();
var types = GetTypes();
bool hasSampleCode = types.TryGetValue("Sample", out string sampleCode);
Assert.That(hasSampleCode);

// The exhaustive switch in Get()/Put() must not be followed by a stray ";" -
// that empty statement is unreachable (every case returns or throws) and
// trips IDE-only unreachable-code analysis (e.g. ReSharper) even though Roslyn
// does not flag it.
Assert.That(Regex.IsMatch(sampleCode, @"\}\s*;\s*\}\s*public virtual void Put"), Is.False);
Assert.That(Regex.IsMatch(sampleCode, @"in Put\(\)""\);\s*\}\s*;"), Is.False);

// CodeDom writes CodeSnippetStatement at column 0, so the snippet carries its own indent.
Assert.That(Regex.Matches(sampleCode, @"(?m)^\t\t\tswitch \(fieldPos\)\r?$").Count, Is.EqualTo(2));
Assert.That(Regex.Matches(sampleCode, @"(?m)^\s*switch \(fieldPos\)").Count, Is.EqualTo(2));
}

[Test]
public void ProtocolRequestSwitchShouldBeIndentedAndNotFollowedByEmptyStatement()
{
AddProtocol(@"
{
""protocol"": ""Greeter"",
""namespace"": ""Avro.Test.CodeGen.ProtocolSwitchRegression"",
""types"": [],
""messages"": {
""hello"": {
""request"": [ { ""name"": ""greeting"", ""type"": ""string"" } ],
""response"": ""string""
}
}
}
");
GenerateCode();
var types = GetTypes();
bool hasGreeterCode = types.TryGetValue("Greeter", out string greeterCode);
Assert.That(hasGreeterCode);

Assert.That(Regex.Matches(greeterCode, @"(?m)^\t\t\tswitch\(messageName\)\r?$").Count, Is.EqualTo(1));
Assert.That(Regex.IsMatch(greeterCode, @"break;\s*\}\s*;"), Is.False);
}

[Test]
public void ProtocolWithoutMessagesShouldGenerateEmptyRequestBody()
{
AddProtocol(@"
{
""protocol"": ""Silent"",
""namespace"": ""Avro.Test.CodeGen.EmptyProtocolRegression"",
""types"": [],
""messages"": {}
}
");
GenerateCode();
var types = GetTypes();
bool hasSilentCode = types.TryGetValue("Silent", out string silentCode);
Assert.That(hasSilentCode);

Assert.That(Regex.IsMatch(silentCode, @"object callback\)\s*\{\s*\}"), Is.True);
}

[Test]
public void EnumWithKeywordSymbolsShouldHavePrefixedSymbols()
{
Expand Down
Loading