private IEdmModel LoadAndTransformRsdlModel(string rsdlPath) { var model = parser.Parse(File.ReadAllText(rsdlPath), System.IO.Path.GetFileNameWithoutExtension(rsdlPath)); var referencedModels = new Dictionary <string, RdmDataModel>(); var transformer = new ModelTransformer(NullLogger.Instance); if (transformer.TryTransform(model, referencedModels, out var result)) { // to work arround the problem of comparing generated models with the ones loaded from a file // we will save and load this in-memory model. using (var xml = XmlWriter.Create(rsdlPath + ".actual.csdl.xml")) { CsdlWriter.TryWriteCsdl(result, xml, CsdlTarget.OData, out var errors); } using (var reader = XmlReader.Create(rsdlPath + ".actual.csdl.xml")) { var loaded = CsdlReader.Parse(reader); return(loaded); } } else { throw new System.Exception("failed to transform model"); } }
/// <summary> /// Converts a RAPID pro schema definition language file (.rsdl) to a CSDL file /// </summary> /// <param name="inputRsdl">reader to read the RSDL to parse</param> /// <param name="output">stream to write the output to</param> /// <param name="format">indicates whether it should be written as XML or JSON CSDL, or OpenAPI</param> /// <param name="modelName">the name of the model being converted</param> public async Task <bool> Convert(TextReader inputRsdl, Stream output, OutputFormat format, string modelName = "RapidModel") { // create parser and model transformer. RdmDataModel model; var parser = new RdmParser(logger); try { var rsdl = await inputRsdl.ReadToEndAsync(); model = parser.Parse(rsdl, modelName); } catch (Exception ex) { logger.LogError(ex, $"error parsing rsdl stream"); return(false); } // todo: Load referenced EDM models // transform into CSDL var transformer = new ModelTransformer(logger); if (transformer.TryTransform(model, null, out var csdlModel)) { return(await WriteOutput(csdlModel, output, format)); } else { return(false); } }
private rdm.RdmDataModel ParseFile(RdmParser parser, string path) { try { var content = File.ReadAllText(path); var model = parser.Parse(content, Path.GetFileName(path)); return(model); } catch (Exception ex) { logger.LogError(ex, $"error parsing rsdl file {path}"); return(default);
private IEdmModel CreateEdmModelFromString(string text) { var model = parser.Parse(text, "main"); var referencedModels = new Dictionary <string, RdmDataModel>(); var transformer = new ModelTransformer(NullLogger.Instance); if (transformer.TryTransform(model, referencedModels, out var result)) { return(result); } else { throw new System.Exception("failed to transform model"); } }
public void TypesFromReferencedModelsAreResolveable() { // arrange var mainText = @"include ""common"" as common type Employee { employmentType: common.EmploymentType }"; var inclText = "namespace com.example.common enum EmploymentType { }"; var main = parser.Parse(mainText, "main"); var incl = parser.Parse(inclText, "incl"); var env = new TypeEnvironment(NullLogger.Instance); env.AddReferences(main, new Dictionary <string, RdmDataModel> { ["common"] = incl }); // act var actual = env.ResolveTypeReference(new RdmTypeReference("common.EmploymentType")); // assert // note that the enumeration has zero members var expected = new EdmEnumTypeReference(new EdmEnumType("com.example.common", "EmploymentType"), false); Assert.Equal(expected.FullName(), actual.FullName()); }
private RdmDataModel LoadModel(string path, RdmParser parser, string baseDirectory) { if (!Path.IsPathRooted(path)) { path = Path.Combine(baseDirectory, path); } if (Path.GetExtension(path) == "") { path = path + ".rsdl"; } ; var model = parser.Parse(File.ReadAllText(path), Path.GetFileName(path)); logger.LogInfo("loaded referenced model file {0} containing namespace {1}", path, model.Namespace.NamespaceName); return(model); }
public void SinglePropertyGetsParsed() { var content = @"type Thing { id: String } "; var actual = parser.Parse(content, "test"); var expected = new RdmDataModel(null, new IRdmSchemaElement[] { new RdmStructuredType("Thing", null, new [] { new RdmProperty("id", new RdmTypeReference("String"), false) }) }); Assert.Equal(expected, actual); }