This is an automated email from the ASF dual-hosted git repository.
rskraba pushed a commit to branch branch-1.11
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/branch-1.11 by this push:
new aa450f35c AVRO-3756: [csharp] Add a method to return types instead of
writing them to disk (#2215)
aa450f35c is described below
commit aa450f35caf4ed209018bf3a70fb9352989b936c
Author: Alex Rosenfeld <[email protected]>
AuthorDate: Wed Jun 14 11:19:29 2023 -0400
AVRO-3756: [csharp] Add a method to return types instead of writing them to
disk (#2215)
* AVRO-3756 add method to return types to user instead of writing to disk
* add using block
* add a unit test
* appease the linter
---
lang/csharp/src/apache/main/CodeGen/CodeGen.cs | 44 +++++++++++++++++++++++
lang/csharp/src/apache/test/CodGen/CodeGenTest.cs | 28 +++++++++++++++
2 files changed, 72 insertions(+)
diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
index e579d8bb0..7e7936272 100644
--- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
+++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
@@ -1136,6 +1136,50 @@ namespace Avro
}
}
+ /// <summary>
+ /// Gets names and generated code of the schema(s) types
+ /// </summary>
+ /// <returns></returns>
+ public virtual IDictionary<string, string> GetTypes()
+ {
+ using (var cscp = new CSharpCodeProvider())
+ {
+ var opts = new CodeGeneratorOptions
+ {
+ BracingStyle = "C", IndentString = "\t",
BlankLinesBetweenMembers = false
+ };
+ CodeNamespaceCollection nsc = CompileUnit.Namespaces;
+
+ var sourceCodeByName = new Dictionary<string, string>();
+ for (int i = 0; i < nsc.Count; i++)
+ {
+ var ns = nsc[i];
+
+ var new_ns = new CodeNamespace(ns.Name);
+ new_ns.Comments.Add(CodeGenUtil.Instance.FileComment);
+ foreach (CodeNamespaceImport nci in
CodeGenUtil.Instance.NamespaceImports)
+ {
+ new_ns.Imports.Add(nci);
+ }
+
+ var types = ns.Types;
+ for (int j = 0; j < types.Count; j++)
+ {
+ var ctd = types[j];
+ using (var writer = new StringWriter())
+ {
+ new_ns.Types.Add(ctd);
+ cscp.GenerateCodeFromNamespace(new_ns, writer,
opts);
+ new_ns.Types.Remove(ctd);
+ sourceCodeByName[ctd.Name] = writer.ToString();
+ }
+ }
+ }
+
+ return sourceCodeByName;
+ }
+ }
+
/// <summary>
/// Writes each types in each namespaces into individual files.
/// </summary>
diff --git a/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
b/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
index e51434720..f8eef4a9a 100644
--- a/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
+++ b/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
@@ -18,6 +18,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.CSharp;
using NUnit.Framework;
@@ -81,6 +82,33 @@ namespace Avro.Test.CodeGen
Protocol protocol = null;
Assert.Throws<ArgumentNullException>(() =>
this.GenerateNames(protocol));
}
+
+
+ [Test]
+ public void GetTypesShouldReturnTypes()
+ {
+ AddSchema(@"
+{
+ ""name"": ""PlanetEnum"",
+ ""namespace"": ""Space.Models"",
+ ""type"": ""enum"",
+ ""symbols"": [
+ ""Earth"",
+ ""Mars"",
+ ""Jupiter"",
+ ""Saturn"",
+ ""Uranus"",
+ ""Neptune""
+ ]
+}
+");
+ GenerateCode();
+ var types = GetTypes();
+ Assert.That(types.Count, Is.EqualTo(1));
+ bool hasPlanetEnumCode = types.TryGetValue("PlanetEnum", out
string planetEnumCode);
+ Assert.That(hasPlanetEnumCode);
+ Assert.That(Regex.Matches(planetEnumCode, "public enum
PlanetEnum").Count, Is.EqualTo(1));
+ }
}
}
}