Author: cutting
Date: Wed Apr 20 21:52:10 2011
New Revision: 1095529
URL: http://svn.apache.org/viewvc?rev=1095529&view=rev
Log:
AVRO-807. C#: Some improvements and bugfixes. Contributed by Dona Alvarez.
Added:
avro/trunk/lang/csharp/src/apache/codegen/AvroGen.cs
- copied, changed from r1095524, avro/trunk/lang/csharp/src/apache/codegen/Program.cs
avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
- copied, changed from r1095524, avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs
Removed:
avro/trunk/lang/csharp/src/apache/codegen/Program.cs
avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs
Modified:
avro/trunk/CHANGES.txt
avro/trunk/lang/csharp/src/apache/codegen/Avro.codegen.csproj
avro/trunk/lang/csharp/src/apache/main/Avro.main.csproj
avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
avro/trunk/lang/csharp/src/apache/main/Schema/NamedSchema.cs
avro/trunk/lang/csharp/src/apache/main/Specific/ObjectCreator.cs
avro/trunk/lang/csharp/src/apache/main/Specific/SpecificReader.cs
avro/trunk/lang/csharp/src/apache/main/Specific/SpecificRecord.cs
avro/trunk/lang/csharp/src/apache/main/Specific/SpecificWriter.cs
avro/trunk/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
avro/trunk/lang/csharp/src/apache/test/Specific/SpecificTests.cs
Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Apr 20 21:52:10 2011
@@ -55,6 +55,10 @@ Avro 1.5.1 (unreleased)
AVRO-763. Java MapReduce API: add support for configure() and
close() methods to mappers and reducers. (Marshall Pierce via cutting)
+ AVRO-807. C#: Some improvements and bugfixes, including making AvroGen
+ extensible and changing ISpecificRecord to an interface.
+ (Dona Alvarez via cutting)
+
BUG FIXES
AVRO-786. Java: Fix equals() to work on objects containing maps. (cutting)
Modified: avro/trunk/lang/csharp/src/apache/codegen/Avro.codegen.csproj
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/codegen/Avro.codegen.csproj?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/codegen/Avro.codegen.csproj (original)
+++ avro/trunk/lang/csharp/src/apache/codegen/Avro.codegen.csproj Wed Apr 20 21:52:10 2011
@@ -117,7 +117,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="Program.cs" />
+ <Compile Include="AvroGen.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Copied: avro/trunk/lang/csharp/src/apache/codegen/AvroGen.cs (from r1095524, avro/trunk/lang/csharp/src/apache/codegen/Program.cs)
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/codegen/AvroGen.cs?p2=avro/trunk/lang/csharp/src/apache/codegen/AvroGen.cs&p1=avro/trunk/lang/csharp/src/apache/codegen/Program.cs&r1=1095524&r2=1095529&rev=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/codegen/Program.cs (original)
+++ avro/trunk/lang/csharp/src/apache/codegen/AvroGen.cs Wed Apr 20 21:52:10 2011
@@ -19,11 +19,10 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
-using Avro.codegen;
-namespace Avro.codegen
+namespace Avro
{
- class Program
+ class AvroGen
{
static void Main(string[] args)
{
@@ -45,7 +44,6 @@ namespace Avro.codegen
Console.WriteLine("Usage:\navrogen -p <protocolfile> <outputdir>\navrogen
-s <schemafile> <outputdir>");
return;
}
-
static void GenProtocol(string infile, string outdir)
{
try
@@ -64,7 +62,6 @@ namespace Avro.codegen
Console.WriteLine("Exception occurred. " + ex.Message);
}
}
-
static void GenSchema(string infile, string outdir)
{
try
@@ -83,6 +80,5 @@ namespace Avro.codegen
Console.WriteLine("Exception occurred. " + ex.Message);
}
}
-
}
}
Modified: avro/trunk/lang/csharp/src/apache/main/Avro.main.csproj
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/Avro.main.csproj?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/main/Avro.main.csproj (original)
+++ avro/trunk/lang/csharp/src/apache/main/Avro.main.csproj Wed Apr 20 21:52:10 2011
@@ -82,7 +82,7 @@
<Compile Include="CodeGen\AvroRuntimeException.cs" />
<Compile Include="CodeGen\CodeGen.cs" />
<Compile Include="CodeGen\CodeGenException.cs" />
- <Compile Include="CodeGen\CodeGenExt.cs" />
+ <Compile Include="CodeGen\CodeGenUtil.cs" />
<Compile Include="Generic\DatumReader.cs" />
<Compile Include="Generic\DatumWriter.cs" />
<Compile Include="Generic\GenericEnum.cs" />
Modified: avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGen.cs
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGen.cs?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGen.cs (original)
+++ avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGen.cs Wed Apr 20 21:52:10 2011
@@ -27,11 +27,25 @@ namespace Avro
{
public class CodeGen
{
+ /// <summary>
+ /// Object that contains all the generated types
+ /// </summary>
public CodeCompileUnit CompileUnit { get; private set; }
+
+ /// <summary>
+ /// List of schemas to generate code for
+ /// </summary>
public IList<Schema> Schemas { get; private set; }
+
+ /// <summary>
+ /// List of protocols to generate code for
+ /// </summary>
public IList<Protocol> Protocols { get; private set; }
- private Dictionary<string, CodeNamespace> namespaceLookup = new Dictionary<string,
CodeNamespace>(StringComparer.Ordinal);
- private CodeGenExt codeGenExt;
+
+ /// <summary>
+ /// List of generated namespaces
+ /// </summary>
+ protected Dictionary<string, CodeNamespace> namespaceLookup = new Dictionary<string,
CodeNamespace>(StringComparer.Ordinal);
/// <summary>
/// Default constructor
@@ -40,14 +54,13 @@ namespace Avro
{
this.Schemas = new List<Schema>();
this.Protocols = new List<Protocol>();
- this.codeGenExt = CodeGenExt.Instance;
}
/// <summary>
/// Adds a protocol object to generate code for
/// </summary>
/// <param name="protocol">protocol object</param>
- public void AddProtocol(Protocol protocol)
+ public virtual void AddProtocol(Protocol protocol)
{
Protocols.Add(protocol);
}
@@ -56,7 +69,7 @@ namespace Avro
/// Adds a schema object to generate code for
/// </summary>
/// <param name="schema">schema object</param>
- public void AddSchema(Schema schema)
+ public virtual void AddSchema(Schema schema)
{
Schemas.Add(schema);
}
@@ -66,7 +79,7 @@ namespace Avro
/// </summary>
/// <param name="name">name of namespace</param>
/// <returns></returns>
- private CodeNamespace addNamespace(string name)
+ protected virtual CodeNamespace addNamespace(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name", "name cannot be null.");
@@ -75,8 +88,8 @@ namespace Avro
if (!namespaceLookup.TryGetValue(name, out ns))
{
- ns = new CodeNamespace(codeGenExt.Mangle(name));
- foreach (CodeNamespaceImport nci in codeGenExt.NamespaceImports)
+ ns = new CodeNamespace(CodeGenUtil.Instance.Mangle(name));
+ foreach (CodeNamespaceImport nci in CodeGenUtil.Instance.NamespaceImports)
ns.Imports.Add(nci);
CompileUnit.Namespaces.Add(ns);
@@ -89,7 +102,7 @@ namespace Avro
/// Generates code for the given protocol and schema objects
/// </summary>
/// <returns>CodeCompileUnit object</returns>
- public CodeCompileUnit GenerateCode()
+ public virtual CodeCompileUnit GenerateCode()
{
CompileUnit = new CodeCompileUnit();
@@ -102,7 +115,7 @@ namespace Avro
/// <summary>
/// Generates code for the schema objects
/// </summary>
- private void processSchemas()
+ protected virtual void processSchemas()
{
foreach (Schema schema in this.Schemas)
{
@@ -125,7 +138,7 @@ namespace Avro
/// <summary>
/// Generates code for the protocol objects
/// </summary>
- private void processProtocols()
+ protected virtual void processProtocols()
{
foreach (Protocol protocol in Protocols)
{
@@ -150,7 +163,7 @@ namespace Avro
/// </summary>
/// <param name="protocol">protocol to process</param>
/// <returns></returns>
- private SchemaNames generateNames(Protocol protocol)
+ protected virtual SchemaNames generateNames(Protocol protocol)
{
var names = new SchemaNames();
foreach (Schema schema in protocol.Types)
@@ -163,7 +176,7 @@ namespace Avro
/// </summary>
/// <param name="schema">schema to process</param>
/// <returns></returns>
- private SchemaNames generateNames(Schema schema)
+ protected virtual SchemaNames generateNames(Schema schema)
{
var names = new SchemaNames();
addName(schema, names);
@@ -175,7 +188,7 @@ namespace Avro
/// </summary>
/// <param name="schema">schema object to search</param>
/// <param name="names">list of named schemas</param>
- private void addName(Schema schema, SchemaNames names)
+ protected virtual void addName(Schema schema, SchemaNames names)
{
NamedSchema ns = schema as NamedSchema;
if (null != ns) if (names.Contains(ns.SchemaName)) return;
@@ -223,7 +236,6 @@ namespace Avro
default:
throw new CodeGenException("Unable to add name for " + schema.Name +
" type " + schema.Tag);
-
}
}
@@ -232,20 +244,20 @@ namespace Avro
/// </summary>
/// <param name="schema">fixed schema</param>
/// <param name="ns">namespace object</param>
- private void processFixed(Schema schema)
+ protected virtual void processFixed(Schema schema)
{
FixedSchema fixedSchema = schema as FixedSchema;
if (null == fixedSchema) throw new CodeGenException("Unable to cast schema into
a fixed");
CodeTypeDeclaration ctd = new CodeTypeDeclaration();
- ctd.Name = codeGenExt.Mangle(fixedSchema.Name);
+ ctd.Name = CodeGenUtil.Instance.Mangle(fixedSchema.Name);
ctd.IsClass = true;
ctd.IsPartial = true;
ctd.Attributes = MemberAttributes.Public;
ctd.BaseTypes.Add("SpecificFixed");
// create static schema field
- createSchemaField(schema, ctd);
+ createSchemaField(schema, ctd, true);
// Add Size field
string sizefname = "fixedSize";
@@ -282,18 +294,18 @@ namespace Avro
/// </summary>
/// <param name="schema">enum schema</param>
/// <param name="ns">namespace</param>
- private void processEnum(Schema schema)
+ protected virtual void processEnum(Schema schema)
{
EnumSchema enumschema = schema as EnumSchema;
if (null == enumschema) throw new CodeGenException("Unable to cast schema into
an enum");
- CodeTypeDeclaration ctd = new CodeTypeDeclaration(codeGenExt.Mangle(enumschema.Name));
+ CodeTypeDeclaration ctd = new CodeTypeDeclaration(CodeGenUtil.Instance.Mangle(enumschema.Name));
ctd.IsEnum = true;
ctd.Attributes = MemberAttributes.Public;
foreach (string symbol in enumschema.Symbols)
{
- if (codeGenExt.ReservedKeywords.Contains(symbol))
+ if (CodeGenUtil.Instance.ReservedKeywords.Contains(symbol))
throw new CodeGenException("Enum symbol " + symbol + " is a C# reserved
keyword");
CodeMemberField field = new CodeMemberField(typeof(int), symbol);
ctd.Members.Add(field);
@@ -313,24 +325,24 @@ namespace Avro
/// <param name="schema">record schema</param>
/// <param name="ns">namespace</param>
/// <returns></returns>
- private CodeTypeDeclaration processRecord(Schema schema)
+ protected virtual CodeTypeDeclaration processRecord(Schema schema)
{
RecordSchema recordSchema = schema as RecordSchema;
if (null == recordSchema) throw new CodeGenException("Unable to cast schema into
a record");
// declare the class
- var ctd = new CodeTypeDeclaration(codeGenExt.Mangle(recordSchema.Name));
- ctd.BaseTypes.Add("SpecificRecord");
+ var ctd = new CodeTypeDeclaration(CodeGenUtil.Instance.Mangle(recordSchema.Name));
+ ctd.BaseTypes.Add("ISpecificRecord");
ctd.Attributes = MemberAttributes.Public;
ctd.IsClass = true;
ctd.IsPartial = true;
- createSchemaField(schema, ctd);
+ createSchemaField(schema, ctd, false);
// declare Get() to be used by the Writer classes
var cmmGet = new CodeMemberMethod();
cmmGet.Name = "Get";
- cmmGet.Attributes = MemberAttributes.Public | MemberAttributes.Override;
+ cmmGet.Attributes = MemberAttributes.Public;
cmmGet.ReturnType = new CodeTypeReference("System.Object");
cmmGet.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
StringBuilder getFieldStmt = new StringBuilder("switch (fieldPos)\n\t\t\t{\n");
@@ -338,7 +350,7 @@ namespace Avro
// declare Put() to be used by the Reader classes
var cmmPut = new CodeMemberMethod();
cmmPut.Name = "Put";
- cmmPut.Attributes = MemberAttributes.Public | MemberAttributes.Override;
+ cmmPut.Attributes = MemberAttributes.Public;
cmmPut.ReturnType = new CodeTypeReference(typeof(void));
cmmPut.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "fieldPos"));
cmmPut.Parameters.Add(new CodeParameterDeclarationExpression("System.Object",
"fieldValue"));
@@ -348,7 +360,7 @@ namespace Avro
{
// Determine type of field
bool nullibleEnum = false;
- string baseType = codeGenExt.Mangle(getType(field.Schema, false, ref nullibleEnum));
+ string baseType = getType(field.Schema, false, ref nullibleEnum);
var ctrfield = new CodeTypeReference(baseType);
// Create field
@@ -370,7 +382,7 @@ namespace Avro
// Create reference to the field - this.fieldname
var fieldRef = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),
privFieldName);
- var mangledName = codeGenExt.Mangle(field.Name);
+ var mangledName = CodeGenUtil.Instance.Mangle(field.Name);
// Create field property with get and set methods
var property = new CodeMemberProperty();
@@ -479,10 +491,10 @@ namespace Avro
throw new CodeGenException("Unable to cast schema into a named schema");
if (nullible)
{
- nullibleEnum = true;
- return "System.Nullable<" + namedSchema.Fullname + ">";
+ nullibleEnum = true;
+ return "System.Nullable<" + CodeGenUtil.Instance.Mangle(namedSchema.Fullname)
+ ">";
}
- else return namedSchema.Fullname;
+ else return CodeGenUtil.Instance.Mangle(namedSchema.Fullname);
case Schema.Type.Fixed:
case Schema.Type.Record:
@@ -490,7 +502,7 @@ namespace Avro
namedSchema = schema as NamedSchema;
if (null == namedSchema)
throw new CodeGenException("Unable to cast schema into a named schema");
- return namedSchema.Fullname;
+ return CodeGenUtil.Instance.Mangle(namedSchema.Fullname);
case Schema.Type.Array:
var arraySchema = schema as ArraySchema;
@@ -511,7 +523,7 @@ namespace Avro
throw new CodeGenException("Unable to cast schema into a union schema");
Schema nullibleType = getNullableType(unionSchema);
if (null == nullibleType)
- return "System.Object";
+ return CodeGenUtil.Object;
else
return getType(nullibleType, true, ref nullibleEnum);
}
@@ -547,7 +559,7 @@ namespace Avro
/// </summary>
/// <param name="schema">schema</param>
/// <param name="ctd">CodeTypeDeclaration for the class</param>
- private void createSchemaField(Schema schema, CodeTypeDeclaration ctd)
+ protected virtual void createSchemaField(Schema schema, CodeTypeDeclaration ctd,
bool overrideFlag)
{
// create schema field
var ctrfield = new CodeTypeReference("Schema");
@@ -564,10 +576,12 @@ namespace Avro
// create property to get static schema field
var property = new CodeMemberProperty();
- property.Attributes = MemberAttributes.Public | MemberAttributes.Override;
+ property.Attributes = MemberAttributes.Public;
+ if (overrideFlag) property.Attributes |= MemberAttributes.Override;
property.Name = "Schema";
property.Type = ctrfield;
- property.GetStatements.Add(new CodeMethodReturnStatement(new CodeTypeReferenceExpression(schema.Name
+ "." + schemaFname)));
+
+ property.GetStatements.Add(new CodeMethodReturnStatement(new CodeTypeReferenceExpression(ctd.Name
+ "." + schemaFname)));
ctd.Members.Add(property);
}
@@ -576,7 +590,7 @@ namespace Avro
/// </summary>
/// <param name="comment">comment</param>
/// <returns>CodeCommentStatement object</returns>
- private CodeCommentStatement createDocComment(string comment)
+ protected virtual CodeCommentStatement createDocComment(string comment)
{
string text = string.Format("<summary>\r\n {0}\r\n </summary>", comment);
return new CodeCommentStatement(text, true);
@@ -586,7 +600,7 @@ namespace Avro
/// Writes the generated compile unit into one file
/// </summary>
/// <param name="outputFile">name of output file to write to</param>
- public void WriteCompileUnit(string outputFile)
+ public virtual void WriteCompileUnit(string outputFile)
{
var cscp = new CSharpCodeProvider();
@@ -605,7 +619,7 @@ namespace Avro
/// Writes each types in each namespaces into individual files
/// </summary>
/// <param name="outputdir">name of directory to write to</param>
- public void WriteTypes(string outputdir)
+ public virtual void WriteTypes(string outputdir)
{
var cscp = new CSharpCodeProvider();
@@ -619,19 +633,19 @@ namespace Avro
{
var ns = nsc[i];
- string dir = outputdir + "\\" + codeGenExt.UnMangle(ns.Name).Replace('.',
'\\');
+ string dir = outputdir + "\\" + CodeGenUtil.Instance.UnMangle(ns.Name).Replace('.',
'\\');
Directory.CreateDirectory(dir);
var new_ns = new CodeNamespace(ns.Name);
- new_ns.Comments.Add(this.codeGenExt.FileComment);
- foreach (CodeNamespaceImport nci in this.codeGenExt.NamespaceImports)
+ 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];
- string file = dir + "\\" + codeGenExt.UnMangle(ctd.Name) + ".cs";
+ string file = dir + "\\" + CodeGenUtil.Instance.UnMangle(ctd.Name) +
".cs";
using (var writer = new StreamWriter(file, false))
{
new_ns.Types.Add(ctd);
Copied: avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs (from r1095524, avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs)
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs?p2=avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs&p1=avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs&r1=1095524&r2=1095529&rev=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs (original)
+++ avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs Wed Apr 20 21:52:10 2011
@@ -26,18 +26,19 @@ namespace Avro
/// <summary>
/// A singleton class containing data used by codegen
/// </summary>
- public sealed class CodeGenExt
+ public sealed class CodeGenUtil
{
- private static readonly CodeGenExt instance = new CodeGenExt();
- public static CodeGenExt Instance { get { return instance; } }
+ private static readonly CodeGenUtil instance = new CodeGenUtil();
+ public static CodeGenUtil Instance { get { return instance; } }
public CodeNamespaceImport[] NamespaceImports { get; private set; }
public CodeCommentStatement FileComment { get; private set; }
public HashSet<string> ReservedKeywords { get; private set; }
- private const char AT = '@';
- private const char DOT = '.';
+ private const char At = '@';
+ private const char Dot = '.';
+ public const string Object = "System.Object";
- private CodeGenExt()
+ private CodeGenUtil()
{
NamespaceImports = new CodeNamespaceImport[] {
new CodeNamespaceImport("System"),
@@ -75,13 +76,13 @@ namespace Avro
public string Mangle(string name)
{
var builder = new StringBuilder();
- string[] names = name.Split(DOT);
+ string[] names = name.Split(Dot);
for (int i = 0; i < names.Length; ++i)
{
if (ReservedKeywords.Contains(names[i]))
- builder.Append(AT);
+ builder.Append(At);
builder.Append(names[i]);
- builder.Append(DOT);
+ builder.Append(Dot);
}
builder.Remove(builder.Length - 1, 1);
return builder.ToString();
@@ -96,7 +97,7 @@ namespace Avro
{
var builder = new StringBuilder(name.Length);
for (int i = 0; i < name.Length; ++i)
- if (name[i] != AT)
+ if (name[i] != At)
builder.Append(name[i]);
return builder.ToString();
}
Modified: avro/trunk/lang/csharp/src/apache/main/Schema/NamedSchema.cs
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/Schema/NamedSchema.cs?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/main/Schema/NamedSchema.cs (original)
+++ avro/trunk/lang/csharp/src/apache/main/Schema/NamedSchema.cs Wed Apr 20 21:52:10 2011
@@ -62,17 +62,6 @@ namespace Avro
/// </summary>
private readonly IList<SchemaName> aliases;
- /*
- /// <summary>
- /// Returns the SchemaName object of the named schema
- /// </summary>
- /// <returns></returns>
- public SchemaName GetName()
- {
- return schemaName;
- }
- */
-
/// <summary>
/// Static function to return a new instance of the named schema
/// </summary>
@@ -112,14 +101,8 @@ namespace Avro
{
this.SchemaName = name;
this.aliases = aliases;
- try
- {
- names.Add(name, this);
- }
- catch (Exception ex)
- {
- throw new AvroException("Could not add " + name.Fullname + ". " + ex.Message);
- }
+ if (!names.Add(name, this))
+ throw new AvroException("Duplicate schema name " + name.Fullname);
}
/// <summary>
Modified: avro/trunk/lang/csharp/src/apache/main/Specific/ObjectCreator.cs
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/Specific/ObjectCreator.cs?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/main/Specific/ObjectCreator.cs (original)
+++ avro/trunk/lang/csharp/src/apache/main/Specific/ObjectCreator.cs Wed Apr 20 21:52:10 2011
@@ -45,7 +45,7 @@ namespace Avro.Specific
private readonly Type[] margs;
private readonly Type[] largs;
- private delegate object CtorDelegate();
+ public delegate object CtorDelegate();
private Type ctorType = typeof(CtorDelegate);
Dictionary<NameCtorKey, CtorDelegate> ctors;
@@ -104,6 +104,74 @@ namespace Avro.Specific
}
/// <summary>
+ /// Gets the type of the specified type name
+ /// </summary>
+ /// <param name="name">name of the object to get type of</param>
+ /// <param name="schemaType">schema type for the object</param>
+ /// <returns>Type</returns>
+ public Type GetType(string name, Schema.Type schemaType)
+ {
+ Type type;
+ if (diffAssembly)
+ {
+ // entry assembly different from current assembly, try entry assembly first
+ type = entryAssembly.GetType(name);
+ if (type == null) // now try current assembly and mscorlib
+ type = Type.GetType(name);
+ }
+ else
+ type = Type.GetType(name);
+
+ if (type == null) // type is still not found, need to loop through all loaded
assemblies
+ {
+ Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
+ foreach (Assembly assembly in assemblies)
+ {
+ type = assembly.GetType(name);
+ if (type != null)
+ break;
+ }
+ }
+ if (type == null)
+ throw new AvroException("Unable to find type " + name + " in all loaded assemblies");
+
+ if (schemaType == Schema.Type.Map)
+ {
+ margs[1] = type;
+ type = GenericMapType.MakeGenericType(margs);
+ }
+ else if (schemaType == Schema.Type.Array)
+ {
+ largs[0] = type;
+ type = GenericListType.MakeGenericType(largs);
+ }
+
+ return type;
+ }
+
+ /// <summary>
+ /// Gets the default constructor for the specified type
+ /// </summary>
+ /// <param name="name">name of object for the type</param>
+ /// <param name="schemaType">schema type for the object</param>
+ /// <param name="type">type of the object</param>
+ /// <returns>Default constructor for the type</returns>
+ public CtorDelegate GetConstructor(string name, Schema.Type schemaType, Type type)
+ {
+ ConstructorInfo ctorInfo = type.GetConstructor(Type.EmptyTypes);
+ if (ctorInfo == null)
+ throw new AvroException("Class " + name + " has no default constructor");
+
+ DynamicMethod dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + name, typeof(object),
null, type, true);
+ ILGenerator ilGen = dynMethod.GetILGenerator();
+ ilGen.Emit(OpCodes.Nop);
+ ilGen.Emit(OpCodes.Newobj, ctorInfo);
+ ilGen.Emit(OpCodes.Ret);
+
+ return (CtorDelegate)dynMethod.CreateDelegate(ctorType);
+ }
+
+ /// <summary>
/// Creates new instance of the given type
/// </summary>
/// <param name="name">fully qualified name of the type</param>
@@ -112,51 +180,13 @@ namespace Avro.Specific
public object New(string name, Schema.Type schemaType)
{
NameCtorKey key = new NameCtorKey(name, schemaType);
+
CtorDelegate ctor;
if (!ctors.TryGetValue(key, out ctor))
{
- Type type;
- if (diffAssembly)
- {
- // entry assembly different from current assembly, try entry assembly
first
- type = entryAssembly.GetType(name);
- if (type == null) // now try current assembly and mscorlib
- type = Type.GetType(name);
- }
- else
- type = Type.GetType(name);
-
- if (type == null) // type is still not found, need to loop through all loaded
assemblies
- {
- Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
- foreach (Assembly assembly in assemblies)
- {
- type = assembly.GetType(name);
- if (type != null)
- break;
- }
- }
- if (type == null)
- throw new AvroException("Unable to find type " + name + " in all loaded
assemblies");
-
- if (schemaType == Schema.Type.Map)
- {
- margs[1] = type;
- type = GenericMapType.MakeGenericType(margs);
- }
- else if (schemaType == Schema.Type.Array)
- {
- largs[0] = type;
- type = GenericListType.MakeGenericType(largs);
- }
-
- DynamicMethod dynMethod = new DynamicMethod("DM$OBJ_FACTORY_" + name, typeof(object),
null, type, true);
- ILGenerator ilGen = dynMethod.GetILGenerator();
- ilGen.Emit(OpCodes.Nop);
- ilGen.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
- ilGen.Emit(OpCodes.Ret);
+ Type type = GetType(name, schemaType);
+ ctor = GetConstructor(name, schemaType, type);
- ctor = (CtorDelegate)dynMethod.CreateDelegate(ctorType);
ctors.Add(key, ctor);
}
return ctor();
Modified: avro/trunk/lang/csharp/src/apache/main/Specific/SpecificReader.cs
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/Specific/SpecificReader.cs?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/main/Specific/SpecificReader.cs (original)
+++ avro/trunk/lang/csharp/src/apache/main/Specific/SpecificReader.cs Wed Apr 20 21:52:10
2011
@@ -38,6 +38,16 @@ namespace Avro.Specific
private readonly SpecificDefaultReader reader;
/// <summary>
+ /// Schema for the writer class
+ /// </summary>
+ public Schema WriterSchema { get { return reader.WriterSchema; } }
+
+ /// <summary>
+ /// Schema for the reader class
+ /// </summary>
+ public Schema ReaderSchema { get { return reader.ReaderSchema; } }
+
+ /// <summary>
/// Constructs a generic reader for the given schemas using the DefaultReader. If
the
/// reader's and writer's schemas are different this class performs the resolution.
/// </summary>
@@ -48,15 +58,10 @@ namespace Avro.Specific
reader = new SpecificDefaultReader(writerSchema, readerSchema);
}
- /// <summary>
- /// Schema for the writer class
- /// </summary>
- public Schema WriterSchema { get { return reader.WriterSchema; } }
-
- /// <summary>
- /// Schema for the reader class
- /// </summary>
- public Schema ReaderSchema { get { return reader.ReaderSchema; } }
+ public SpecificReader(SpecificDefaultReader reader)
+ {
+ this.reader = reader;
+ }
/// <summary>
/// Generic read function
@@ -102,7 +107,7 @@ namespace Avro.Specific
{
RecordSchema rs = (RecordSchema)readerSchema;
- SpecificRecord rec = (reuse != null ? reuse : ObjectCreator.Instance.New(rs.Fullname,
Schema.Type.Record)) as SpecificRecord;
+ ISpecificRecord rec = (reuse != null ? reuse : ObjectCreator.Instance.New(rs.Fullname,
Schema.Type.Record)) as ISpecificRecord;
object obj;
foreach (Field wf in writerSchema)
{
@@ -252,7 +257,7 @@ namespace Avro.Specific
/// <param name="schema">schema containing the type to be determined</param>
/// <param name="nullible">used for union schema</param>
/// <returns></returns>
- private string getTargetType(Schema schema)
+ protected virtual string getTargetType(Schema schema)
{
bool nEnum = false;
string type = Avro.CodeGen.getType(schema, false, ref nEnum);
Modified: avro/trunk/lang/csharp/src/apache/main/Specific/SpecificRecord.cs
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/Specific/SpecificRecord.cs?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/main/Specific/SpecificRecord.cs (original)
+++ avro/trunk/lang/csharp/src/apache/main/Specific/SpecificRecord.cs Wed Apr 20 21:52:10
2011
@@ -23,12 +23,12 @@ using System.Text;
namespace Avro.Specific
{
/// <summary>
- /// Base class for generated classes
+ /// Interface class for generated classes
/// </summary>
- public abstract class SpecificRecord
+ public interface ISpecificRecord
{
- public abstract Schema Schema { get; }
- public abstract object Get(int fieldPos);
- public abstract void Put(int fieldPos, object fieldValue);
+ Schema Schema { get; }
+ object Get(int fieldPos);
+ void Put(int fieldPos, object fieldValue);
}
}
Modified: avro/trunk/lang/csharp/src/apache/main/Specific/SpecificWriter.cs
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/Specific/SpecificWriter.cs?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/main/Specific/SpecificWriter.cs (original)
+++ avro/trunk/lang/csharp/src/apache/main/Specific/SpecificWriter.cs Wed Apr 20 21:52:10
2011
@@ -31,6 +31,7 @@ namespace Avro.Specific
public class SpecificWriter<T> : GenericWriter<T>
{
public SpecificWriter(Schema schema) : base(new SpecificDefaultWriter(schema)) {
}
+ public SpecificWriter(SpecificDefaultWriter writer) : base(writer) { }
}
/// <summary>
@@ -54,9 +55,9 @@ namespace Avro.Specific
protected override void WriteRecord(RecordSchema schema, object value, Encoder encoder)
{
- var rec = value as SpecificRecord;
+ var rec = value as ISpecificRecord;
if (rec == null)
- throw new AvroTypeException("Record object is not derived from SpecificRecord");
+ throw new AvroTypeException("Record object is not derived from ISpecificRecord");
foreach (Field field in schema)
{
@@ -193,7 +194,8 @@ namespace Avro.Specific
case Schema.Type.String:
return obj is string;
case Schema.Type.Record:
- return obj is SpecificRecord && (obj as SpecificRecord).Schema.Name.Equals(sc.Name);
+ return obj is ISpecificRecord &&
+ (((obj as ISpecificRecord).Schema) as RecordSchema).SchemaName.Equals((sc
as RecordSchema).SchemaName);
case Schema.Type.Enumeration:
return obj.GetType().IsEnum && (sc as EnumSchema).Symbols.Contains(obj.ToString());
case Schema.Type.Array:
@@ -203,7 +205,8 @@ namespace Avro.Specific
case Schema.Type.Union:
return false; // Union directly within another union not allowed!
case Schema.Type.Fixed:
- return obj is SpecificFixed && (obj as SpecificFixed).Schema.Name.Equals(sc.Name);
+ return obj is SpecificFixed &&
+ (((obj as SpecificFixed).Schema) as FixedSchema).SchemaName.Equals((sc
as FixedSchema).SchemaName);
default:
throw new AvroException("Unknown schema type: " + sc.Tag);
}
Modified: avro/trunk/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs (original)
+++ avro/trunk/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs Wed Apr 20 21:52:10 2011
@@ -96,7 +96,7 @@ namespace Avro.Test
Assert.AreEqual(0, compres.Errors.Count);
// instantiate object
- SpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[0])
as SpecificRecord;
+ ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[0])
as ISpecificRecord;
Assert.IsNotNull(rec);
// test type of each fields
Modified: avro/trunk/lang/csharp/src/apache/test/Specific/SpecificTests.cs
URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/test/Specific/SpecificTests.cs?rev=1095529&r1=1095528&r2=1095529&view=diff
==============================================================================
--- avro/trunk/lang/csharp/src/apache/test/Specific/SpecificTests.cs (original)
+++ avro/trunk/lang/csharp/src/apache/test/Specific/SpecificTests.cs Wed Apr 20 21:52:10 2011
@@ -166,7 +166,7 @@ namespace Avro.Test
Assert.IsTrue(compres.Errors.Count == 0);
// create record
- SpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1])
as SpecificRecord;
+ ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1])
as ISpecificRecord;
Assert.IsFalse(rec == null);
// serialize
@@ -179,7 +179,7 @@ namespace Avro.Test
stream.Position = 0;
var decoder = new BinaryDecoder(stream);
var reader = new SpecificDefaultReader(rec.Schema, rec.Schema);
- var rec2 = (SpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);
+ var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);
Assert.IsFalse(rec2 == null);
}
}
|