Ejemplo n.º 1
0
        private static Tuple <bool, string> SaveHeaderAndBiFile(string savePath = null)
        {
            var path           = FileManager.StandardPath();
            var xmlInfo        = XmlInfo.GetInstance;
            var schemaCompiler = new SchemaCompiler(new StreamReader(xmlInfo.GetXsdPath()));
            var xmlString      = File.ReadAllText(xmlInfo.GetXmlPath());

            if (savePath != null)
            {
                path = savePath;
            }

            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                File.WriteAllLines(Path.Combine(path, "headerFile.h"), schemaCompiler.GetHeaderFile());

                // load Xml document
                XmlDocument doc = new XmlDocument();
                doc.Load(XmlReader.Create(new StringReader(xmlString)));
                var compiledByteArray = schemaCompiler.Compile(doc);

                File.WriteAllBytes($"{path}/binFile.bin", compiledByteArray);
            }
            catch (Exception ex)
            {
                return(new Tuple <bool, string>(false, $"Error on Write files :  {ex.Message}"));
            }

            return(new Tuple <bool, string>(true, $"Files correctly saved on: {path}"));
        }
        public void TestSchemaMutationType()
        {
            var results          = SchemaCompiler.Compile(File.ReadAllText("../../../schema.graphql"));
            var mutationTypeName = results.Schema.First(s => s.Name == "mutation").TypeName;

            var mutType = results.Types[mutationTypeName];

            Assert.Equal(3, mutType.Fields.Count);
            Assert.Equal("addMovie", mutType.Fields.ElementAt(0).Name);
            Assert.Equal("Add a new Movie object", mutType.Fields.ElementAt(0).Description);
            Assert.Equal("Movie", mutType.Fields.ElementAt(0).TypeName);
            Assert.False(mutType.Fields.ElementAt(0).IsArray);

            Assert.Equal(5, mutType.Fields.ElementAt(0).Args.Count);
            Assert.Equal("name", mutType.Fields.ElementAt(0).Args.ElementAt(0).Name);
            Assert.True(mutType.Fields.ElementAt(0).Args.ElementAt(0).Required);
            Assert.Equal("String", mutType.Fields.ElementAt(0).Args.ElementAt(0).TypeName);
            Assert.Equal("rating", mutType.Fields.ElementAt(0).Args.ElementAt(1).Name);
            Assert.Equal("Float", mutType.Fields.ElementAt(0).Args.ElementAt(1).TypeName);
            Assert.Equal("details", mutType.Fields.ElementAt(0).Args.ElementAt(2).Name);
            Assert.Equal("Detail", mutType.Fields.ElementAt(0).Args.ElementAt(2).TypeName);
            Assert.True(mutType.Fields.ElementAt(0).Args.ElementAt(2).IsArray);
            Assert.Equal("released", mutType.Fields.ElementAt(0).Args.ElementAt(4).Name);
            Assert.Equal("Date", mutType.Fields.ElementAt(0).Args.ElementAt(4).TypeName);
            Assert.False(mutType.Fields.ElementAt(0).Args.ElementAt(4).IsArray);
        }
Ejemplo n.º 3
0
        private static Tuple <bool, string> ValidateXml()
        {
            XmlInfo xmlInfo        = XmlInfo.GetInstance;
            var     schemaCompiler = new SchemaCompiler(new StreamReader(xmlInfo.GetXsdPath()));
            var     xmlString      = File.ReadAllText(xmlInfo.GetXmlPath());

            var validateXml = XmlUtilities.ValidateXmlAgainstXsd(xmlString, schemaCompiler);

            if (!validateXml.Item1)
            {
                return(new Tuple <bool, string>(false, validateXml.Item2));
            }

            return(new Tuple <bool, string>(true, string.Empty));
        }
