Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV-29934: Enable template helper to read formatversion G1.0a etc. #115

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions EDILibrary/TemplateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace EDILibrary
{
public class TemplateHelper
{
// Todo @JoschaMetze: Add a docstring
protected static void ParseAPERAKString(string aperak, out string dataType, out int length,

private static readonly Regex FormatVersionRegex = new(@"^(?<format>[A-Z]{6,7})(?<version>[A-Z]?\d+\.\d+[a-z]?)$");

private static void ParseAPERAKString(string aperak, out string dataType, out int length,
out List<string> list)
{
dataType = null;
Expand Down Expand Up @@ -44,8 +46,8 @@ protected static void ParseAPERAKString(string aperak, out string dataType, out
}
}

// Todo @JoschaMetze: Add a docstring
protected static void Recurse(XElement cur, JArray refObj, TreeElement tree)

private static void Recurse(XElement cur, JArray refObj, TreeElement tree)
{
var i = cur.Descendants("field").Count(d => d.Parent == cur);
var hasClass = cur.Descendants("class").Any(d => d.Parent == cur);
Expand Down Expand Up @@ -136,7 +138,7 @@ protected static void Recurse(XElement cur, JArray refObj, TreeElement tree)
{
meta.Add("sg", elem.Attribute("meta.sg").Value);
}
//check for parent sg
//check for parent sg
else if (cur.Attribute("ref") != null)
{
meta.Add("sg", cur.Attribute("ref").Value);
Expand Down Expand Up @@ -442,17 +444,37 @@ public string ConvertFilesToJSON(string inputFileName, string outputFileName, st
{
tree = new GenericEDILoader().LoadTree(File.ReadAllText(treeFileName));
}

var tmp = new JArray();
Recurse(srcXml, tmp, tree);
//retriev version information from inputFileName
var part = inputFileName.Split(new[] { ".template" }, StringSplitOptions.None)[0].Split(System.IO.Path.DirectorySeparatorChar).Last();
var version = part.Substring(part.IndexOf('.') - 1);
string version = RetrieveFormatVersionFromInputFileName(inputFileName);
((tmp[0] as JObject)["_meta"] as JObject).Add("version", version);
File.WriteAllText(outputFileName, JsonConvert.SerializeObject(tmp));
return JsonConvert.SerializeObject(tmp);

}

/// <summary>
/// Extract the Format version (e.g. "1.0a") from a given template file name (e.g. "COMDIS1.0a.template")
/// </summary>
/// <param name="inputFileName">path of name of the template file</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static string RetrieveFormatVersionFromInputFileName(string inputFileName)
{
var actualFileName = inputFileName.Split(new[] { ".template" }, StringSplitOptions.None)[0].Split(Path.DirectorySeparatorChar).Last();
var match = FormatVersionRegex.Match(actualFileName);
if (match.Success)
{
return match.Groups["version"].Value;
}

if (inputFileName.Contains(".create.template"))
{
throw new ArgumentException("Pass the name of the \".template\" file, not \".create.template\"", nameof(inputFileName));
}
throw new ArgumentException($"Format version could not be determined: {actualFileName}", nameof(inputFileName));
}

// the format version will be written to the json template file, thus needs to be passed
public string ConvertToJSON(string xmlTemplate, string treeTemplate, string formatVersion)
{
Expand Down
27 changes: 27 additions & 0 deletions EDILibraryTests/TemplateHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using EDILibrary;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EDILibraryTests;

[TestClass]
public class TemplateHelperTests
{
[TestMethod]
[DataRow("COMDIS1.0c.template", "1.0c")]
[DataRow("UTILMDGG1.0a.template", "G1.0a")]
public void Test_RetrieveFormatVersionFromInputFileName(string filename, string expectedFormatVersion)
{
var actualFormatVersion = TemplateHelper.RetrieveFormatVersionFromInputFileName(filename);
Assert.AreEqual(expectedFormatVersion, actualFormatVersion);
}

[TestMethod]
public void Test_RetrieveFormatVersionFromInputFileName_ForPaths()
{
var filepathAsString = $"Path{Path.DirectorySeparatorChar}To{Path.DirectorySeparatorChar}My{Path.DirectorySeparatorChar}Favourite{Path.DirectorySeparatorChar}Templates{Path.DirectorySeparatorChar}folder{Path.DirectorySeparatorChar}COMDIS1.0c.template";
var actualFormatVersion = TemplateHelper.RetrieveFormatVersionFromInputFileName(filepathAsString);
Assert.AreEqual("1.0c", actualFormatVersion);
}

}
Loading