Skip to content

Commit

Permalink
Started work on XML Serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed May 30, 2024
1 parent fb7680e commit fae9fb0
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 7 deletions.
2 changes: 1 addition & 1 deletion YangParser/SemanticModel/Builtins/Binary.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace YangParser.SemanticModel.Builtins;

public class Binary() : BuiltinType("binary", (_) => ("byte[]", null));
public class Binary() : BuiltinType("binary", _ => ("byte[]", null));
10 changes: 8 additions & 2 deletions YangParser/SemanticModel/Builtins/BuiltinTypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static bool IsBuiltin(Type type, out string? cSharpType, out string? defi

return false;
}

public static bool IsBuiltinKeyword(string keyword)
{
return m_builtIns.Any(b => b.Name == keyword);
Expand All @@ -58,17 +59,21 @@ public class {{typeName}}
{
{{Statement.Indent(string.Join("\n", staticFields))}}
public {{baseTypeName}} WrittenValue { get; }
public static implicit operator {{baseTypeName}}({{typeName}} input) => input.WrittenValue;
public static implicit operator {{baseTypeName}}?({{typeName}}? input) => input?.WrittenValue;
public static implicit operator {{typeName}}({{baseTypeName}} input) => new {{typeName}}(input);
public {{typeName}}({{baseTypeName}} input)
{
{{Statement.Indent(Statement.Indent(string.Join("\n", constructorStatements)))}}
WrittenValue = input;
}
public override string ToString()
{
return WrittenValue.ToString();
}
}
""";
}

public static string TypeName(IStatement type)
{
string Postfix = string.Empty;
Expand All @@ -78,6 +83,7 @@ public static string TypeName(IStatement type)
Postfix += Array.IndexOf(parent.Children, type);
parent = parent.Parent!;
}

return Statement.MakeName(parent.Argument) + Postfix;
}
}
7 changes: 6 additions & 1 deletion YangParser/SemanticModel/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace YangParser.SemanticModel;

public class Container : Statement, IClassSource
public class Container : Statement, IClassSource, IXMLSource
{
public List<string> Comments { get; } = new();

Expand Down Expand Up @@ -62,14 +62,19 @@ public override string ToCode()
name = "sub-" + name;
}

TargetName = MakeName(name);

string property = $"public{KeywordString}{MakeName(name)}Container? {MakeName(name)} {{ get; set; }}";
return $$"""
{{property}}
{{DescriptionString}}{{AttributeString}}
public class {{MakeName(name)}}Container
{
{{string.Join("\n\t", nodes.Select(Indent))}}
{{Indent(XmlFunction())}}
}
""";
}

public string TargetName { get; private set; }
}
16 changes: 16 additions & 0 deletions YangParser/SemanticModel/IStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public interface IStatement
string Prefix { get; }
}

/// <summary>
/// Implies this class makes an XElement with potential children
/// </summary>
public interface IXMLSource : IStatement
{
string TargetName { get; }
}
/// <summary>
/// Implies this class makes a singular XElement with no children, and may not have a method
/// </summary>
public interface IXMLValue : IStatement
{
string TargetName { get; }
string WriteCall { get; }
}

public interface ICommentable : IStatement
{
List<string> Comments { get; }
Expand Down
22 changes: 21 additions & 1 deletion YangParser/SemanticModel/Leaf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace YangParser.SemanticModel;

public class Leaf : Statement
public class Leaf : Statement, IXMLValue
{
public override ChildRule[] PermittedChildren { get; } =
[
Expand Down Expand Up @@ -66,11 +66,31 @@ public override string ToCode()
name += "Value";
}

TargetName = name;

return $$"""
{{DescriptionString}}{{AttributeString}}
public{{KeywordString}}{{typeName}}{{nullable}} {{name}} { get; set; } {{defaulting}}
{{definition}}
{{Default?.Addendum}}
""";
}

public string TargetName { get; private set; }

public string WriteCall
{
get
{
var pre = string.IsNullOrWhiteSpace(Prefix) ? "null" : $"\"{Prefix}\"";
return $$"""
if({{TargetName}} != default)
{
await writer.WriteStartElementAsync({{pre}},"{{Argument}}",null);
await writer.WriteStringAsync({{TargetName}}!.ToString());
await writer.WriteEndElementAsync();
}
""";
}
}
}
25 changes: 24 additions & 1 deletion YangParser/SemanticModel/LeafList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace YangParser.SemanticModel;

public class LeafList : Statement
public class LeafList : Statement, IXMLValue
{
public override ChildRule[] PermittedChildren { get; } =
[
Expand Down Expand Up @@ -57,6 +57,8 @@ public override string ToCode()
name += "Value";
}

TargetName = name + "List";

return $$"""
{{addendum}}
{{DescriptionString}}{{AttributeString}}
Expand All @@ -65,4 +67,25 @@ public override string ToCode()
{{Default?.Addendum}}
""";
}

public string TargetName { get; private set; }

public string WriteCall
{
get
{
var pre = string.IsNullOrWhiteSpace(Prefix) ? "null" : $"\"{Prefix}\"";
return $$"""
if({{TargetName}} != null)
{
foreach(var element in {{TargetName}})
{
await writer.WriteStartElementAsync({{pre}},"{{Argument}}",null);
await writer.WriteStringAsync(element!.ToString());
await writer.WriteEndElementAsync();
}
}
""";
}
}
}
1 change: 1 addition & 0 deletions YangParser/SemanticModel/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public override string ToCode()
var extraDefinitions = HiddenDefinitions.Select(t => Indent(t.ToCode())).ToArray();
var raw = $$"""
using System;
using System.Xml;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
Expand Down
23 changes: 22 additions & 1 deletion YangParser/SemanticModel/Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ namespace YangParser.SemanticModel;

public abstract class Statement : IStatement
{
protected string XmlFunction()
{
var ns = XmlNamespace?.Namespace;
ns = ns is null ? "null" : $"\"{ns}\"";
var pre = string.IsNullOrWhiteSpace(Prefix) ? "null" : $"\"{Prefix}\"";
var writeCalls = Children.OfType<IXMLSource>()
.Select(t => $"if({t.TargetName} is not null) await {t.TargetName}.WriteXML(writer);");
var elementCalls = Children.OfType<IXMLValue>()
.Select(t => t.WriteCall);
return $$"""
public async Task WriteXML(XmlWriter writer)
{
await writer.WriteStartElementAsync({{pre}},"{{Source.Argument}}",{{ns}});
{{Indent(string.Join("\n", elementCalls))}}
{{Indent(string.Join("\n", writeCalls))}}
await writer.WriteEndElementAsync();
}
""";
}

protected Statement(YangStatement statement, bool validate = true)
{
Argument = statement.Argument?.ToString() ?? string.Empty;
Expand All @@ -26,9 +46,10 @@ protected Statement(YangStatement statement, bool validate = true)

public (string Namespace, string Prefix)? XmlNamespace { get; set; }
public string Prefix => XmlNamespace?.Prefix ?? Parent?.Prefix ?? string.Empty;
protected string XmlObjectName => (string.IsNullOrEmpty(Prefix) ? string.Empty : Prefix + ":") + Source.Argument;

public string XPath => ((Parent?.XPath ?? String.Empty) + "/").Replace("//", "/") +
(string.IsNullOrEmpty(Prefix) ? string.Empty : Prefix + ":") + Source.Argument;
XmlObjectName;

public YangStatement Source { get; set; }

Expand Down

0 comments on commit fae9fb0

Please sign in to comment.