Return-Path: Delivered-To: apmail-avro-commits-archive@www.apache.org Received: (qmail 2102 invoked from network); 31 Mar 2011 21:17:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 31 Mar 2011 21:17:24 -0000 Received: (qmail 25894 invoked by uid 500); 31 Mar 2011 21:17:22 -0000 Delivered-To: apmail-avro-commits-archive@avro.apache.org Received: (qmail 25874 invoked by uid 500); 31 Mar 2011 21:17:22 -0000 Mailing-List: contact commits-help@avro.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@avro.apache.org Delivered-To: mailing list commits@avro.apache.org Received: (qmail 25867 invoked by uid 99); 31 Mar 2011 21:17:22 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 Mar 2011 21:17:22 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 31 Mar 2011 21:17:13 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id A5F662388A60; Thu, 31 Mar 2011 21:16:50 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r1087439 [1/6] - in /avro/trunk: ./ lang/csharp/ lang/csharp/lib/ lang/csharp/lib/main/ lang/csharp/lib/test/ lang/csharp/src/ lang/csharp/src/apache/ lang/csharp/src/apache/codegen/ lang/csharp/src/apache/codegen/Properties/ lang/csharp/sr... Date: Thu, 31 Mar 2011 21:16:49 -0000 To: commits@avro.apache.org From: cutting@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110331211650.A5F662388A60@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cutting Date: Thu Mar 31 21:16:28 2011 New Revision: 1087439 URL: http://svn.apache.org/viewvc?rev=1087439&view=rev Log: AVRO-533. Add a C# implementation. Contributed by Jeremy Custenborder, Dona Alvarez and thiru. Added: avro/trunk/lang/csharp/ (with props) avro/trunk/lang/csharp/Avro.nunit avro/trunk/lang/csharp/Avro.sln avro/trunk/lang/csharp/README avro/trunk/lang/csharp/build.sh (with props) avro/trunk/lang/csharp/lib/ avro/trunk/lang/csharp/lib/main/ avro/trunk/lang/csharp/lib/main/Newtonsoft.Json.dll (with props) avro/trunk/lang/csharp/lib/main/log4net.dll (with props) avro/trunk/lang/csharp/lib/test/ avro/trunk/lang/csharp/lib/test/nunit.framework.dll (with props) avro/trunk/lang/csharp/src/ avro/trunk/lang/csharp/src/apache/ avro/trunk/lang/csharp/src/apache/codegen/ avro/trunk/lang/csharp/src/apache/codegen/Avro.codegen.csproj avro/trunk/lang/csharp/src/apache/codegen/Program.cs avro/trunk/lang/csharp/src/apache/codegen/Properties/ avro/trunk/lang/csharp/src/apache/codegen/Properties/AssemblyInfo.cs avro/trunk/lang/csharp/src/apache/codegen/app.config (with props) avro/trunk/lang/csharp/src/apache/main/ avro/trunk/lang/csharp/src/apache/main/Avro.main.csproj avro/trunk/lang/csharp/src/apache/main/CodeGen/ avro/trunk/lang/csharp/src/apache/main/CodeGen/AvroRuntimeException.cs avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGen.cs avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenException.cs avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs avro/trunk/lang/csharp/src/apache/main/Generic/ avro/trunk/lang/csharp/src/apache/main/Generic/DatumReader.cs avro/trunk/lang/csharp/src/apache/main/Generic/DatumWriter.cs avro/trunk/lang/csharp/src/apache/main/Generic/GenericEnum.cs avro/trunk/lang/csharp/src/apache/main/Generic/GenericFixed.cs avro/trunk/lang/csharp/src/apache/main/Generic/GenericReader.cs avro/trunk/lang/csharp/src/apache/main/Generic/GenericRecord.cs avro/trunk/lang/csharp/src/apache/main/Generic/GenericWriter.cs avro/trunk/lang/csharp/src/apache/main/IO/ avro/trunk/lang/csharp/src/apache/main/IO/BinaryDecoder.cs avro/trunk/lang/csharp/src/apache/main/IO/BinaryEncoder.cs avro/trunk/lang/csharp/src/apache/main/IO/Decoder.cs avro/trunk/lang/csharp/src/apache/main/IO/Encoder.cs avro/trunk/lang/csharp/src/apache/main/IO/Resolver.cs avro/trunk/lang/csharp/src/apache/main/Properties/ avro/trunk/lang/csharp/src/apache/main/Properties/AssemblyInfo.cs avro/trunk/lang/csharp/src/apache/main/Properties/Settings.Designer.cs avro/trunk/lang/csharp/src/apache/main/Protocol/ avro/trunk/lang/csharp/src/apache/main/Protocol/Message.cs avro/trunk/lang/csharp/src/apache/main/Protocol/Protocol.cs avro/trunk/lang/csharp/src/apache/main/Protocol/ProtocolParseException.cs avro/trunk/lang/csharp/src/apache/main/Schema/ avro/trunk/lang/csharp/src/apache/main/Schema/ArraySchema.cs avro/trunk/lang/csharp/src/apache/main/Schema/AvroException.cs avro/trunk/lang/csharp/src/apache/main/Schema/AvroTypeException.cs avro/trunk/lang/csharp/src/apache/main/Schema/EnumSchema.cs avro/trunk/lang/csharp/src/apache/main/Schema/Field.cs avro/trunk/lang/csharp/src/apache/main/Schema/FixedSchema.cs avro/trunk/lang/csharp/src/apache/main/Schema/JsonHelper.cs avro/trunk/lang/csharp/src/apache/main/Schema/MapSchema.cs avro/trunk/lang/csharp/src/apache/main/Schema/NamedSchema.cs avro/trunk/lang/csharp/src/apache/main/Schema/PrimitiveSchema.cs avro/trunk/lang/csharp/src/apache/main/Schema/Property.cs avro/trunk/lang/csharp/src/apache/main/Schema/RecordSchema.cs avro/trunk/lang/csharp/src/apache/main/Schema/Schema.cs avro/trunk/lang/csharp/src/apache/main/Schema/SchemaName.cs avro/trunk/lang/csharp/src/apache/main/Schema/SchemaParseException.cs avro/trunk/lang/csharp/src/apache/main/Schema/UnionSchema.cs avro/trunk/lang/csharp/src/apache/main/Schema/UnnamedSchema.cs avro/trunk/lang/csharp/src/apache/main/Specific/ avro/trunk/lang/csharp/src/apache/main/Specific/ObjectCreator.cs avro/trunk/lang/csharp/src/apache/main/Specific/SpecificFixed.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/ avro/trunk/lang/csharp/src/apache/test/Avro.test.csproj avro/trunk/lang/csharp/src/apache/test/CodGen/ avro/trunk/lang/csharp/src/apache/test/CodGen/CodeGenTest.cs avro/trunk/lang/csharp/src/apache/test/Generic/ avro/trunk/lang/csharp/src/apache/test/Generic/GenericTests.cs avro/trunk/lang/csharp/src/apache/test/IO/ avro/trunk/lang/csharp/src/apache/test/IO/BinaryCodecTests.cs avro/trunk/lang/csharp/src/apache/test/Properties/ avro/trunk/lang/csharp/src/apache/test/Properties/AssemblyInfo.cs avro/trunk/lang/csharp/src/apache/test/Protocol/ avro/trunk/lang/csharp/src/apache/test/Protocol/ProtocolTest.cs avro/trunk/lang/csharp/src/apache/test/Schema/ avro/trunk/lang/csharp/src/apache/test/Schema/AliasTest.cs avro/trunk/lang/csharp/src/apache/test/Schema/SchemaTests.cs avro/trunk/lang/csharp/src/apache/test/Specific/ avro/trunk/lang/csharp/src/apache/test/Specific/SpecificTests.cs Modified: avro/trunk/CHANGES.txt avro/trunk/build.sh avro/trunk/share/rat-excludes.txt Modified: avro/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1087439&r1=1087438&r2=1087439&view=diff ============================================================================== --- avro/trunk/CHANGES.txt (original) +++ avro/trunk/CHANGES.txt Thu Mar 31 21:16:28 2011 @@ -12,6 +12,11 @@ Avro 1.6.0 (unreleased) Avro 1.5.1 (unreleased) + NEW FEATURES + + AVRO-533. Add a C# implementation. + (Jeremy Custenborder, Dona Alvarez and thiru) + IMPROVEMENTS AVRO-785. Java: Squash a Velocity warning by upgrading to Velocity 1.7. Modified: avro/trunk/build.sh URL: http://svn.apache.org/viewvc/avro/trunk/build.sh?rev=1087439&r1=1087438&r2=1087439&view=diff ============================================================================== --- avro/trunk/build.sh (original) +++ avro/trunk/build.sh Thu Mar 31 21:16:28 2011 @@ -44,6 +44,7 @@ case "$target" in (cd lang/py; ant test) (cd lang/c; ./build.sh test) (cd lang/c++; ./build.sh test) + (cd lang/csharp; ./build.sh test) (cd lang/ruby; rake test) (cd lang/php; ./build.sh test) @@ -144,6 +145,8 @@ case "$target" in (cd lang/c++; ./build.sh clean) + (cd lang/csharp; ./build.sh clean) + (cd lang/ruby; rake clean) (cd lang/php; ./build.sh clean) Propchange: avro/trunk/lang/csharp/ ------------------------------------------------------------------------------ --- svn:ignore (added) +++ svn:ignore Thu Mar 31 21:16:28 2011 @@ -0,0 +1,5 @@ +build +src/apache/codegen/obj +src/apache/main/obj +src/apache/test/obj +TestResult.xml Added: avro/trunk/lang/csharp/Avro.nunit URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/Avro.nunit?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/Avro.nunit (added) +++ avro/trunk/lang/csharp/Avro.nunit Thu Mar 31 21:16:28 2011 @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + Added: avro/trunk/lang/csharp/Avro.sln URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/Avro.sln?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/Avro.sln (added) +++ avro/trunk/lang/csharp/Avro.sln Thu Mar 31 21:16:28 2011 @@ -0,0 +1,55 @@ +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +lang/csharp/Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avro.main", "src\apache\main\Avro.main.csproj", "{A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avro.test", "src\apache\test\Avro.test.csproj", "{911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avro.codegen", "src\apache\codegen\Avro.codegen.csproj", "{BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|Mixed Platforms = Debug|Mixed Platforms + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|Mixed Platforms = Release|Mixed Platforms + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Debug|x86.ActiveCfg = Debug|Any CPU + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Release|Any CPU.Build.0 = Release|Any CPU + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0}.Release|x86.ActiveCfg = Release|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Debug|x86.ActiveCfg = Debug|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Release|Any CPU.Build.0 = Release|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {911D56AB-587B-4E5F-B5EA-D47D8A46F1FA}.Release|x86.ActiveCfg = Release|Any CPU + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Debug|x86.ActiveCfg = Debug|x86 + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Debug|x86.Build.0 = Debug|x86 + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Release|Any CPU.Build.0 = Release|Any CPU + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Release|Mixed Platforms.ActiveCfg = Release|x86 + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Release|Mixed Platforms.Build.0 = Release|x86 + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Release|x86.ActiveCfg = Release|x86 + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal Added: avro/trunk/lang/csharp/README URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/README?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/README (added) +++ avro/trunk/lang/csharp/README Thu Mar 31 21:16:28 2011 @@ -0,0 +1,34 @@ +1. Building Avro for .NET + +a. Windows + +Prerequisites + Microsoft Visual C# Express 2010 + +To build open Avro.sln solution in VC# Express 2010 and build from there. + +b. Linux + +Prerequisites + Mono 2.6 or above. + +For Ubuntu Lucid Lynx, mono that comes with it is 2.4. To install mono 2.6 +follow the procedure described at http://badgerports.org/ + +To build issue the command: +$ cd lang/csharp + +then + +$ CONFIGURATION=Release TARGETFRAMEWORKVERSION=3.5 xbuild Avro.sln + +or + +$ CONFIGURATION=Debug TARGETFRAMEWORKVERSION=3.5 xbuild Avro.sln + +2. Unit tests + +This is a common procedure for both Windows and Linux. + +To run unit-tests, please install NUnit 2.5 or above. From within the NUnit +GUI, open the project Avro.nunit and run the tests. Added: avro/trunk/lang/csharp/build.sh URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/build.sh?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/build.sh (added) +++ avro/trunk/lang/csharp/build.sh Thu Mar 31 21:16:28 2011 @@ -0,0 +1,22 @@ +#!/bin/bash +set -e # exit on error +set -x + +case "$1" in + + test) + CONFIGURATION=Release TARGETFRAMEWORKVERSION=3.5 xbuild + nunit-console Avro.nunit + ;; + + clean) + rm -rf src/apache/{main,test,codegen}/obj + rm -rf build + ;; + + *) + echo "Usage: $0 {test|clean}" + exit 1 +esac + +exit 0 Propchange: avro/trunk/lang/csharp/build.sh ------------------------------------------------------------------------------ svn:executable = * Added: avro/trunk/lang/csharp/lib/main/Newtonsoft.Json.dll URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/lib/main/Newtonsoft.Json.dll?rev=1087439&view=auto ============================================================================== Binary file - no diff available. Propchange: avro/trunk/lang/csharp/lib/main/Newtonsoft.Json.dll ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: avro/trunk/lang/csharp/lib/main/log4net.dll URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/lib/main/log4net.dll?rev=1087439&view=auto ============================================================================== Binary file - no diff available. Propchange: avro/trunk/lang/csharp/lib/main/log4net.dll ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: avro/trunk/lang/csharp/lib/test/nunit.framework.dll URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/lib/test/nunit.framework.dll?rev=1087439&view=auto ============================================================================== Binary file - no diff available. Propchange: avro/trunk/lang/csharp/lib/test/nunit.framework.dll ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: 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=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/src/apache/codegen/Avro.codegen.csproj (added) +++ avro/trunk/lang/csharp/src/apache/codegen/Avro.codegen.csproj Thu Mar 31 21:16:28 2011 @@ -0,0 +1,162 @@ + + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {BF0D313C-1AA3-4900-B277-B0F5F9DDCDA8} + Exe + Properties + Avro.codegen + avrogen + v4.0 + Client + 512 + false + + + 3.5 + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + true + + + AnyCPU + true + full + false + ..\..\..\build\codegen\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + none + true + ..\..\..\build\codegen\Release\ + + + prompt + 4 + + + true + ..\..\..\build\codegen\Debug\ + DEBUG;TRACE + full + AnyCPU + ..\..\..\build\codegen\Debug\avrogen.exe.CodeAnalysisLog.xml + true + GlobalSuppressions.cs + prompt + MinimumRecommendedRules.ruleset + ;C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets + true + ;C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules + true + Off + + + ..\..\..\build\codegen\Release\ + + + true + none + AnyCPU + ..\..\..\build\codegen\Release\avrogen.exe.CodeAnalysisLog.xml + true + GlobalSuppressions.cs + prompt + MinimumRecommendedRules.ruleset + ;C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\\Rule Sets + false + ;C:\Program Files\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop\\Rules + false + Off + + + ..\..\..\build\codegen\Release\ + + + + + + + + + + + + + + + + + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0} + Avro.main + + + + + False + Microsoft .NET Framework 4 Client Profile %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + + + + + + + + Added: avro/trunk/lang/csharp/src/apache/codegen/Program.cs URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/codegen/Program.cs?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/src/apache/codegen/Program.cs (added) +++ avro/trunk/lang/csharp/src/apache/codegen/Program.cs Thu Mar 31 21:16:28 2011 @@ -0,0 +1,88 @@ +/** + * 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 + * + * http://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.Linq; +using System.Text; +using Avro.codegen; + +namespace Avro.codegen +{ + class Program + { + static void Main(string[] args) + { + if (args.Length != 3) + { + Usage(); + return; + } + if (args[0] == "-p") + GenProtocol(args[1], args[2]); + else if (args[0] == "-s") + GenSchema(args[1], args[2]); + else + Usage(); + } + + static void Usage() + { + Console.WriteLine("Usage:\navrogen -p \navrogen -s "); + return; + } + + static void GenProtocol(string infile, string outdir) + { + try + { + string text = System.IO.File.ReadAllText(infile); + Protocol protocol = Protocol.Parse(text); + + CodeGen codegen = new CodeGen(); + codegen.AddProtocol(protocol); + + codegen.GenerateCode(); + codegen.WriteTypes(outdir); + } + catch (Exception ex) + { + Console.WriteLine("Exception occurred. " + ex.Message); + } + } + + static void GenSchema(string infile, string outdir) + { + try + { + string text = System.IO.File.ReadAllText(infile); + Schema schema = Schema.Parse(text); + + CodeGen codegen = new CodeGen(); + codegen.AddSchema(schema); + + codegen.GenerateCode(); + codegen.WriteTypes(outdir); + } + catch (Exception ex) + { + Console.WriteLine("Exception occurred. " + ex.Message); + } + } + + } +} Added: avro/trunk/lang/csharp/src/apache/codegen/Properties/AssemblyInfo.cs URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/codegen/Properties/AssemblyInfo.cs?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/src/apache/codegen/Properties/AssemblyInfo.cs (added) +++ avro/trunk/lang/csharp/src/apache/codegen/Properties/AssemblyInfo.cs Thu Mar 31 21:16:28 2011 @@ -0,0 +1,54 @@ +/** + * 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 + * + * http://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.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Avro.codegen")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Bank of America")] +[assembly: AssemblyProduct("Avro.codegen")] +[assembly: AssemblyCopyright("Copyright © Bank of America 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("3ccfcbf4-bdf9-4f75-8d5a-ab476bb4408f")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] Added: avro/trunk/lang/csharp/src/apache/codegen/app.config URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/codegen/app.config?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/src/apache/codegen/app.config (added) +++ avro/trunk/lang/csharp/src/apache/codegen/app.config Thu Mar 31 21:16:28 2011 @@ -0,0 +1,19 @@ + + + + Propchange: avro/trunk/lang/csharp/src/apache/codegen/app.config ------------------------------------------------------------------------------ svn:executable = * Added: 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=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/src/apache/main/Avro.main.csproj (added) +++ avro/trunk/lang/csharp/src/apache/main/Avro.main.csproj Thu Mar 31 21:16:28 2011 @@ -0,0 +1,155 @@ + + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {A0A5CA3C-F58C-4D07-98B0-2C7B62AB20F0} + Library + Properties + Avro + Avro + v4.0 + 512 + Client + + + 3.5 + + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + + + true + full + false + ..\..\..\build\main\Debug\ + DEBUG;TRACE + prompt + 4 + Off + + + none + true + ..\..\..\build\main\Release\ + + + prompt + 4 + Off + + + + ..\..\..\lib\main\log4net.dll + + + ..\..\..\lib\main\Newtonsoft.Json.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + False + Microsoft .NET Framework 4 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + + + + + Added: avro/trunk/lang/csharp/src/apache/main/CodeGen/AvroRuntimeException.cs URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/CodeGen/AvroRuntimeException.cs?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/src/apache/main/CodeGen/AvroRuntimeException.cs (added) +++ avro/trunk/lang/csharp/src/apache/main/CodeGen/AvroRuntimeException.cs Thu Mar 31 21:16:28 2011 @@ -0,0 +1,38 @@ +/** + * 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 + * + * http://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.Linq; +using System.Text; + +namespace Avro +{ + public class AvroRuntimeException : AvroException + { + public AvroRuntimeException(string s) + : base(s) + { + + } + public AvroRuntimeException(string s, Exception inner) + : base(s, inner) + { + + } + } +} Added: 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=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGen.cs (added) +++ avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGen.cs Thu Mar 31 21:16:28 2011 @@ -0,0 +1,645 @@ +/** + * 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 + * + * http://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; +using System.CodeDom; +using System.CodeDom.Compiler; +using Microsoft.CSharp; +using System.IO; + +namespace Avro +{ + public class CodeGen + { + public CodeCompileUnit CompileUnit { get; private set; } + public IList Schemas { get; private set; } + public IList Protocols { get; private set; } + private Dictionary namespaceLookup = new Dictionary(StringComparer.Ordinal); + private CodeGenExt codeGenExt; + + /// + /// Default constructor + /// + public CodeGen() + { + this.Schemas = new List(); + this.Protocols = new List(); + this.codeGenExt = CodeGenExt.Instance; + } + + /// + /// Adds a protocol object to generate code for + /// + /// protocol object + public void AddProtocol(Protocol protocol) + { + Protocols.Add(protocol); + } + + /// + /// Adds a schema object to generate code for + /// + /// schema object + public void AddSchema(Schema schema) + { + Schemas.Add(schema); + } + + /// + /// Adds a namespace object for the given name into the dictionary if it doesn't exist yet + /// + /// name of namespace + /// + private CodeNamespace addNamespace(string name) + { + if (string.IsNullOrEmpty(name)) + throw new ArgumentNullException("name", "name cannot be null."); + + CodeNamespace ns = null; + + if (!namespaceLookup.TryGetValue(name, out ns)) + { + ns = new CodeNamespace(codeGenExt.Mangle(name)); + foreach (CodeNamespaceImport nci in codeGenExt.NamespaceImports) + ns.Imports.Add(nci); + + CompileUnit.Namespaces.Add(ns); + namespaceLookup.Add(name, ns); + } + return ns; + } + + /// + /// Generates code for the given protocol and schema objects + /// + /// CodeCompileUnit object + public CodeCompileUnit GenerateCode() + { + CompileUnit = new CodeCompileUnit(); + + processSchemas(); + processProtocols(); + + return CompileUnit; + } + + /// + /// Generates code for the schema objects + /// + private void processSchemas() + { + foreach (Schema schema in this.Schemas) + { + SchemaNames names = generateNames(schema); + foreach (KeyValuePair sn in names) + { + switch (sn.Value.Tag) + { + case Schema.Type.Enumeration: processEnum(sn.Value); break; + case Schema.Type.Fixed: processFixed(sn.Value); break; + case Schema.Type.Record: processRecord(sn.Value); break; + case Schema.Type.Error: processRecord(sn.Value); break; + default: + throw new CodeGenException("Names in schema should only be of type NamedSchema, type found " + sn.Value.Tag); + } + } + } + } + + /// + /// Generates code for the protocol objects + /// + private void processProtocols() + { + foreach (Protocol protocol in Protocols) + { + SchemaNames names = generateNames(protocol); + foreach (KeyValuePair sn in names) + { + switch (sn.Value.Tag) + { + case Schema.Type.Enumeration: processEnum(sn.Value); break; + case Schema.Type.Fixed: processFixed(sn.Value); break; + case Schema.Type.Record: processRecord(sn.Value); break; + case Schema.Type.Error: processRecord(sn.Value); break; + default: + throw new CodeGenException("Names in protocol should only be of type NamedSchema, type found " + sn.Value.Tag); + } + } + } + } + + /// + /// Generate list of named schemas from given protocol + /// + /// protocol to process + /// + private SchemaNames generateNames(Protocol protocol) + { + var names = new SchemaNames(); + foreach (Schema schema in protocol.Types) + addName(schema, names); + return names; + } + + /// + /// Generate list of named schemas from given schema + /// + /// schema to process + /// + private SchemaNames generateNames(Schema schema) + { + var names = new SchemaNames(); + addName(schema, names); + return names; + } + + /// + /// Recursively search the given schema for named schemas and adds them to the given container + /// + /// schema object to search + /// list of named schemas + private void addName(Schema schema, SchemaNames names) + { + NamedSchema ns = schema as NamedSchema; + if (null != ns) if (names.Contains(ns.SchemaName)) return; + + switch (schema.Tag) + { + case Schema.Type.Null: + case Schema.Type.Boolean: + case Schema.Type.Int: + case Schema.Type.Long: + case Schema.Type.Float: + case Schema.Type.Double: + case Schema.Type.Bytes: + case Schema.Type.String: + break; + + case Schema.Type.Enumeration: + case Schema.Type.Fixed: + names.Add(ns); + break; + + case Schema.Type.Record: + case Schema.Type.Error: + var rs = schema as RecordSchema; + names.Add(rs); + foreach (Field field in rs.Fields) + addName(field.Schema, names); + break; + + case Schema.Type.Array: + var asc = schema as ArraySchema; + addName(asc.ItemSchema, names); + break; + + case Schema.Type.Map: + var ms = schema as MapSchema; + addName(ms.ValueSchema, names); + break; + + case Schema.Type.Union: + var us = schema as UnionSchema; + foreach (Schema usc in us.Schemas) + addName(usc, names); + break; + + default: + throw new CodeGenException("Unable to add name for " + schema.Name + " type " + schema.Tag); + + } + } + + /// + /// Creates a class declaration for fixed schema + /// + /// fixed schema + /// namespace object + private 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.IsClass = true; + ctd.IsPartial = true; + ctd.Attributes = MemberAttributes.Public; + ctd.BaseTypes.Add("SpecificFixed"); + + // create static schema field + createSchemaField(schema, ctd); + + // Add Size field + string sizefname = "fixedSize"; + var ctrfield = new CodeTypeReference(typeof(uint)); + var codeField = new CodeMemberField(ctrfield, sizefname); + codeField.Attributes = MemberAttributes.Private | MemberAttributes.Static; + codeField.InitExpression = new CodePrimitiveExpression(fixedSchema.Size); + ctd.Members.Add(codeField); + + // Add Size property + var fieldRef = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), sizefname); + var property = new CodeMemberProperty(); + property.Attributes = MemberAttributes.Public | MemberAttributes.Static; + property.Name = "FixedSize"; + property.Type = ctrfield; + property.GetStatements.Add(new CodeMethodReturnStatement(new CodeTypeReferenceExpression(schema.Name + "." + sizefname))); + ctd.Members.Add(property); + + // create constructor to initiate base class SpecificFixed + CodeConstructor cc = new CodeConstructor(); + cc.Attributes = MemberAttributes.Public; + cc.BaseConstructorArgs.Add(new CodeVariableReferenceExpression(sizefname)); + ctd.Members.Add(cc); + + string nspace = fixedSchema.Namespace; + if (string.IsNullOrEmpty(nspace)) + throw new CodeGenException("Namespace required for enum schema " + fixedSchema.Name); + CodeNamespace codens = addNamespace(nspace); + codens.Types.Add(ctd); + } + + /// + /// Creates an enum declaration + /// + /// enum schema + /// namespace + private 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)); + ctd.IsEnum = true; + ctd.Attributes = MemberAttributes.Public; + + foreach (string symbol in enumschema.Symbols) + { + if (codeGenExt.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); + } + + string nspace = enumschema.Namespace; + if (string.IsNullOrEmpty(nspace)) + throw new CodeGenException("Namespace required for enum schema " + enumschema.Name); + CodeNamespace codens = addNamespace(nspace); + + codens.Types.Add(ctd); + } + + /// + /// Creates a class declaration + /// + /// record schema + /// namespace + /// + private 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"); + ctd.Attributes = MemberAttributes.Public; + ctd.IsClass = true; + ctd.IsPartial = true; + + createSchemaField(schema, ctd); + + // declare Get() to be used by the Writer classes + var cmmGet = new CodeMemberMethod(); + cmmGet.Name = "Get"; + cmmGet.Attributes = MemberAttributes.Public | MemberAttributes.Override; + 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"); + + // declare Put() to be used by the Reader classes + var cmmPut = new CodeMemberMethod(); + cmmPut.Name = "Put"; + cmmPut.Attributes = MemberAttributes.Public | MemberAttributes.Override; + 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)\n\t\t\t{\n"); + + foreach (Field field in recordSchema.Fields) + { + // Determine type of field + bool nullibleEnum = false; + string baseType = codeGenExt.Mangle(getType(field.Schema, false, ref nullibleEnum)); + var ctrfield = new CodeTypeReference(baseType); + + // Create field + string privFieldName = string.Concat("_", field.Name); + var codeField = new CodeMemberField(ctrfield, privFieldName); + codeField.Attributes = MemberAttributes.Private; + + // Process field documentation if it exist and add to the field + CodeCommentStatement propertyComment = null; + if (!string.IsNullOrEmpty(field.Documentation)) + { + propertyComment = createDocComment(field.Documentation); + if (null != propertyComment) + codeField.Comments.Add(propertyComment); + } + + // Add field to class + ctd.Members.Add(codeField); + + // Create reference to the field - this.fieldname + var fieldRef = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), privFieldName); + var mangledName = codeGenExt.Mangle(field.Name); + + // Create field property with get and set methods + var property = new CodeMemberProperty(); + property.Attributes = MemberAttributes.Public | MemberAttributes.Final; + property.Name = mangledName; + property.Type = ctrfield; + property.GetStatements.Add(new CodeMethodReturnStatement(fieldRef)); + property.SetStatements.Add(new CodeAssignStatement(fieldRef, new CodePropertySetValueReferenceExpression())); + if (null != propertyComment) + property.Comments.Add(propertyComment); + + // Add field property to class + ctd.Members.Add(property); + + // add to Get() + getFieldStmt.Append("\t\t\tcase "); + getFieldStmt.Append(field.Pos); + getFieldStmt.Append(": return this."); + getFieldStmt.Append(mangledName); + getFieldStmt.Append(";\n"); + + // add to Put() + putFieldStmt.Append("\t\t\tcase "); + putFieldStmt.Append(field.Pos); + putFieldStmt.Append(": this."); + putFieldStmt.Append(mangledName); + + if (nullibleEnum) + { + putFieldStmt.Append(" = fieldValue == null ? ("); + putFieldStmt.Append(baseType); + putFieldStmt.Append(")null : ("); + + string type = baseType.Remove(0, 16); // remove System.Nullable< + type = type.Remove(type.Length - 1); // remove > + + putFieldStmt.Append(type); + putFieldStmt.Append(")fieldValue; break;\n"); + } + else + { + putFieldStmt.Append(" = ("); + putFieldStmt.Append(baseType); + putFieldStmt.Append(")fieldValue; break;\n"); + } + } + + // end switch block for Get() + getFieldStmt.Append("\t\t\tdefault: throw new AvroRuntimeException(\"Bad index \" + fieldPos + \" in Get()\");\n\t\t\t}"); + var cseGet = new CodeSnippetExpression(getFieldStmt.ToString()); + cmmGet.Statements.Add(cseGet); + ctd.Members.Add(cmmGet); + + // end switch block for Put() + putFieldStmt.Append("\t\t\tdefault: throw new AvroRuntimeException(\"Bad index \" + fieldPos + \" in Put()\");\n\t\t\t}"); + var csePut = new CodeSnippetExpression(putFieldStmt.ToString()); + cmmPut.Statements.Add(csePut); + ctd.Members.Add(cmmPut); + + string nspace = recordSchema.Namespace; + if (string.IsNullOrEmpty(nspace)) + throw new CodeGenException("Namespace required for record schema " + recordSchema.Name); + CodeNamespace codens = addNamespace(nspace); + + codens.Types.Add(ctd); + + return ctd; + } + + /// + /// Gets the string representation of the schema's data type + /// + /// schema + /// flag to indicate union with null + /// + internal static string getType(Schema schema, bool nullible, ref bool nullibleEnum) + { + switch (schema.Tag) + { + case Schema.Type.Null: + return "System.Object"; + case Schema.Type.Boolean: + if (nullible) return "System.Nullable"; + else return typeof(bool).ToString(); + case Schema.Type.Int: + if (nullible) return "System.Nullable"; + else return typeof(int).ToString(); + case Schema.Type.Long: + if (nullible) return "System.Nullable"; + else return typeof(long).ToString(); + case Schema.Type.Float: + if (nullible) return "System.Nullable"; + else return typeof(float).ToString(); + case Schema.Type.Double: + if (nullible) return "System.Nullable"; + else return typeof(double).ToString(); + + case Schema.Type.Bytes: + return typeof(byte[]).ToString(); + case Schema.Type.String: + return typeof(string).ToString(); + + case Schema.Type.Enumeration: + var namedSchema = schema as NamedSchema; + if (null == namedSchema) + throw new CodeGenException("Unable to cast schema into a named schema"); + if (nullible) + { + nullibleEnum = true; + return "System.Nullable<" + namedSchema.Fullname + ">"; + } + else return namedSchema.Fullname; + + case Schema.Type.Fixed: + case Schema.Type.Record: + case Schema.Type.Error: + namedSchema = schema as NamedSchema; + if (null == namedSchema) + throw new CodeGenException("Unable to cast schema into a named schema"); + return namedSchema.Fullname; + + case Schema.Type.Array: + var arraySchema = schema as ArraySchema; + if (null == arraySchema) + throw new CodeGenException("Unable to cast schema into an array schema"); + + return "IList<" + getType(arraySchema.ItemSchema, false, ref nullibleEnum) + ">"; + + case Schema.Type.Map: + var mapSchema = schema as MapSchema; + if (null == mapSchema) + throw new CodeGenException("Unable to cast schema into a map schema"); + return "IDictionary"; + + case Schema.Type.Union: + var unionSchema = schema as UnionSchema; + if (null == unionSchema) + throw new CodeGenException("Unable to cast schema into a union schema"); + Schema nullibleType = getNullableType(unionSchema); + if (null == nullibleType) + return "System.Object"; + else + return getType(nullibleType, true, ref nullibleEnum); + } + throw new CodeGenException("Unable to generate CodeTypeReference for " + schema.Name + " type " + schema.Tag); + } + + /// + /// Gets the schema of a union with null + /// + /// union schema + /// schema that is nullible + public static Schema getNullableType(UnionSchema schema) + { + Schema ret = null; + if (schema.Count == 2) + { + bool nullable = false; + foreach (Schema childSchema in schema.Schemas) + { + if (childSchema.Tag == Schema.Type.Null) + nullable = true; + else + ret = childSchema; + } + if (!nullable) + ret = null; + } + return ret; + } + + /// + /// Creates the static schema field for class types + /// + /// schema + /// CodeTypeDeclaration for the class + private void createSchemaField(Schema schema, CodeTypeDeclaration ctd) + { + // create schema field + var ctrfield = new CodeTypeReference("Schema"); + string schemaFname = "_SCHEMA"; + var codeField = new CodeMemberField(ctrfield, schemaFname); + codeField.Attributes = MemberAttributes.Private | MemberAttributes.Static; + // create function call Schema.Parse(json) + var cpe = new CodePrimitiveExpression(schema.ToString()); + var cmie = new CodeMethodInvokeExpression( + new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Schema)), "Parse"), + new CodeExpression[] { cpe }); + codeField.InitExpression = cmie; + ctd.Members.Add(codeField); + + // create property to get static schema field + var property = new CodeMemberProperty(); + property.Attributes = MemberAttributes.Public | MemberAttributes.Override; + property.Name = "Schema"; + property.Type = ctrfield; + property.GetStatements.Add(new CodeMethodReturnStatement(new CodeTypeReferenceExpression(schema.Name + "." + schemaFname))); + ctd.Members.Add(property); + } + + /// + /// Creates an XML documentation for the given comment + /// + /// comment + /// CodeCommentStatement object + private CodeCommentStatement createDocComment(string comment) + { + string text = string.Format("\r\n {0}\r\n ", comment); + return new CodeCommentStatement(text, true); + } + + /// + /// Writes the generated compile unit into one file + /// + /// name of output file to write to + public void WriteCompileUnit(string outputFile) + { + var cscp = new CSharpCodeProvider(); + + var opts = new CodeGeneratorOptions(); + opts.BracingStyle = "C"; + opts.IndentString = "\t"; + opts.BlankLinesBetweenMembers = false; + + using (var outfile = new StreamWriter(outputFile)) + { + cscp.GenerateCodeFromCompileUnit(CompileUnit, outfile, opts); + } + } + + /// + /// Writes each types in each namespaces into individual files + /// + /// name of directory to write to + public void WriteTypes(string outputdir) + { + var cscp = new CSharpCodeProvider(); + + var opts = new CodeGeneratorOptions(); + opts.BracingStyle = "C"; + opts.IndentString = "\t"; + opts.BlankLinesBetweenMembers = false; + + CodeNamespaceCollection nsc = CompileUnit.Namespaces; + for (int i = 0; i < nsc.Count; i++) + { + var ns = nsc[i]; + + string dir = outputdir + "\\" + codeGenExt.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.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"; + using (var writer = new StreamWriter(file, false)) + { + new_ns.Types.Add(ctd); + cscp.GenerateCodeFromNamespace(new_ns, writer, opts); + new_ns.Types.Remove(ctd); + } + } + } + } + } +} Added: avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenException.cs URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenException.cs?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenException.cs (added) +++ avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenException.cs Thu Mar 31 21:16:28 2011 @@ -0,0 +1,38 @@ +/** + * 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 + * + * http://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.Linq; +using System.Text; + +namespace Avro +{ + class CodeGenException : AvroException + { + public CodeGenException(string s) + : base(s) + { + + } + public CodeGenException(string s, Exception inner) + : base(s, inner) + { + + } + } +} Added: avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs URL: http://svn.apache.org/viewvc/avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs?rev=1087439&view=auto ============================================================================== --- avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs (added) +++ avro/trunk/lang/csharp/src/apache/main/CodeGen/CodeGenExt.cs Thu Mar 31 21:16:28 2011 @@ -0,0 +1,104 @@ +/** + * 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 + * + * http://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.Linq; +using System.Text; +using System.CodeDom; + +namespace Avro +{ + /// + /// A singleton class containing data used by codegen + /// + public sealed class CodeGenExt + { + private static readonly CodeGenExt instance = new CodeGenExt(); + public static CodeGenExt Instance { get { return instance; } } + + public CodeNamespaceImport[] NamespaceImports { get; private set; } + public CodeCommentStatement FileComment { get; private set; } + public HashSet ReservedKeywords { get; private set; } + private const char AT = '@'; + private const char DOT = '.'; + + private CodeGenExt() + { + NamespaceImports = new CodeNamespaceImport[] { + new CodeNamespaceImport("System"), + new CodeNamespaceImport("System.Collections.Generic"), + new CodeNamespaceImport("System.Text"), + new CodeNamespaceImport("Avro"), + new CodeNamespaceImport("Avro.Specific") }; + + FileComment = new CodeCommentStatement( +@"------------------------------------------------------------------------------ + + Generated by " + System.AppDomain.CurrentDomain.FriendlyName + ", version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version + @" + Changes to this file may cause incorrect behavior and will be lost if code + is regenerated + + ------------------------------------------------------------------------------"); + + // Visual Studio 2010 http://msdn.microsoft.com/en-us/library/x53a06bb.aspx + ReservedKeywords = new HashSet() { + "abstract","as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", + "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", + "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", + "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", + "null", "object", "operator", "out", "override", "params", "private", "protected", "public", + "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", + "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", + "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while", "value", "partial" }; + } + + /// + /// Append @ to all reserved keywords that appear on the given name + /// + /// + /// + public string Mangle(string name) + { + var builder = new StringBuilder(); + string[] names = name.Split(DOT); + for (int i = 0; i < names.Length; ++i) + { + if (ReservedKeywords.Contains(names[i])) + builder.Append(AT); + builder.Append(names[i]); + builder.Append(DOT); + } + builder.Remove(builder.Length - 1, 1); + return builder.ToString(); + } + + /// + /// Remove all the @ + /// + /// + /// + public string UnMangle(string name) + { + var builder = new StringBuilder(name.Length); + for (int i = 0; i < name.Length; ++i) + if (name[i] != AT) + builder.Append(name[i]); + return builder.ToString(); + } + } +}