Skip to content

Commit

Permalink
Merge pull request #788 from EdwardCooke/ec-740-nested
Browse files Browse the repository at this point in the history
Support nested classes in the static code generator

+semver:fix
  • Loading branch information
EdwardCooke committed Mar 11, 2023
2 parents c44a46b + ec8d51d commit f460b5c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions YamlDotNet.Analyzers.StaticGenerator/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ static string GetFullName(this INamedTypeSymbol symbol)
var space = GetNamespace(symbol);
var suffix = GetGenericTypes(symbol.TypeArguments); ;

var containingSymbol = symbol.ContainingType;
while (containingSymbol != null)
{
if (string.IsNullOrWhiteSpace(space))
{
space = containingSymbol.Name;
}
else
{
space = $"{space}.{containingSymbol.Name}";
}

containingSymbol = containingSymbol.ContainingType;
}

if (!string.IsNullOrWhiteSpace(space))
{
return space + "." + symbol.Name + suffix + symbol.GetNullable();
Expand Down
13 changes: 13 additions & 0 deletions YamlDotNet.Test/Analyzers/StaticGenerator/ObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public void RegularObjectWorks()
Inner:
Prop1: a
Prop2: 2
Nested:
NestedProp: abc
";
var actual = deserializer.Deserialize<RegularObjectOuter>(yaml);
Assert.Equal("hello", actual.Prop1);
Expand All @@ -59,6 +61,8 @@ public void RegularObjectWorks()
Assert.NotNull(actual.Inner);
Assert.Equal("a", actual.Inner.Prop1);
Assert.Equal(2, actual.Inner.Prop2);
Assert.NotNull(actual.Nested);
Assert.Equal("abc", actual.Nested.NestedProp);

var serializer = new StaticSerializerBuilder(new StaticContext()).Build();
var actualYaml = serializer.Serialize(actual);
Expand All @@ -69,6 +73,8 @@ public void RegularObjectWorks()
Inner:
Prop1: a
Prop2: 2
Nested:
NestedProp: abc
";
Assert.Equal(yaml.NormalizeNewLines().TrimNewLines(), actualYaml.NormalizeNewLines().TrimNewLines());
}
Expand All @@ -94,6 +100,13 @@ public class RegularObjectOuter
[YamlIgnore]
public string Ignored { get; set; } = "I am ignored";
public RegularObjectInner Inner { get; set; }
public NestedClass Nested { get; set; }

[YamlSerializable]
public class NestedClass
{
public string NestedProp { get; set; }
}
}

[YamlSerializable]
Expand Down

0 comments on commit f460b5c

Please sign in to comment.