Skip to content

Commit

Permalink
Augmented Namespaces working.
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed Jun 3, 2024
1 parent aec7753 commit 428a714
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 13 deletions.
15 changes: 13 additions & 2 deletions YangParser/SemanticModel/Augment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion YangParser/SemanticModel/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down Expand Up @@ -69,7 +70,7 @@ public Module(YangStatement statement) : base(statement)
}

public List<Feature> Features { get; } = [];

public Dictionary<string, string> PrefixToNamespaceTable { get; } = [];
public Dictionary<string, string> ImportedModules { get; } = [];
public Dictionary<string, string> Usings { get; }
public string MyNamespace { get; private set; }
Expand Down
19 changes: 9 additions & 10 deletions YangParser/SemanticModel/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}\"";

Expand Down Expand Up @@ -110,11 +109,11 @@ protected string ReadFunctionWithInvisibleSelf()
return ReadFunctionWithInvisibleSelf(type);
}

protected string ReadFunctionWithInvisibleSelf(string type)
private string ReadFunctionWithInvisibleSelf(string type)
{
List<string> declarations = new List<string>();
List<string> assignments = new List<string>();
List<string> cases = new List<string>();
var declarations = new List<string>();
var assignments = new List<string>();
var cases = new List<string>();
CollectParsingChildren(declarations, assignments, cases, "break");

if (cases.Count > 0)
Expand All @@ -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)))}}
};
}
""";
Expand Down Expand Up @@ -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; }
Expand Down
61 changes: 61 additions & 0 deletions YangSourceTests/IetfInterfacesTests.cs
Original file line number Diff line number Diff line change
@@ -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<YangNode.InterfacesContainer.InterfaceEntry>
{
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());
}
}

0 comments on commit 428a714

Please sign in to comment.