Skip to content

Commit

Permalink
Rename TypeInfo to SerdeInfo (#180)
Browse files Browse the repository at this point in the history
This avoids confusion with the ITypeInfo interface in the System.Reflection namespace.
  • Loading branch information
agocke authored Jul 14, 2024
1 parent 94efd9d commit cdff121
Show file tree
Hide file tree
Showing 279 changed files with 1,390 additions and 1,317 deletions.
4 changes: 2 additions & 2 deletions perf/bench/SampleTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public partial record Location

public partial record LocationWrap : IDeserialize<Location>
{
private static readonly TypeInfo s_fieldMap = TypeInfo.Create(
private static readonly SerdeInfo s_fieldMap = SerdeInfo.Create(
"Location",
TypeInfo.TypeKind.CustomType, [
SerdeInfo.TypeKind.CustomType, [
("id", typeof(Location).GetProperty("Id")!),
("address1", typeof(Location).GetProperty("Address1")!),
("address2", typeof(Location).GetProperty("Address2")!),
Expand Down
22 changes: 11 additions & 11 deletions src/generator/Generator.Deserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ internal static (MemberDeclarationSyntax[], BaseListSyntax) GenerateDeserializeI
/// <code>
/// static T IDeserialize&lt;T&gt;Deserialize(IDeserializer deserializer)
/// {
/// var typeInfo = TSerdeTypeInfo.TypeInfo;
/// var de = deserializer.DeserializeType(typeInfo);
/// var serdeInfo = TSerdeInfo.Instance;
/// var de = deserializer.DeserializeType(serdeInfo);
/// int index;
/// if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound)
/// if ((index = de.TryReadIndex(serdeInfo, out var errorName)) == IDeserializeType.IndexNotFound)
/// {
/// throw new InvalidDeserializeValueException($"Unexpected value: {errorName}");
/// }
Expand Down Expand Up @@ -85,10 +85,10 @@ private static MethodDeclarationSyntax GenerateEnumDeserializeMethod(
var src = $$"""
static {{typeFqn}} IDeserialize<{{typeFqn}}>.Deserialize(IDeserializer deserializer)
{
var typeInfo = {{typeFqn}}SerdeTypeInfo.TypeInfo;
var de = deserializer.DeserializeType(typeInfo);
var serdeInfo = {{typeFqn}}SerdeInfo.Instance;
var de = deserializer.DeserializeType(serdeInfo);
int index;
if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound)
if ((index = de.TryReadIndex(serdeInfo, out var errorName)) == IDeserializeType.IndexNotFound)
{
throw new InvalidDeserializeValueException($"Unexpected value: {errorName}");
}
Expand All @@ -111,10 +111,10 @@ private static MethodDeclarationSyntax GenerateEnumDeserializeMethod(
/// ...
/// var _localN = default!;
///
/// var typeInfo = {typeName}SerdeTypeInfo.TypeInfo;
/// var typDeserializer = deserializer.DeserializeType(typeInfo);
/// var serdeInfo = {typeName}SerdeInfo.Instance;
/// var typDeserializer = deserializer.DeserializeType(serdeInfo);
/// int index;
/// while ((index = typeDeserialize.TryReadIndex(typeInfo)) != IDeserializeType.EndOfType)
/// while ((index = typeDeserialize.TryReadIndex(serdeInfo)) != IDeserializeType.EndOfType)
/// {
/// switch (index)
/// {
Expand Down Expand Up @@ -143,7 +143,7 @@ private static MethodDeclarationSyntax GenerateCustomDeserializeMethod(
var (cases, locals, requiredMask) = InitCasesAndLocals();
string typeCreationExpr = GenerateTypeCreation(context, typeFqn, type, members, requiredMask);

const string typeInfoLocalName = "_l_typeInfo";
const string typeInfoLocalName = "_l_serdeInfo";
const string indexLocalName = "_l_index_";

var methodText = $$"""
Expand All @@ -152,7 +152,7 @@ private static MethodDeclarationSyntax GenerateCustomDeserializeMethod(
{{locals}}
{{assignedVarType}} {{AssignedVarName}} = 0;

var {{typeInfoLocalName}} = {{type.Name}}SerdeTypeInfo.TypeInfo;
var {{typeInfoLocalName}} = {{type.Name}}SerdeInfo.Instance;
var typeDeserialize = deserializer.DeserializeType({{typeInfoLocalName}});
int {{indexLocalName}};
while (({{indexLocalName}} = typeDeserialize.TryReadIndex({{typeInfoLocalName}}, out var _l_errorName)) != IDeserializeType.EndOfType)
Expand Down
22 changes: 11 additions & 11 deletions src/generator/Generator.Serialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal static (MemberDeclarationSyntax[], BaseListSyntax) GenSerialize(
var enumType = (INamedTypeSymbol)receiverType;
var typeSyntax = enumType.ToFqnSyntax();
var underlying = enumType.EnumUnderlyingType!;
statements.Add(ParseStatement($"var _l_typeInfo = {receiverType.ToFqnSyntax()}SerdeTypeInfo.TypeInfo;"));
statements.Add(ParseStatement($"var _l_serdeInfo = {receiverType.ToFqnSyntax()}SerdeInfo.Instance;"));
statements.Add(ParseStatement($$"""
var index = value switch
{
Expand All @@ -51,21 +51,21 @@ internal static (MemberDeclarationSyntax[], BaseListSyntax) GenSerialize(
"""));
var wrapper = Wrappers.TryGetPrimitiveWrapper(underlying, SerdeUsage.Serialize).NotNull().Wrapper;
statements.Add(ParseStatement(
$"serializer.SerializeEnumValue(_l_typeInfo, index, ({underlying.ToFqnSyntax()})value, default({wrapper.ToFullString()}));"));
$"serializer.SerializeEnumValue(_l_serdeInfo, index, ({underlying.ToFqnSyntax()})value, default({wrapper.ToFullString()}));"));
}
else
{
// The generated body of ISerialize is
// `var _l_typeInfo = {TypeName}SerdeTypeInfo.TypeInfo;`
// `var type = serializer.SerializeType(_l_typeInfo);
// type.SerializeField<FieldType, Serialize>(_l_typeInfo, FieldIndex, receiver.FieldValue);
// `var _l_serdeInfo = {TypeName}SerdeTypeInfo.TypeInfo;`
// `var type = serializer.SerializeType(_l_serdeInfo);
// type.SerializeField<FieldType, Serialize>(_l_serdeInfo, FieldIndex, receiver.FieldValue);
// type.End();

// `var _l_typeInfo = {TypeName}SerdeTypeInfo.TypeInfo;`
statements.Add(ParseStatement($"var _l_typeInfo = {receiverType.Name}SerdeTypeInfo.TypeInfo;"));
// `var _l_serdeInfo = {TypeName}SerdeTypeInfo.TypeInfo;`
statements.Add(ParseStatement($"var _l_serdeInfo = {receiverType.Name}SerdeInfo.Instance;"));

// `var type = serializer.SerializeType(_l_typeInfo);`
statements.Add(ParseStatement("var type = serializer.SerializeType(_l_typeInfo);"));
// `var type = serializer.SerializeType(_l_serdeInfo);`
statements.Add(ParseStatement("var type = serializer.SerializeType(_l_serdeInfo);"));

for (int i = 0; i < fieldsAndProps.Count; i++)
{
Expand Down Expand Up @@ -132,8 +132,8 @@ static ExpressionStatementSyntax MakeSerializeFieldStmt(
ExpressionSyntax receiver)
{
var arguments = new List<ExpressionSyntax>() {
// _l_typeInfo
ParseExpression("_l_typeInfo"),
// _l_serdeInfo
ParseExpression("_l_serdeInfo"),
// Index
LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(index)),
// Value
Expand Down
2 changes: 1 addition & 1 deletion src/generator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static void RunGeneration(
generationContext);
}

SerdeTypeInfoGenerator.GenerateTypeInfo(
SerdeTypeInfoGenerator.GenerateSerdeInfo(
typeDecl,
receiverType,
generationContext);
Expand Down
12 changes: 6 additions & 6 deletions src/generator/SerdeTypeInfoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ internal static class SerdeTypeInfoGenerator
/// <code>
/// internal static class {typeName}SerdeTypeInfo
/// {
/// internal static readonly TypeInfo TypeInfo = TypeInfo.Create([
/// internal static readonly SerdeInfo TypeInfo = TypeInfo.Create([
/// ("{{fieldName}}", typeof({typeName}).GetField("{fieldName}")!),
/// ...
/// ]);
/// }
/// </code>
/// </summary>
public static void GenerateTypeInfo(
public static void GenerateSerdeInfo(
BaseTypeDeclarationSyntax typeDecl,
INamedTypeSymbol receiverType,
GeneratorExecutionContext context)
Expand All @@ -41,11 +41,11 @@ public static void GenerateTypeInfo(
typeString = typeString + "<" + new string(',', receiverType.TypeParameters.Length - 1) + ">";
}
var newType = $$"""
internal static class {{typeName}}SerdeTypeInfo
internal static class {{typeName}}SerdeInfo
{
internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create(
internal static readonly Serde.SerdeInfo Instance = Serde.SerdeInfo.Create(
"{{typeName}}",
Serde.TypeInfo.TypeKind.{{(receiverType.TypeKind == TypeKind.Enum ? "Enum" : "CustomType")}},
Serde.SerdeInfo.TypeKind.{{(receiverType.TypeKind == TypeKind.Enum ? "Enum" : "CustomType")}},
new (string, System.Reflection.MemberInfo)[] {
{{string.Join("," + Environment.NewLine,
fieldsAndProps.Select(x => $@"(""{x.GetFormattedName()}"", typeof({typeString}).Get{(x.Symbol.Kind == SymbolKind.Field ? "Field" : "Property")}(""{x.Name}"")!)"))}}
Expand All @@ -56,7 +56,7 @@ internal static class {{typeName}}SerdeTypeInfo
newType = typeDeclContext.WrapNewType(newType);
string fullTypeName = string.Join(".", typeDeclContext.NamespaceNames
.Concat(typeDeclContext.ParentTypeInfo.Select(x => x.Name))
.Concat(new[] { $"{typeName}SerdeTypeInfo" }));
.Concat(new[] { $"{typeName}SerdeInfo" }));

context.AddSource(fullTypeName, newType);
}
Expand Down
14 changes: 7 additions & 7 deletions src/serde-xml/XmlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void SerializeDouble(double d)
_writer.WriteValue(d);
}

void ISerializer.SerializeEnumValue<T, U>(TypeInfo typeInfo, int index, T value, U serialize)
void ISerializer.SerializeEnumValue<T, U>(SerdeInfo typeInfo, int index, T value, U serialize)
{
var name = typeInfo.GetStringSerializeName(index);
SerializeString(name);
Expand Down Expand Up @@ -156,13 +156,13 @@ private static string FormatTypeName(string name)
return formattingListener.ToString();
}

public ISerializeCollection SerializeCollection(TypeInfo typeInfo, int? length)
public ISerializeCollection SerializeCollection(SerdeInfo typeInfo, int? length)
{
if (typeInfo.Kind == TypeInfo.TypeKind.Dictionary)
if (typeInfo.Kind == SerdeInfo.TypeKind.Dictionary)
{
throw new NotSupportedException("Serde.XmlSerializer doesn't currently support serializing dictionaries");
}
else if (typeInfo.Kind != TypeInfo.TypeKind.Enumerable)
else if (typeInfo.Kind != SerdeInfo.TypeKind.Enumerable)
{
throw new ArgumentException("typeInfo must be a collection type", nameof(typeInfo));
}
Expand All @@ -188,7 +188,7 @@ public SerializeCollectionImpl(XmlSerializer serializer, State savedState)
void ISerializeCollection.SerializeElement<T, U>(T value, U serialize)
=> serialize.Serialize(value, _serializer);

void ISerializeCollection.End(TypeInfo typeInfo)
void ISerializeCollection.End(SerdeInfo typeInfo)
{
if (_savedState == State.Enumerable)
{
Expand All @@ -198,7 +198,7 @@ void ISerializeCollection.End(TypeInfo typeInfo)
}
}

public ISerializeType SerializeType(TypeInfo typeInfo)
public ISerializeType SerializeType(SerdeInfo typeInfo)
{
var saved = _state;
bool writeEnd;
Expand Down Expand Up @@ -228,7 +228,7 @@ public XmlTypeSerializer(bool writeEnd, XmlSerializer parent, State savedState)
_savedState = savedState;
}

public void SerializeField<T, U>(TypeInfo typeInfo, int fieldIndex, T value, U impl)
public void SerializeField<T, U>(SerdeInfo typeInfo, int fieldIndex, T value, U impl)
where U : ISerialize<T>
{
var name = typeInfo.GetStringSerializeName(fieldIndex);
Expand Down
8 changes: 4 additions & 4 deletions src/serde/IDeserialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public interface IDeserializeCollection
{
int? SizeOpt { get; }

bool TryReadValue<T, D>(TypeInfo typeInfo, [MaybeNullWhen(false)] out T next)
bool TryReadValue<T, D>(SerdeInfo typeInfo, [MaybeNullWhen(false)] out T next)
where D : IDeserialize<T>;
}

Expand Down Expand Up @@ -96,7 +96,7 @@ public interface IDeserializeType
/// cref="IndexNotFound" /> and set <paramref name="errorName" /> to the name of the missing
/// field, or the best-possible user-facing name.
/// </summary>
int TryReadIndex(TypeInfo map, out string? errorName);
int TryReadIndex(SerdeInfo map, out string? errorName);

V ReadValue<V, D>(int index) where D : IDeserialize<V>;
}
Expand All @@ -120,7 +120,7 @@ public interface IDeserializer
T DeserializeString<T>(IDeserializeVisitor<T> v);
T DeserializeIdentifier<T>(IDeserializeVisitor<T> v);
T DeserializeNullableRef<T>(IDeserializeVisitor<T> v);
IDeserializeCollection DeserializeCollection(TypeInfo typeInfo);
IDeserializeType DeserializeType(TypeInfo typeInfo);
IDeserializeCollection DeserializeCollection(SerdeInfo typeInfo);
IDeserializeType DeserializeType(SerdeInfo typeInfo);
}
}
18 changes: 9 additions & 9 deletions src/serde/ISerialize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ public interface ISerialize<T>

public interface ISerializeType
{
void SerializeField<T, U>(TypeInfo typeInfo, int index, T value, U serialize) where U : ISerialize<T>;
void SkipField(TypeInfo typeInfo, int index) { }
void SerializeField<T, U>(SerdeInfo typeInfo, int index, T value, U serialize) where U : ISerialize<T>;
void SkipField(SerdeInfo typeInfo, int index) { }
void End();
}

public static class ISerializeTypeExt
{
public static void SerializeField<T, U>(
this ISerializeType serializeType,
TypeInfo typeInfo,
SerdeInfo typeInfo,
int index,
T value) where U : struct, ISerialize<T>
{
Expand All @@ -28,7 +28,7 @@ public static void SerializeField<T, U>(

public static void SerializeFieldIfNotNull<T, U>(
this ISerializeType serializeType,
TypeInfo typeInfo,
SerdeInfo typeInfo,
int index,
T value,
U serialize) where U : ISerialize<T>
Expand All @@ -45,7 +45,7 @@ public static void SerializeFieldIfNotNull<T, U>(

public static void SerializeFieldIfNotNull<T, U>(
this ISerializeType serializeType,
TypeInfo typeInfo,
SerdeInfo typeInfo,
int index,
T value) where U : struct, ISerialize<T>
{
Expand All @@ -56,7 +56,7 @@ public static void SerializeFieldIfNotNull<T, U>(
public interface ISerializeCollection
{
void SerializeElement<T, U>(T value, U serialize) where U : ISerialize<T>;
void End(TypeInfo typeInfo);
void End(SerdeInfo typeInfo);
}

public interface ISerializer
Expand All @@ -76,10 +76,10 @@ public interface ISerializer
void SerializeDecimal(decimal d);
void SerializeString(string s);
void SerializeNull();
void SerializeEnumValue<T, U>(TypeInfo typeInfo, int index, T value, U serialize)
void SerializeEnumValue<T, U>(SerdeInfo typeInfo, int index, T value, U serialize)
where T : unmanaged
where U : ISerialize<T>;

ISerializeType SerializeType(TypeInfo typeInfo);
ISerializeCollection SerializeCollection(TypeInfo typeInfo, int? length);
ISerializeType SerializeType(SerdeInfo typeInfo);
ISerializeCollection SerializeCollection(SerdeInfo typeInfo, int? length);
}
Loading

0 comments on commit cdff121

Please sign in to comment.