Skip to content
Closed
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: 0 additions & 1 deletion lang/csharp/src/apache/codegen/Avro.codegen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
<!-- See lang/csharp/README.md for tool and library dependency update strategy -->
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" />
<PackageReference Include="System.CodeDom" Version="$(SystemCodeDomVersion)" />
<PackageReference Include="System.Reflection" Version="$(SystemReflectionVersion)" />
<PackageReference Include="System.Reflection.Emit.ILGeneration" Version="$(SystemReflectionEmitILGenerationVersion)" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="$(SystemReflectionEmitLightweightVersion)" />
Expand Down
86 changes: 22 additions & 64 deletions lang/csharp/src/apache/codegen/AvroGen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand All @@ -17,13 +17,13 @@
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Avro
{
class AvroGen
public class AvroGenTool
{
static int Main(string[] args)
public static int Main(string[] args)
{
// Print usage if no arguments provided
if (args.Length == 0)
Expand All @@ -33,7 +33,7 @@ static int Main(string[] args)
}

// Print usage if help requested
if (args[0] == "-h" || args[0] == "--help")
if (args.Contains("-h") || args.Contains("--help"))
{
Usage();
return 0;
Expand Down Expand Up @@ -102,7 +102,6 @@ static int Main(string[] args)

// Ensure we got all the command line arguments we need
bool isValid = true;
int rc = 0;
if (!isProtocol.HasValue || inputFile == null)
{
Console.Error.WriteLine("Must provide either '-p <protocolfile>' or '-s <schemafile>'");
Expand All @@ -114,21 +113,30 @@ static int Main(string[] args)
isValid = false;
}


if (!isValid)
{
Usage();
rc = 1;
return 1;
}

try
{
// Generate code
if (isProtocol.Value)
AvroGen.GenerateProtocolFromFile(inputFile, outputDir, namespaceMapping);
else
AvroGen.GenerateSchemaFromFile(inputFile, outputDir, namespaceMapping);
}
catch (Exception ex)
{
Console.Error.WriteLine("Exception occurred. " + ex.Message);
return 1;
}
else if (isProtocol.Value)
rc = GenProtocol(inputFile, outputDir, namespaceMapping);
else
rc = GenSchema(inputFile, outputDir, namespaceMapping);

return rc;
return 0;
}

static void Usage()
private static void Usage()
{
Console.WriteLine("{0}\n\n" +
"Usage:\n" +
Expand All @@ -142,55 +150,5 @@ static void Usage()
AppDomain.CurrentDomain.FriendlyName);
return;
}
static int GenProtocol(string infile, string outdir,
IEnumerable<KeyValuePair<string, string>> namespaceMapping)
{
try
{
string text = System.IO.File.ReadAllText(infile);
Protocol protocol = Protocol.Parse(text);

CodeGen codegen = new CodeGen();
codegen.AddProtocol(protocol);

foreach (var entry in namespaceMapping)
codegen.NamespaceMapping[entry.Key] = entry.Value;

codegen.GenerateCode();
codegen.WriteTypes(outdir);
}
catch (Exception ex)
{
Console.Error.WriteLine("Exception occurred. " + ex.Message);
return 1;
}

return 0;
}
static int GenSchema(string infile, string outdir,
IEnumerable<KeyValuePair<string, string>> namespaceMapping)
{
try
{
string text = System.IO.File.ReadAllText(infile);
Schema schema = Schema.Parse(text);

CodeGen codegen = new CodeGen();
codegen.AddSchema(schema);

foreach (var entry in namespaceMapping)
codegen.NamespaceMapping[entry.Key] = entry.Value;

codegen.GenerateCode();
codegen.WriteTypes(outdir);
}
catch (Exception ex)
{
Console.Error.WriteLine("Exception occurred. " + ex.Message);
return 1;
}

return 0;
}
}
}
135 changes: 135 additions & 0 deletions lang/csharp/src/apache/main/CodeGen/AvroGen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Avro
{
/// <summary>
/// Generates C# code from Avro schemas and protocols.
/// </summary>
public static class AvroGen
{
/// <summary>
/// Generate files for protocol.
/// </summary>
/// <param name="input">input protocol definition.</param>
/// <param name="outputDir">output directory for generated files.</param>
/// <param name="namespaceMapping">namespace mappings object.</param>
public static void GenerateProtocol(string input, string outputDir, IEnumerable<KeyValuePair<string, string>> namespaceMapping = null)
{
if (namespaceMapping != null)
input = ReplaceMappedNamespacesInSchema(input, namespaceMapping);

Protocol protocol = Protocol.Parse(input);

CodeGen codegen = new CodeGen();
codegen.AddProtocol(protocol);

codegen.GenerateCode();
codegen.WriteTypes(outputDir);
}

/// <summary>
/// Generate files for protocol.
/// </summary>
/// <param name="inputFile">input protocol definition file.</param>
/// <param name="outputDir">output directory for generated files.</param>
/// <param name="namespaceMapping">namespace mappings object.</param>
public static void GenerateProtocolFromFile(string inputFile, string outputDir, IEnumerable<KeyValuePair<string, string>> namespaceMapping = null)
{
string text = System.IO.File.ReadAllText(inputFile);
GenerateProtocol(text, outputDir, namespaceMapping);
}

/// <summary>
/// Generate files for schema.
/// </summary>
/// <param name="input">input schema definition.</param>
/// <param name="outputDir">output directory for generated files.</param>
/// <param name="namespaceMapping">namespace mappings object.</param>
public static void GenerateSchema(string input, string outputDir, IEnumerable<KeyValuePair<string, string>> namespaceMapping = null)
{
if (namespaceMapping != null)
input = ReplaceMappedNamespacesInSchema(input, namespaceMapping);

Schema schema = Schema.Parse(input);

CodeGen codegen = new CodeGen();
codegen.AddSchema(schema);

codegen.GenerateCode();
codegen.WriteTypes(outputDir);
}

/// <summary>
/// Generate files for schema.
/// </summary>
/// <param name="inputFile">input schema definition file.</param>
/// <param name="outputDir">output directory for generated files.</param>
/// <param name="namespaceMapping">namespace mappings object.</param>
public static void GenerateSchemaFromFile(string inputFile, string outputDir, IEnumerable<KeyValuePair<string, string>> namespaceMapping = null)
{
string text = System.IO.File.ReadAllText(inputFile);
GenerateSchema(text, outputDir, namespaceMapping);
}

/// <summary>
/// Replace namespace(s) in schema or protocol definition.
/// </summary>
/// <param name="input">input schema or protocol definition.</param>
/// <param name="namespaceMapping">namespace mappings object.</param>
public static string ReplaceMappedNamespacesInSchema(string input, IEnumerable<KeyValuePair<string, string>> namespaceMapping)
{
if (namespaceMapping == null || input == null)
return input;

// Replace namespace in "namespace" definitions:
// "namespace": "originalnamespace" -> "namespace": "mappednamespace"
// "namespace": "originalnamespace.whatever" -> "namespace": "mappednamespace.whatever"
// Note: It keeps the original whitespaces
return Regex.Replace(input, @"""namespace""(\s*):(\s*)""([^""]*)""", m =>
{
// m.Groups[1]: whitespaces before ':'
// m.Groups[2]: whitespaces after ':'
// m.Groups[3]: the namespace

string ns = m.Groups[3].Value;

foreach (var mapping in namespaceMapping)
{
// Full match
if (mapping.Key == ns)
{
ns = mapping.Value;
break;
}
else
// Partial match
if (ns.StartsWith($"{mapping.Key}."))
{
ns = $"{mapping.Value}.{ns.Substring(mapping.Key.Length + 1)}";
break;
}
}
return $@"""namespace""{m.Groups[1].Value}:{m.Groups[2].Value}""{ns}""";
});
}
}
}
15 changes: 2 additions & 13 deletions lang/csharp/src/apache/main/CodeGen/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ public class CodeGen
/// </value>
public IList<Protocol> Protocols { get; private set; }

