Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
carl-andersson-at-westermo committed Jun 18, 2024
1 parent e914165 commit cb392f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
42 changes: 22 additions & 20 deletions dotnetYang/SemanticModel/Builtins/LeafReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,41 @@ public class LeafReference() : BuiltinType("leafref", (statement) =>
assignation = string.Empty;
}

var type = target!.GetChild<Type>();
var bname = target switch
{
IXMLReadValue rw => rw.ClassName,
IXMLParseable ps => ps.ClassName,
_ => "string"
};
if (bname.Contains(":") || bname.Contains("."))
if(target.TryGetChild<Type>(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))

Check warning on line 39 in dotnetYang/SemanticModel/Builtins/LeafReference.cs

View workflow job for this annotation

GitHub Actions / pack-test

Dereference of a possibly null reference.

Check warning on line 39 in dotnetYang/SemanticModel/Builtins/LeafReference.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Dereference of a possibly null reference.

Check warning on line 39 in dotnetYang/SemanticModel/Builtins/LeafReference.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 39 in dotnetYang/SemanticModel/Builtins/LeafReference.cs

View workflow job for this annotation

GitHub Actions / pack-release

Dereference of a possibly null reference.

Check warning on line 39 in dotnetYang/SemanticModel/Builtins/LeafReference.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
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);
});
11 changes: 7 additions & 4 deletions dotnetYang/SemanticModel/StatementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ public static T GetChild<T>(this IStatement statement) where T : class, IStateme
return (T)statement.Children.First(c => c is T);
}

public static bool TryGetChild<T>(this IStatement statement, out T? child) where T : class, IStatement
public static bool TryGetChild<T>(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;
Expand Down Expand Up @@ -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<Type>();
continue;
if (target.TryGetChild<Type>(out var targetType))
{
type = targetType!;
continue;
}
}

chosenType = type;
Expand Down

0 comments on commit cb392f1

Please sign in to comment.