Skip to content

Commit

Permalink
Modules are serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed Jun 3, 2024
1 parent 428a714 commit 1413e70
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Leaf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override string ToCode()
var defaultValue = Default?.ToCode();

var defaulting = defaultValue is null ? string.Empty : $"= {defaultValue};";
var nullable = Required ? string.Empty : "?";
var nullable = Required && !Children.Any(c => c is When) ? string.Empty : "?";
var name = MakeName(Argument);
var typeName = Type.Name;
var definition = Type.Definition;
Expand Down
6 changes: 5 additions & 1 deletion YangParser/SemanticModel/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace YangParser.SemanticModel;

public class Module : Statement, ITopLevelStatement
public class Module : Statement, ITopLevelStatement, IXMLParseable
{
public Module(YangStatement statement) : base(statement)
{
Expand Down Expand Up @@ -130,6 +130,8 @@ public class YangNode
{
{{string.Join("\n\t", nodes)}}
{{string.Join("\n\t", extraDefinitions)}}
{{Indent(ReadFunction())}}
{{Indent(WriteFunction())}}
}
""";
raw = ReplacePrefixes(raw);
Expand Down Expand Up @@ -216,6 +218,8 @@ private void ExpandPrefixes(IStatement statement)
// return builder.ToString();
// }
// }
public string? TargetName => MakeName(Argument);
public string ClassName => "YangNode";
}

public interface ITopLevelStatement : IStatement
Expand Down
28 changes: 15 additions & 13 deletions YangParser/SemanticModel/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ protected string ReadFunction()

protected string ReadFunction(string type)
{
List<string> declarations = new List<string>();
List<string> assignments = new List<string>();
List<string> cases = new List<string>();
CollectParsingChildren(declarations, assignments, cases, "continue");
var declarations = new List<string>();
var assignments = new List<string>();
var cases = new List<string>();
HashSet<string> caseKeywords = [];
CollectParsingChildren(declarations, assignments, cases, caseKeywords, "continue");

return $$"""
public static async Task<{{type}}> ParseAsync(XmlReader reader)
Expand Down Expand Up @@ -114,7 +115,8 @@ private string ReadFunctionWithInvisibleSelf(string type)
var declarations = new List<string>();
var assignments = new List<string>();
var cases = new List<string>();
CollectParsingChildren(declarations, assignments, cases, "break");
HashSet<string> caseKeywords = [];
CollectParsingChildren(declarations, assignments, cases, caseKeywords, "break");

if (cases.Count > 0)
{
Expand Down Expand Up @@ -143,6 +145,7 @@ private string ReadFunctionWithInvisibleSelf(string type)
}

private void CollectParsingChildren(List<string> declarations, List<string> assignments, List<string> cases,
HashSet<string> caseKeywords,
string escapeKeyword)
{
foreach (var child in Children)
Expand All @@ -169,6 +172,8 @@ private void CollectParsingChildren(List<string> declarations, List<string> assi
""");
break;
}

caseKeywords.Add(child.Argument);
}
}

Expand Down Expand Up @@ -420,18 +425,15 @@ protected static string TypeName(Type type)

public string AttributeString
{
get
{
if (this is IXMLWriteValue || this is IXMLSource) Attributes.Add("XPath(@\"" + XPath + '"' + ')');
return "\n" + string.Join("\n", Attributes.OrderBy(x => x.Length).Select(attr => $"[{attr}]"));
}
get { return "\n" + string.Join("\n", Attributes.OrderBy(x => x.Length).Select(attr => $"[{attr}]")); }
}

public string DescriptionString => $"""
public string DescriptionString => Children.FirstOrDefault(c => c is Description) is Description description ? $"""
///<summary>
///{Children.FirstOrDefault(child => child is Description)?.Argument.Replace("\n", "\n///")}
///{description.Argument.Replace("\n", "\n///")}
///</summary>
""";
"""
: string.Empty;

/// <summary>
///
Expand Down
30 changes: 23 additions & 7 deletions YangSourceTests/IetfInterfacesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ namespace YangSourceTests;

public class IetfInterfacesTests(ITestOutputHelper output)
{
[Fact]
public async Task AugmentedSerializationTest()
private static readonly YangNode node = new()
{
var node = new YangNode.InterfacesContainer
Interfaces = new()
{
Interface = new List<YangNode.InterfacesContainer.InterfaceEntry>
{
new()
{
TypeValue = "vlan",
ChannelSeparation = 0,
ModeValue = "yes",
PowerMode = new YangNode.InterfacesContainer.InterfaceEntry.PowerModeChoice
{
Atpc =
Expand Down Expand Up @@ -47,15 +45,33 @@ public async Task AugmentedSerializationTest()
{
RxFrequency = 402,
},
BridgePort = new YangNode.InterfacesContainer.InterfaceEntry.BridgePortContainer()

BridgePort = new YangNode.InterfacesContainer.InterfaceEntry.BridgePortContainer(),
ModeValue = "",
}
}
};
}
};

[Fact]
public async Task AugmentedSerializationTest()
{
var builder = new StringBuilder();
await using var writer = XmlWriter.Create(builder, SerializationHelper.GetStandardWriterSettings());
await node.WriteXMLAsync(writer);
await writer.FlushAsync();
output.WriteLine(builder.ToString());
}
// [Fact]
// public async Task AugmentedDeserializationTest()
// {
// var builder = new StringBuilder();
// await using var writer = XmlWriter.Create(builder, SerializationHelper.GetStandardWriterSettings());
// await node.WriteXMLAsync(writer);
// await writer.FlushAsync();
// output.WriteLine(builder.ToString());
// using var ms = new MemoryStream(Encoding.UTF8.GetBytes(builder.ToString()));
// using var reader = XmlReader.Create(ms, SerializationHelper.GetStandardReaderSettings());
// await reader.ReadAsync();
// var nNode = await YangNode.ParseAsync(reader);
// }
}
2 changes: 2 additions & 0 deletions yang-compiler.sln.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
&lt;TestId&gt;xUnit::D7772430-87F7-4707-8BC1-4F5C601C4AD3::net8.0::YangSourceTests.BfdIpMhTests.NotificationDeserializationTest&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::D7772430-87F7-4707-8BC1-4F5C601C4AD3::net8.0::YangSourceTests.BfdIpMhTests&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::D7772430-87F7-4707-8BC1-4F5C601C4AD3::net8.0::YangSourceTests.RpcTests.RpcSend&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::D7772430-87F7-4707-8BC1-4F5C601C4AD3::net8.0::YangSourceTests.IetfInterfacesTests.AugmentedSerializationTest&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::D7772430-87F7-4707-8BC1-4F5C601C4AD3::net8.0::YangSourceTests.IetfInterfacesTests.AugmentedDeserializationTest&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/Or&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>

0 comments on commit 1413e70

Please sign in to comment.