/// <summary>
/// Gets mapping of Avro namespaces to C# namespaces.
/// </summary>
/// <value>
/// The namespace mapping.
/// </value>
public IDictionary<string, string> NamespaceMapping { get; private set; }

/// <summary>
/// Gets list of generated namespaces.
/// </summary>
Expand All @@ -80,7 +72,6 @@ public CodeGen()
{
Schemas = new List<Schema>();
Protocols = new List<Protocol>();
NamespaceMapping = new Dictionary<string, string>();
NamespaceLookup = new Dictionary<string, CodeNamespace>(StringComparer.Ordinal);
}

Expand All @@ -97,7 +88,7 @@ public CodeGen(Dictionary<string, CodeNamespace> namespaceLookup)
/// <summary>
/// Adds a protocol object to generate code for.
/// </summary>
/// <param name="protocol">The protocol.</param>
/// <param name="protocol">protocol object.</param>
public virtual void AddProtocol(Protocol protocol)
{
Protocols.Add(protocol);
Expand Down Expand Up @@ -129,9 +120,7 @@ protected virtual CodeNamespace AddNamespace(string name)

if (!NamespaceLookup.TryGetValue(name, out CodeNamespace ns))
{
ns = NamespaceMapping.TryGetValue(name, out string csharpNamespace)
? new CodeNamespace(csharpNamespace)
: new CodeNamespace(CodeGenUtil.Instance.Mangle(name));
ns = new CodeNamespace(CodeGenUtil.Instance.Mangle(name));

foreach (CodeNamespaceImport nci in CodeGenUtil.Instance.NamespaceImports)
{
Expand Down
8 changes: 4 additions & 4 deletions lang/csharp/src/apache/test/Avro.test.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
Expand Down Expand Up @@ -35,14 +35,14 @@
<PackageReference Include="NUnit" Version="$(NUnitVersion)" />
<PackageReference Include="NUnit3TestAdapter" Version="$(NUnit3TestAdapterVersion)" />
<PackageReference Include="NUnit.ConsoleRunner" Version="$(NUnitConsoleRunnerVersion)" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisCSharpVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\main\Avro.main.csproj" />
<ProjectReference Include="..\codegen\Avro.codegen.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading