diff --git a/dotnetYang/SemanticModel/Builtins/LeafReference.cs b/dotnetYang/SemanticModel/Builtins/LeafReference.cs index c39c00a..ea4bdfe 100644 --- a/dotnetYang/SemanticModel/Builtins/LeafReference.cs +++ b/dotnetYang/SemanticModel/Builtins/LeafReference.cs @@ -16,39 +16,41 @@ public class LeafReference() : BuiltinType("leafref", (statement) => assignation = string.Empty; } - var type = target!.GetChild(); var bname = target switch { IXMLReadValue rw => rw.ClassName, IXMLParseable ps => ps.ClassName, _ => "string" }; - if (bname.Contains(":") || bname.Contains(".")) + if(target.TryGetChild(out var type)) { - var p = bname.Prefix(out _); - if (bname.Contains(":") && !statement.GetModule()!.Usings.ContainsKey(p)) + if (bname.Contains(":") || bname.Contains(".")) { - statement.GetModule()!.Usings[p] = target.GetModule()!.Usings[p]; + var p = bname.Prefix(out _); + if (bname.Contains(":") && !statement.GetModule()!.Usings.ContainsKey(p)) + { + statement.GetModule()!.Usings[p] = target.GetModule()!.Usings[p]; + } + + return (bname, null); } - return (bname, null); - } + if (bname == "string") return (bname, null); + if (type.Definition is null && !BuiltinTypeReference.IsBuiltinKeyword(type.Argument)) + { + return (target.ModuleQualifiedClassName(), null); + } - if (bname == "string") return (bname, null); - if (type.Definition is null && !BuiltinTypeReference.IsBuiltinKeyword(type.Argument)) - { - return (target.ModuleQualifiedClassName(), null); - } + if (string.IsNullOrWhiteSpace(prefix) && !BuiltinTypeReference.IsBuiltinKeyword(type.Argument)) + { + return (target.FullyQualifiedClassName(), null); + } - if (string.IsNullOrWhiteSpace(prefix) && !BuiltinTypeReference.IsBuiltinKeyword(type.Argument)) - { - return (target.FullyQualifiedClassName(), null); - } + if (BuiltinTypeReference.IsBuiltin(type, out var name, out var def)) + { + return def is null ? (name!, null) : (target.FullyQualifiedClassName(), null); + } - if (BuiltinTypeReference.IsBuiltin(type, out var name, out var def)) - { - return def is null ? (name!, null) : (target.FullyQualifiedClassName(), null); } - return (assignation + bname, null); }); \ No newline at end of file diff --git a/dotnetYang/SemanticModel/StatementExtensions.cs b/dotnetYang/SemanticModel/StatementExtensions.cs index a7d4229..1dd658b 100644 --- a/dotnetYang/SemanticModel/StatementExtensions.cs +++ b/dotnetYang/SemanticModel/StatementExtensions.cs @@ -176,10 +176,10 @@ public static T GetChild(this IStatement statement) where T : class, IStateme return (T)statement.Children.First(c => c is T); } - public static bool TryGetChild(this IStatement statement, out T? child) where T : class, IStatement + public static bool TryGetChild(this IStatement? statement, out T? child) where T : class, IStatement { child = null; - if (statement.Children.FirstOrDefault(c => c is T) is T chosen) + if (statement?.Children.FirstOrDefault(c => c is T) is T chosen) { child = chosen; return true; @@ -469,8 +469,11 @@ public static string GetBaseType(this Type type, out string prefix, out Type cho { var path = (Path)type.Children.First(c => c is Path); var target = type.Parent.Navigate(path.Argument); - type = target!.GetChild(); - continue; + if (target.TryGetChild(out var targetType)) + { + type = targetType!; + continue; + } } chosenType = type;