From 428a714d868f60aee7ea8c8a11963d5d8a4a7516 Mon Sep 17 00:00:00 2001 From: caran Date: Mon, 3 Jun 2024 15:42:48 +0200 Subject: [PATCH] Augmented Namespaces working. --- YangParser/SemanticModel/Augment.cs | 15 ++++++- YangParser/SemanticModel/Module.cs | 3 +- YangParser/SemanticModel/Statement.cs | 19 ++++---- YangSourceTests/IetfInterfacesTests.cs | 61 ++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 YangSourceTests/IetfInterfacesTests.cs diff --git a/YangParser/SemanticModel/Augment.cs b/YangParser/SemanticModel/Augment.cs index ee27022..33aaaf1 100644 --- a/YangParser/SemanticModel/Augment.cs +++ b/YangParser/SemanticModel/Augment.cs @@ -110,7 +110,7 @@ private IStatement GetTarget(IStatement top, string[] components, string sourceN foreach (var xpath in components) { if (string.IsNullOrWhiteSpace(xpath)) continue; - xpath.Prefix(out var childName); + var prefix = xpath.Prefix(out var childName); var origin = current; foreach (var currentChild in current.Children) { @@ -123,7 +123,10 @@ private IStatement GetTarget(IStatement top, string[] components, string sourceN if (current == origin) { - var newChild = new Container(childName, Source.Metadata); + var newChild = new Container(childName, Source.Metadata) + { + XmlNamespace = (this.FindSourceFor(prefix)?.XmlNamespace?.Namespace, prefix)! + }; newChild.Attributes.Add($"Augmented(\"{sourceNS}\")"); current.Insert([newChild]); current = newChild; @@ -204,6 +207,14 @@ private void PrepareTransitionTo(IStatement top) target.ImportedModules[pair.Key] = pair.Value; } } + + foreach (var pair in source.PrefixToNamespaceTable) + { + if (!target.PrefixToNamespaceTable.ContainsKey(pair.Key)) + { + target.PrefixToNamespaceTable[pair.Key] = pair.Value; + } + } } } } diff --git a/YangParser/SemanticModel/Module.cs b/YangParser/SemanticModel/Module.cs index 0b48dde..c58ffe7 100644 --- a/YangParser/SemanticModel/Module.cs +++ b/YangParser/SemanticModel/Module.cs @@ -20,6 +20,7 @@ public Module(YangStatement statement) : base(statement) }; ImportedModules[localPrefix] = Argument; MyNamespace = localNS; + PrefixToNamespaceTable[localPrefix] = XmlNamespace.Value.Namespace; foreach (var child in this.Unwrap()) { @@ -69,7 +70,7 @@ public Module(YangStatement statement) : base(statement) } public List Features { get; } = []; - + public Dictionary PrefixToNamespaceTable { get; } = []; public Dictionary ImportedModules { get; } = []; public Dictionary Usings { get; } public string MyNamespace { get; private set; } diff --git a/YangParser/SemanticModel/Statement.cs b/YangParser/SemanticModel/Statement.cs index a2d5d6f..754d4f6 100644 --- a/YangParser/SemanticModel/Statement.cs +++ b/YangParser/SemanticModel/Statement.cs @@ -5,13 +5,12 @@ using System.Text.RegularExpressions; using YangParser.Parser; using YangParser.SemanticModel.Builtins; -using String = System.String; namespace YangParser.SemanticModel; public abstract class Statement : IStatement { - protected string xmlPrefix => (string.IsNullOrWhiteSpace(Prefix) || string.IsNullOrWhiteSpace(Namespace)) + protected string xmlPrefix => string.IsNullOrWhiteSpace(Prefix) || string.IsNullOrWhiteSpace(Namespace) ? "null" : $"\"{Prefix}\""; @@ -110,11 +109,11 @@ protected string ReadFunctionWithInvisibleSelf() return ReadFunctionWithInvisibleSelf(type); } - protected string ReadFunctionWithInvisibleSelf(string type) + private string ReadFunctionWithInvisibleSelf(string type) { - List declarations = new List(); - List assignments = new List(); - List cases = new List(); + var declarations = new List(); + var assignments = new List(); + var cases = new List(); CollectParsingChildren(declarations, assignments, cases, "break"); if (cases.Count > 0) @@ -125,11 +124,11 @@ protected string ReadFunctionWithInvisibleSelf(string type) {{Indent(string.Join("\n", declarations))}} switch(reader.Name) { - {{((Indent(Indent(Indent(string.Join("\n", cases))))))}} + {{Indent(Indent(string.Join("\n", cases)))}} default: throw new Exception($"Unexpected element '{reader.Name}' under '{{Argument}}'"); } return new {{type}}{ - {{((Indent(Indent(Indent(string.Join("\n", assignments))))))}} + {{Indent(Indent(string.Join("\n", assignments)))}} }; } """; @@ -313,9 +312,9 @@ protected Statement(YangStatement statement, bool validate = true) public (string Namespace, string Prefix)? XmlNamespace { get; set; } public string Prefix => XmlNamespace?.Prefix ?? Parent?.Prefix ?? string.Empty; public string Namespace => XmlNamespace?.Namespace ?? Parent?.Namespace ?? string.Empty; - protected string XmlObjectName => (string.IsNullOrEmpty(Prefix) ? string.Empty : Prefix + ":") + Source.Argument; + private string XmlObjectName => (string.IsNullOrEmpty(Prefix) ? string.Empty : Prefix + ":") + Source.Argument; - public string XPath => ((Parent?.XPath ?? String.Empty) + "/").Replace("//", "/") + + public string XPath => ((Parent?.XPath ?? string.Empty) + "/").Replace("//", "/") + XmlObjectName; public YangStatement Source { get; set; } diff --git a/YangSourceTests/IetfInterfacesTests.cs b/YangSourceTests/IetfInterfacesTests.cs new file mode 100644 index 0000000..0c4a8a6 --- /dev/null +++ b/YangSourceTests/IetfInterfacesTests.cs @@ -0,0 +1,61 @@ +using System.Text; +using System.Xml; +using Ietf.Interfaces; +using Xunit.Abstractions; +using Yang.Attributes; + +namespace YangSourceTests; + +public class IetfInterfacesTests(ITestOutputHelper output) +{ + [Fact] + public async Task AugmentedSerializationTest() + { + var node = new YangNode.InterfacesContainer + { + Interface = new List + { + new() + { + TypeValue = "vlan", + ChannelSeparation = 0, + ModeValue = "yes", + PowerMode = new YangNode.InterfacesContainer.InterfaceEntry.PowerModeChoice + { + Atpc = + new YangNode.InterfacesContainer.InterfaceEntry.PowerModeChoice. + AtpcContainer + { + AtpcLowerThreshold = 0, + AtpcUpperThreshold = 1, + MaximumNominalPower = 2, + } + }, + TxFrequency = 2, + CodingModulationMode = + new YangNode.InterfacesContainer.InterfaceEntry.CodingModulationModeChoice + { + Single = + new YangNode.InterfacesContainer.InterfaceEntry. + CodingModulationModeChoice.SingleContainer() + { + SelectedCmValue = "cm" + } + }, + FreqOrDistance = + new YangNode.InterfacesContainer.InterfaceEntry.FreqOrDistanceChoice + { + RxFrequency = 402, + }, + BridgePort = new YangNode.InterfacesContainer.InterfaceEntry.BridgePortContainer() + + } + } + }; + var builder = new StringBuilder(); + await using var writer = XmlWriter.Create(builder, SerializationHelper.GetStandardWriterSettings()); + await node.WriteXMLAsync(writer); + await writer.FlushAsync(); + output.WriteLine(builder.ToString()); + } +} \ No newline at end of file