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); } }
static int GenSchema(string infile, string outdir, IEnumerable <KeyValuePair <string, string> > namespaceMapping) { try { string text = System.IO.File.ReadAllText(infile); Schema schema = Schema.Parse(text); CodeGen codegen = new CodeGen(); codegen.AddSchema(schema); foreach (var entry in namespaceMapping) { codegen.NamespaceMapping[entry.Key] = entry.Value; } codegen.GenerateCode(); codegen.WriteTypes(outdir); } catch (Exception ex) { Console.Error.WriteLine("Exception occurred. " + ex.Message); return(1); } return(0); }
static void GenSchema(List <string> infiles, string outdir, IEnumerable <KeyValuePair <string, string> > namespaceMapping) { try { var sn = new SchemaNames(); CodeGen codegen = new CodeGen(); foreach (var infile in infiles) { string text = System.IO.File.ReadAllText(infile); Schema schema = Schema.Parse(text, sn); codegen.AddSchema(schema); } foreach (var entry in namespaceMapping) { codegen.NamespaceMapping[entry.Key] = entry.Value; } 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)); } }
static void GenSchema(string infile, string outdir) { try { var text = System.IO.File.ReadAllText(infile); var schema = Schema.Parse(text); var codegen = new CodeGen(); codegen.AddSchema(schema); codegen.GenerateCode(); codegen.WriteTypes(outdir); } catch (Exception ex) { throw new Exception(string.Concat("Exception occurred. ", ex.Message)); } }
static void GenProtocol(string infile, string outdir) { try { string text = System.IO.File.ReadAllText(infile); var protocol = Protocol.Parse(text); var codegen = new CodeGen(); codegen.AddProtocol(protocol); codegen.GenerateCode(); codegen.WriteTypes(outdir); } catch (Exception ex) { throw new Exception(string.Concat("Exception occurred. ", ex.Message)); } }
private static void GenSchema(IEnumerable <string> infile, string outdir) { try { var codeGen = new CodeGen(); var schemaName = new SchemaNames(); foreach (var str in infile) { var schema = Schema.Parse(System.IO.File.ReadAllText(str), schemaName); schemaName.Add(schema as NamedSchema); codeGen.AddSchema(schema); } codeGen.GenerateCode(); codeGen.WriteTypes(outdir); } catch (Exception exception) { //Console.WriteLine(string.Concat("Exception occurred. ", exception.Message)); throw new Exception(string.Concat("Exception occurred. ", exception.Message)); } }
static void GenProtocol(string infile, string outdir, IEnumerable <KeyValuePair <string, string> > namespaceMapping) { try { string text = System.IO.File.ReadAllText(infile); Protocol protocol = Protocol.Parse(text); CodeGen codegen = new CodeGen(); codegen.AddProtocol(protocol); foreach (var entry in namespaceMapping) { codegen.NamespaceMapping[entry.Key] = entry.Value; } codegen.GenerateCode(); codegen.WriteTypes(outdir); } catch (Exception ex) { Console.WriteLine("Exception occurred. " + ex.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); }