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); } }
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); } }
public ValidationResult Validate(string schema) { try { var x = new Avro.CodeGen(); x.AddSchema(Schema.Parse(schema)); var y = x.GenerateCode(); return(ValidationResult.Success); } catch (System.Exception e) { return(new ValidationResult(e.Message)); } }
public static void TestSpecific(string str, object[] result) { Protocol protocol = Protocol.Parse(str); var codegen = new CodeGen(); codegen.AddProtocol(protocol); var compileUnit = codegen.GenerateCode(); // add a constructor to the main class using the passed assignment statements CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]]; CodeConstructor constructor = new CodeConstructor(); constructor.Attributes = MemberAttributes.Public; CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]); constructor.Statements.Add(snippet); ctd.Members.Add(constructor); // add a function to the main class to populate the data // This has been moved from constructor, as it was causing some tests to pass that shouldn't when referencing a blank object on READ. CodeMemberMethod method = new CodeMemberMethod(); method.Attributes = MemberAttributes.Public; method.Name = "Populate"; CodeSnippetExpression snippet2 = new CodeSnippetExpression((string)result[3]); method.Statements.Add(snippet2); ctd.Members.Add(method); // compile var comparam = new CompilerParameters(new string[] { "mscorlib.dll" }); comparam.ReferencedAssemblies.Add("System.dll"); comparam.ReferencedAssemblies.Add("Avro.dll"); comparam.GenerateInMemory = true; var ccp = new Microsoft.CSharp.CSharpCodeProvider(); var units = new CodeCompileUnit[] { compileUnit }; var compres = ccp.CompileAssemblyFromDom(comparam, units); if (compres == null || compres.Errors.Count > 0) { for (int i = 0; i < compres.Errors.Count; i++) Console.WriteLine(compres.Errors[i]); } if (null != compres) Assert.IsTrue(compres.Errors.Count == 0); // create record ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord; // Call populate to put some data in it. Type recType = rec.GetType(); ; MethodInfo methodInfo = recType.GetMethod("Populate"); methodInfo.Invoke(rec, null); var x1 = compres.CompiledAssembly.FullName; Assert.IsFalse(rec == null); // serialize var stream = new MemoryStream(); var binEncoder = new BinaryEncoder(stream); var writer = new SpecificDefaultWriter(rec.Schema); writer.Write(rec.Schema, rec, binEncoder); // deserialize stream.Position = 0; var decoder = new BinaryDecoder(stream); var reader = new SpecificDefaultReader(rec.Schema, rec.Schema); var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder); Assert.IsFalse(rec2 == null); }
private void SaveOutProtocals() { CodeGen cg = new CodeGen(); foreach (Protocol loaded in Protos.Values) { cg.AddProtocol(loaded); foreach (var s in loaded.Types) { cg.AddSchema(s); } } foreach (var s in Loaded.Values) { cg.AddSchema(s); } cg.GenerateCode(); string at = @"..\sources\modules\RoboKindAvroQPIDModule\"; Directory.CreateDirectory(at); cg.WriteTypes(at); }
public static void TestSpecific(string str, object[] result) { Protocol protocol = Protocol.Parse(str); var codegen = new CodeGen(); codegen.AddProtocol(protocol); var compileUnit = codegen.GenerateCode(); // add a constructor to the main class using the passed assignment statements CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]]; CodeConstructor constructor = new CodeConstructor(); constructor.Attributes = MemberAttributes.Public; CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]); constructor.Statements.Add(snippet); ctd.Members.Add(constructor); // compile var comparam = new CompilerParameters(new string[] { "mscorlib.dll" }); comparam.ReferencedAssemblies.Add("System.dll"); comparam.ReferencedAssemblies.Add("System.Core.dll"); comparam.ReferencedAssemblies.Add(Type.GetType("Mono.Runtime") != null ? "Mono.CSharp.dll" : "Microsoft.CSharp.dll"); comparam.ReferencedAssemblies.Add("Avro.dll"); comparam.GenerateInMemory = true; var ccp = new Microsoft.CSharp.CSharpCodeProvider(); var units = new CodeCompileUnit[] { compileUnit }; var compres = ccp.CompileAssemblyFromDom(comparam, units); if (compres == null || compres.Errors.Count > 0) { for (int i = 0; i < compres.Errors.Count; i++) Console.WriteLine(compres.Errors[i]); } if (null != compres) Assert.IsTrue(compres.Errors.Count == 0); // create record ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord; Assert.IsFalse(rec == null); // serialize var stream = new MemoryStream(); var binEncoder = new BinaryEncoder(stream); var writer = new SpecificDefaultWriter(rec.Schema); writer.Write(rec.Schema, rec, binEncoder); // deserialize stream.Position = 0; var decoder = new BinaryDecoder(stream); var reader = new SpecificDefaultReader(rec.Schema, rec.Schema); var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder); Assert.IsFalse(rec2 == null); }