Ejemplo n.º 4
0
        public Assembly Compile(EmitAssemblyBuilder assembly)
        {
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assembly.Name), AssemblyBuilderAccess.RunAndSave);
            var moduleBuilder   = assemblyBuilder.DefineDynamicModule(assembly.Name, "module.dll", true);

            var compiler = new SchemaCompiler(moduleBuilder);
            var types    = new List <Type>();

            foreach (EmitTypeBuilder type in assembly.Types)
            {
                var typeBuilder = (TypeBuilder)type.Accept(compiler);
                var createdType = typeBuilder.CreateType();
                types.Add(createdType);
            }

            return(types.First().Assembly);
        }
        public void TestSchemaTypeDef()
        {
            var results = SchemaCompiler.Compile(File.ReadAllText("../../../schema.graphql"));

            Assert.Equal(2, results.Schema.Count);
            Assert.Equal(8, results.Types.Count);
            Assert.Single(results.Inputs);
            var queryTypeName    = results.Schema.First(s => s.Name == "query").TypeName;
            var mutationTypeName = results.Schema.First(s => s.Name == "mutation").TypeName;

            var typeDef = results.Types["Movie"];

            Assert.Equal("This is a movie entity", typeDef.Description);
            Assert.Equal(9, typeDef.Fields.Count);
            Assert.Equal("id", typeDef.Fields.ElementAt(0).Name);
            Assert.Equal("String", typeDef.Fields.ElementAt(1).TypeName);
            Assert.False(typeDef.Fields.ElementAt(1).IsArray);
            Assert.Equal("actors", typeDef.Fields.ElementAt(4).Name);
            Assert.Equal("Person", typeDef.Fields.ElementAt(4).TypeName);
            Assert.True(typeDef.Fields.ElementAt(4).IsArray);
        }
        public void TestSchemaQueryType()
        {
            var results = SchemaCompiler.Compile(File.ReadAllText("../../../schema.graphql"));

            Assert.Equal(2, results.Schema.Count);
            Assert.Equal(8, results.Types.Count);
            Assert.Single(results.Inputs);
            var queryTypeName = results.Schema.First(s => s.Name == "query").TypeName;

            var queryType = results.Types[queryTypeName];

            Assert.Equal(8, queryType.Fields.Count);
            Assert.Equal("actors", queryType.Fields.ElementAt(1).Name);
            Assert.Equal("Person", queryType.Fields.ElementAt(1).TypeName);
            Assert.True(queryType.Fields.ElementAt(1).IsArray);
            Assert.Equal("person", queryType.Fields.ElementAt(6).Name);
            Assert.Equal("Person", queryType.Fields.ElementAt(6).TypeName);
            Assert.False(queryType.Fields.ElementAt(6).IsArray);
            Assert.Single(queryType.Fields.ElementAt(6).Args);
            Assert.Equal("id", queryType.Fields.ElementAt(6).Args.First().Name);
            Assert.Equal("Int", queryType.Fields.ElementAt(6).Args.First().TypeName);
            Assert.True(queryType.Fields.ElementAt(6).Args.First().Required);
        }
Ejemplo n.º 7
0
        public static Tuple <bool, string> ValidateXmlAgainstXsd(string xml, SchemaCompiler schemaCompiler)
        {
            try
            {
                // load Xml document
                XmlDocument doc = new XmlDocument();
                doc.Load(XmlReader.Create(new StringReader(xml)));

                // validate against XSD
                var list = schemaCompiler.ValidateXml(doc);
                if (list.Any(e => e.Severity == XmlSeverityType.Error))
                {
                    return(new Tuple <bool, string>(false, $"XSD validation fail: {list[0].Message}"));
                }
            }
            catch (Exception e)
            {
                // the XML file is not valid
                return(new Tuple <bool, string>(false, $"XML not valid: {e}"));
            }

            return(new Tuple <bool, string>(true, string.Empty));
        }
 public void TestSchemaQueryRequired()
 {
     Assert.Throws <SchemaException>(() => SchemaCompiler.Compile("schema { mutation: Mutation }"));
 }