Skip to content

Commit 25ad767

Browse files
committed
Toon Generator done
1 parent 4a97e2f commit 25ad767

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

sandbox/ConsoleApp1/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@
4343
[Cysharp.AI.GenerateToonTabularArrayConverter]
4444
public record Person(int Id, string Name, int Age);
4545

46+
public class Dummy
47+
{
48+
}
49+
4650

src/ToonEncoder.Generator/ToonEncoderGenerator.cs

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.Text;
23

34
namespace Cysharp.AI;
45

@@ -44,13 +45,10 @@ internal sealed class GenerateToonTabularArrayConverter : Attribute
4445

4546
static void EmitTabularArrayConverter(SourceProductionContext sourceProductionContext, TabularArrayInfo tabularArrayInfo)
4647
{
47-
//sourceProductionContext.ReportDiagnostic(Diagnostic.Create(new DiagnosticDescriptor(
48-
// "TEG001",
49-
// "Generating Toon Tabular Array Converter",
50-
// $"Generating Toon Tabular Array Converter for {tabularArrayInfo.ElementFullName}",
51-
// "ToonEncoderGenerator",
52-
// DiagnosticSeverity.Info,
53-
// isEnabledByDefault: true), Location.None));
48+
if (!tabularArrayInfo.Verify(sourceProductionContext))
49+
{
50+
return;
51+
}
5452

5553
var arrayType = $"{tabularArrayInfo.ElementFullName}[]";
5654
var utf8FieldNames = string.Join(", ", tabularArrayInfo.PropertyNames.Select(n => $"\"{n}\"u8.ToArray()"));
@@ -176,10 +174,20 @@ public override void Write(Utf8JsonWriter utf8JsonWriter, {{arrayType}} value, J
176174
}
177175
}
178176

177+
public record struct LocationSlim(string FilePath, TextSpan TextSpan, LinePositionSpan LinePositionSpan)
178+
{
179+
public Location CreateLocation()
180+
{
181+
if (FilePath == null) return Location.None;
182+
return Location.Create(FilePath, TextSpan, LinePositionSpan);
183+
}
184+
}
185+
179186
public record TabularArrayInfo
180187
{
181188
public string ElementFullName { get; }
182189
public string ConverterName { get; }
190+
public LocationSlim Location { get; }
183191
public string[] PropertyNames { get; }
184192
public ToonPrimitiveKind[] PropertyKinds { get; }
185193

@@ -188,6 +196,16 @@ public TabularArrayInfo(ITypeSymbol symbol)
188196
ElementFullName = symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
189197
ConverterName = $"{ElementFullName.Replace("global::", "").Replace(".", "_")}TabularArrayConverter";
190198

199+
var location = symbol.Locations.FirstOrDefault();
200+
if (location != null)
201+
{
202+
var lineSpan = location.GetLineSpan();
203+
Location = new LocationSlim(
204+
location.SourceTree?.FilePath ?? "",
205+
location.SourceSpan,
206+
lineSpan.Span);
207+
}
208+
191209
var nameAndKinds = symbol.GetMembers()
192210
.Where(x => x.DeclaredAccessibility == Accessibility.Public)
193211
.OfType<IPropertySymbol>()
@@ -272,6 +290,39 @@ static bool IsGuid(ITypeSymbol type)
272290
PropertyNames = nameAndKinds.Select(x => x.Name).ToArray();
273291
PropertyKinds = nameAndKinds.Select(x => x.Kind).ToArray();
274292
}
293+
294+
public bool Verify(SourceProductionContext sourceProductionContext)
295+
{
296+
var hasUnsupported = false;
297+
List<string>? unsupportedPropertyNames = null;
298+
for (int i = 0; i < PropertyKinds.Length; i++)
299+
{
300+
if (PropertyKinds[i] == ToonPrimitiveKind.Unsupported)
301+
{
302+
if (unsupportedPropertyNames == null)
303+
{
304+
unsupportedPropertyNames = new List<string>();
305+
}
306+
unsupportedPropertyNames.Add(PropertyNames[i]);
307+
hasUnsupported = true;
308+
}
309+
}
310+
311+
if (hasUnsupported)
312+
{
313+
sourceProductionContext.ReportDiagnostic(Diagnostic.Create(new DiagnosticDescriptor(
314+
"TEG001",
315+
"Unsupported Property Type for Toon Tabular Array Converter",
316+
$"The property type is not supported for Toon Tabular Array serialization in {ElementFullName.Replace("global::", "")}.{string.Join(", ", unsupportedPropertyNames)}.",
317+
"ToonEncoderGenerator",
318+
DiagnosticSeverity.Error,
319+
isEnabledByDefault: true), Location.CreateLocation()));
320+
321+
return false;
322+
}
323+
324+
return true;
325+
}
275326
}
276327

277328
public enum ToonPrimitiveKind

0 commit comments

Comments
 (0)