Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Immutable collection interface and generator bug fixes and improvements #31

Merged
merged 4 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ These types can be serialized/deserialized by default as properties of custom `c
* `KeyValuePair<>`, `Tuple<,...>`, `ValueTuple<,...>`
* `OrderedDictionary<>`, `ReadOnlySet<>`
* `ImmutableArray<>`, `ImmutableList<>`, `ImmutableStack<>`, `ImmutableQueue<>`, `ImmutableHashSet<>`, `ImmutableSortedSet<>`, `ImmutableDictionary<>`, `ImmutableSortedDictionary<>`
* `IImmutableList<>`, `IImmutableStack<>`, `IImmutableQueue<>`, `IImmutableSet<>`, `IImmutableDictionary<>`

Customize Formatter
---
Expand Down
39 changes: 33 additions & 6 deletions sandbox/ConsoleNativeAOT/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Xml.Linq;
using System.Collections.ObjectModel;
using CsToml.Values;
using System.Collections.Immutable;

Console.WriteLine("Hello, World!");

Expand Down Expand Up @@ -120,7 +121,7 @@
{
new TestStruct() { Value = 999, Str = "Test" },
new TestStruct() { Value = 999, Str = "Test" },
},
}.ToImmutableHashSet(),
TestStruct2 = new Custom() { Value = 12345 }
};

Expand All @@ -133,10 +134,37 @@
var testEnum = CsTomlSerializer.Deserialize<TestEnum>(@"color = ""Red"""u8);
Console.WriteLine(testEnum.color);

//
var sample = CsTomlSerializer.Deserialize<Sample>(@"Key = 12345"u8);
Console.WriteLine($"Sample.Custom = {sample.Key.Value}");

var type = new TypeImmutable()
{
ImmutableArray = [new TypeTable() { Value = "[1] This is TypeTable in ImmutableArray" },
new TypeTable() { Value = "[2] This is TypeTable in ImmutableArray" },
new TypeTable() { Value = "[3] This is TypeTable in ImmutableArray" }],
ImmutableList = [new TypeTable() { Value = "[1] This is TypeTable in ImmutableList" },
new TypeTable() { Value = "[2] This is TypeTable in ImmutableList" },
new TypeTable() { Value = "[3] This is TypeTable in ImmutableList" }],
IImmutableList = [new TypeTable() { Value = "[1] This is TypeTable in IImmutableList" },
new TypeTable() { Value = "[2] This is TypeTable in IImmutableList" },
new TypeTable() { Value = "[3] This is TypeTable in IImmutableList" }],
};

Console.WriteLine($"Check TypeImmutable");
using var bytes = CsTomlSerializer.Serialize(type);
var typeImmutable2 = CsTomlSerializer.Deserialize<TypeImmutable>(bytes.ByteSpan);
Console.WriteLine($"{nameof(typeImmutable2.ImmutableArray)}.Length = {typeImmutable2.ImmutableArray.Length}");
Console.WriteLine($"{nameof(typeImmutable2.ImmutableArray)}[0] = {typeImmutable2.ImmutableArray[0].Value}");
Console.WriteLine($"{nameof(typeImmutable2.ImmutableArray)}[1] = {typeImmutable2.ImmutableArray[1].Value}");
Console.WriteLine($"{nameof(typeImmutable2.ImmutableArray)}[2] = {typeImmutable2.ImmutableArray[2].Value}");
Console.WriteLine($"{nameof(typeImmutable2.ImmutableList)}.Count = {typeImmutable2.ImmutableList.Count}");
Console.WriteLine($"{nameof(typeImmutable2.ImmutableList)}[0] = {typeImmutable2.ImmutableList[0].Value}");
Console.WriteLine($"{nameof(typeImmutable2.ImmutableList)}[1] = {typeImmutable2.ImmutableList[1].Value}");
Console.WriteLine($"{nameof(typeImmutable2.ImmutableList)}[2] = {typeImmutable2.ImmutableList[2].Value}");
Console.WriteLine($"{nameof(typeImmutable2.IImmutableList)}.Count = {typeImmutable2.IImmutableList.Count}");
Console.WriteLine($"{nameof(typeImmutable2.IImmutableList)}[0] = {typeImmutable2.IImmutableList[0].Value}");
Console.WriteLine($"{nameof(typeImmutable2.IImmutableList)}[1] = {typeImmutable2.IImmutableList[1].Value}");
Console.WriteLine($"{nameof(typeImmutable2.IImmutableList)}[2] = {typeImmutable2.IImmutableList[2].Value}");
Console.WriteLine("END!");

[TomlSerializedObject]
Expand Down Expand Up @@ -165,10 +193,10 @@ public partial class TestClass
public KeyValuePair<int, StringBuilder> Pair { get; set; }

[TomlValueOnSerialized]
public ValueTuple<int, int> TwoValueTuple { get; set; } = default!;
public ValueTuple<int, int>? TwoValueTuple { get; set; } = default!;

[TomlValueOnSerialized]
public List<TestStruct?> TestStructArr { get; set; }
public IImmutableSet<TestStruct?> TestStructArr { get; set; }

[TomlValueOnSerialized]
public Custom TestStruct2 { get; set; }
Expand Down Expand Up @@ -243,5 +271,4 @@ public void Serialize<TBufferWriter>(ref Utf8TomlDocumentWriter<TBufferWriter> w
{
writer.WriteInt64(target.Value);
}
}

}
32 changes: 32 additions & 0 deletions sandbox/ConsoleNativeAOT/Type.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

using CsToml;
using System.Buffers;
using System.Collections.Immutable;

namespace ConsoleNativeAOT;

Expand All @@ -22,6 +23,7 @@ public partial class Sample
{
[TomlValueOnSerialized]
public Custom Key { get; set; }

}


Expand All @@ -45,3 +47,33 @@ public Custom(string str)
}
}
}

[TomlSerializedObject]
public partial class TestData
{
[TomlValueOnSerialized] public string E { get; set; }
[TomlValueOnSerialized] public int EE { get; set; }
[TomlValueOnSerialized] public long EEE { get; set; }
[TomlValueOnSerialized] public float EEEE { get; set; }
[TomlValueOnSerialized] public double EEEEE { get; set; }
}

[TomlSerializedObject]
internal partial class TypeImmutable
{
[TomlValueOnSerialized]
public ImmutableArray<TypeTable> ImmutableArray { get; set; }

[TomlValueOnSerialized]
public ImmutableList<TypeTable> ImmutableList { get; set; }

[TomlValueOnSerialized]
public IImmutableList<TypeTable> IImmutableList { get; set; }
}

[TomlSerializedObject]
internal partial class TypeTable
{
[TomlValueOnSerialized]
public string Value { get; set; }
}
112 changes: 0 additions & 112 deletions src/CsToml.Generator/CollectionMetaData.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/CsToml.Generator/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.CodeAnalysis;
#pragma warning disable RS2008

using Microsoft.CodeAnalysis;

namespace CsToml.Generator;

Expand Down
Loading
Loading