Skip to content
Draft

Aot gen #1965

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
220 changes: 220 additions & 0 deletions InterfaceStubGenerator.Shared/AttributeDataAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
// using System.Reflection;
//
// using Microsoft.CodeAnalysis;
// using Microsoft.CodeAnalysis.CSharp.Syntax;
// using Microsoft.CodeAnalysis.Operations;
//
// namespace Refit.Generator
// {
// public class AttributeDataAccessor(WellKnownTypes symbolAccessor)
// {
// private const string NameOfOperatorName = "nameof";
// private const char FullNameOfPrefix = '@';
//
// public TAttribute AccessSingle<TAttribute>(ISymbol symbol)
// where TAttribute : Attribute => AccessSingle<TAttribute, TAttribute>(symbol);
//
// public TData AccessSingle<TAttribute, TData>(ISymbol symbol)
// where TAttribute : Attribute
// where TData : notnull => Access<TAttribute, TData>(symbol).Single();
//
// public TAttribute? AccessFirstOrDefault<TAttribute>(ISymbol symbol)
// where TAttribute : Attribute => Access<TAttribute, TAttribute>(symbol).FirstOrDefault();
//
// public TData? AccessFirstOrDefault<TAttribute, TData>(ISymbol symbol)
// where TAttribute : Attribute
// where TData : notnull => Access<TAttribute, TData>(symbol).FirstOrDefault();
//
// // public bool HasAttribute<TAttribute>(ISymbol symbol)
// // where TAttribute : Attribute => symbolAccessor.HasAttribute<TAttribute>(symbol);
//
// public IEnumerable<TAttribute> Access<TAttribute>(ISymbol symbol)
// where TAttribute : Attribute => Access<TAttribute, TAttribute>(symbol);
//
// // public IEnumerable<TAttribute> TryAccess<TAttribute>(IEnumerable<AttributeData> data)
// // where TAttribute : Attribute => TryAccess<TAttribute, TAttribute>(data);
//
// public IEnumerable<TData> Access<TAttribute, TData>(ISymbol symbol)
// where TAttribute : Attribute
// where TData : notnull
// {
// var attrDatas = symbolAccessor.GetAttributes<TAttribute>(symbol);
// return Access<TAttribute, TData>(attrDatas);
// }
//
// // public IEnumerable<TData> TryAccess<TAttribute, TData>(IEnumerable<AttributeData> attributes)
// // where TAttribute : Attribute
// // where TData : notnull
// // {
// // var attrDatas = symbolAccessor.TryGetAttributes<TAttribute>(attributes);
// // return attrDatas.Select(a => Access<TAttribute, TData>(a));
// // }
//
// /// <summary>
// /// Reads the attribute data and sets it on a newly created instance of <see cref="TData"/>.
// /// If <see cref="TAttribute"/> has n type parameters,
// /// <see cref="TData"/> needs to have an accessible ctor with the parameters 0 to n-1 to be of type <see cref="ITypeSymbol"/>.
// /// <see cref="TData"/> needs to have exactly the same constructors as <see cref="TAttribute"/> with additional type arguments.
// /// </summary>
// /// <param name="attributes">The attributes data.</param>
// /// <typeparam name="TAttribute">The type of the attribute.</typeparam>
// /// <typeparam name="TData">The type of the data class. If no type parameters are involved, this is usually the same as <see cref="TAttribute"/>.</typeparam>
// /// <returns>The attribute data.</returns>
// /// <exception cref="InvalidOperationException">If a property or ctor argument of <see cref="TData"/> could not be read on the attribute.</exception>
// public IEnumerable<TData> Access<TAttribute, TData>(IEnumerable<AttributeData> attributes)
// where TAttribute : Attribute
// where TData : notnull
// {
// foreach (var attrData in symbolAccessor.GetAttributes<TAttribute>(attributes))
// {
// yield return Access<TAttribute, TData>(attrData, symbolAccessor);
// }
// }
//
// internal static TData Access<TAttribute, TData>(AttributeData attrData, WellKnownTypes? symbolAccessor = null)
// where TAttribute : Attribute
// where TData : notnull
// {
// var attrType = typeof(TAttribute);
// var dataType = typeof(TData);
//
// var syntax = (AttributeSyntax?)attrData.ApplicationSyntaxReference?.GetSyntax();
// var syntaxArguments =
// (IReadOnlyList<AttributeArgumentSyntax>?)syntax?.ArgumentList?.Arguments
// ?? new AttributeArgumentSyntax[attrData.ConstructorArguments.Length + attrData.NamedArguments.Length];
// var typeArguments = (IReadOnlyCollection<ITypeSymbol>?)attrData.AttributeClass?.TypeArguments ?? [];
// var attr = Create<TData>(typeArguments, attrData.ConstructorArguments, syntaxArguments, symbolAccessor);
//
// var syntaxIndex = attrData.ConstructorArguments.Length;
// var propertiesByName =
// dataType.GetProperties().GroupBy(x => x.Name).ToDictionary(x => x.Key, x => x.First());
// foreach (var namedArgument in attrData.NamedArguments)
// {
// if (!propertiesByName.TryGetValue(namedArgument.Key, out var prop))
// throw new InvalidOperationException(
// $"Could not get property {namedArgument.Key} of attribute {attrType.FullName}");
//
// var value = BuildArgumentValue(namedArgument.Value, prop.PropertyType, syntaxArguments[syntaxIndex],
// symbolAccessor);
// prop.SetValue(attr, value);
// syntaxIndex++;
// }
//
// // if (attr is HasSyntaxReference symbolRefHolder)
// // {
// // symbolRefHolder.SyntaxReference = attrData.ApplicationSyntaxReference?.GetSyntax();
// // }
//
// return attr;
// }
//
// private static TData Create<TData>(
// IReadOnlyCollection<ITypeSymbol> typeArguments,
// IReadOnlyCollection<TypedConstant> constructorArguments,
// IReadOnlyList<AttributeArgumentSyntax> argumentSyntax,
// WellKnownTypes? symbolAccessor
// )
// where TData : notnull
// {
// // The data class should have a constructor
// // with generic type parameters of the attribute class
// // as ITypeSymbol parameters followed by all other parameters
// // of the attribute constructor.
// // Multiple attribute class constructors/generic data classes are not yet supported.
// var argCount = typeArguments.Count + constructorArguments.Count;
// foreach (var constructor in typeof(TData).GetConstructors())
// {
// var parameters = constructor.GetParameters();
// if (parameters.Length != argCount)
// continue;
//
// var constructorArgumentValues = constructorArguments.Select(
// (arg, i) => BuildArgumentValue(arg, parameters[i + typeArguments.Count].ParameterType,
// argumentSyntax[i], symbolAccessor)
// );
// var constructorTypeAndValueArguments = typeArguments.Concat(constructorArgumentValues).ToArray();
// if (!ValidateParameterTypes(constructorTypeAndValueArguments, parameters))
// continue;
//
// return (TData?)Activator.CreateInstance(typeof(TData), constructorTypeAndValueArguments)
// ?? throw new InvalidOperationException($"Could not create instance of {typeof(TData)}");
// }
//
// throw new InvalidOperationException(
// $"{typeof(TData)} does not have a constructor with {argCount} parameters and matchable arguments"
// );
// }
//
// private static object? BuildArgumentValue(
// TypedConstant arg,
// Type targetType,
// AttributeArgumentSyntax? syntax,
// WellKnownTypes? symbolAccessor
// )
// {
// return arg.Kind switch
// {
// // _ when (targetType == typeof(AttributeValue?) || targetType == typeof(AttributeValue)) && syntax != null => new AttributeValue(
// // arg,
// // syntax.Expression
// // ),
// _ when arg.IsNull => null,
// // _ when targetType == typeof(IMemberPathConfiguration) => CreateMemberPath(arg, syntax, symbolAccessor),
// TypedConstantKind.Enum => GetEnumValue(arg, targetType),
// TypedConstantKind.Array => BuildArrayValue(arg, targetType, symbolAccessor),
// TypedConstantKind.Primitive => arg.Value,
// TypedConstantKind.Type when targetType == typeof(ITypeSymbol) => arg.Value,
// _ => throw new ArgumentOutOfRangeException(
// $"{nameof(AttributeDataAccessor)} does not support constructor arguments of kind {arg.Kind.ToString()} or cannot convert it to {targetType}"
// ),
// };
// }
//
// private static object?[] BuildArrayValue(TypedConstant arg, Type targetType, WellKnownTypes? symbolAccessor)
// {
// if (!targetType.IsGenericType || targetType.GetGenericTypeDefinition() != typeof(IReadOnlyCollection<>))
// throw new InvalidOperationException(
// $"{nameof(IReadOnlyCollection<object>)} is the only supported array type");
//
// var elementTargetType = targetType.GetGenericArguments()[0];
// return arg.Values.Select(x => BuildArgumentValue(x, elementTargetType, null, symbolAccessor)).ToArray();
// }
//
// private static object? GetEnumValue(TypedConstant arg, Type targetType)
// {
// if (arg.Value == null)
// return null;
//
// var enumRoslynType = arg.Type ?? throw new InvalidOperationException("Type is null");
// if (targetType == typeof(IFieldSymbol))
// return enumRoslynType.GetMembers().Where(x => x.Kind == SymbolKind.Field)
// .First(f => Equals(f.ConstantValue, arg.Value));
//
// if (targetType.IsConstructedGenericType && targetType.GetGenericTypeDefinition() == typeof(Nullable<>))
// {
// targetType = Nullable.GetUnderlyingType(targetType)!;
// }
//
// return Enum.ToObject(targetType, arg.Value);
// }
//
// private static bool ValidateParameterTypes(object?[] arguments, ParameterInfo[] parameters)
// {
// if (arguments.Length != parameters.Length)
// return false;
//
// for (var argIdx = 0; argIdx < arguments.Length; argIdx++)
// {
// var value = arguments[argIdx];
// var param = parameters[argIdx];
// if (value == null && param.ParameterType.IsValueType)
// return false;
//
// // if (value?.GetType().IsAssignableTo(param.ParameterType) == false)
// // return false;
// }
//
// return true;
// }
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Refit.Generator.Configuration;

// TODO: I hate how I have to duplicate the attributes in this file.
// See if I can remove this. Iirc Mapperly didn't need to do this initially. Might not need all this
// Arguably cleaner doing the current system tho
public class AliasAsConfiguration(string name)
{
public string Name { get; protected set; } = name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Refit.Generator.Configuration;

public class AttachmentNameConfiguration(string name)
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; protected set; } = name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Refit.Generator.Configuration;

public class AuthorizeConfiguration(string scheme = "Bearer") : Attribute
{
/// <summary>
/// Gets the scheme.
/// </summary>
/// <value>
/// The scheme.
/// </value>
public string Scheme { get; } = scheme;
}
53 changes: 53 additions & 0 deletions InterfaceStubGenerator.Shared/Configuration/BodyConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
namespace Refit.Generator.Configuration;

public class BodyConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="BodyAttribute"/> class.
/// </summary>
public BodyConfiguration() { }

/// <summary>
/// Initializes a new instance of the <see cref="BodyAttribute"/> class.
/// </summary>
/// <param name="buffered">if set to <c>true</c> [buffered].</param>
public BodyConfiguration(bool buffered) => Buffered = buffered;

/// <summary>
/// Initializes a new instance of the <see cref="BodyAttribute"/> class.
/// </summary>
/// <param name="serializationMethod">The serialization method.</param>
/// <param name="buffered">if set to <c>true</c> [buffered].</param>
public BodyConfiguration(BodySerializationMethod serializationMethod, bool buffered)
{
SerializationMethod = serializationMethod;
Buffered = buffered;
}

/// <summary>
/// Initializes a new instance of the <see cref="BodyAttribute"/> class.
/// </summary>
/// <param name="serializationMethod">The serialization method.</param>
public BodyConfiguration(
BodySerializationMethod serializationMethod = BodySerializationMethod.Default
)
{
SerializationMethod = serializationMethod;
}

/// <summary>
/// Gets or sets the buffered.
/// </summary>
/// <value>
/// The buffered.
/// </value>
public bool? Buffered { get; }

/// <summary>
/// Gets or sets the serialization method.
/// </summary>
/// <value>
/// The serialization method.
/// </value>
public BodySerializationMethod SerializationMethod { get; } = BodySerializationMethod.Default;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#nullable enable
namespace Refit.Generator.Configuration;

/// <summary>
/// Defines methods to serialize HTTP requests' bodies.
/// </summary>
public enum BodySerializationMethod
{
/// <summary>
/// Encodes everything using the ContentSerializer in RefitSettings except for strings. Strings are set as-is
/// </summary>
Default = 0,

/// <summary>
/// Json encodes everything, including strings
/// </summary>
[Obsolete("Use BodySerializationMethod.Serialized instead", false)]
Json = 1,

/// <summary>
/// Form-UrlEncode's the values
/// </summary>
UrlEncoded = 2,

/// <summary>
/// Encodes everything using the ContentSerializer in RefitSettings
/// </summary>
Serialized = 3,
}
39 changes: 39 additions & 0 deletions InterfaceStubGenerator.Shared/Configuration/CollectionFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#nullable enable
namespace Refit.Generator.Configuration;

/// <summary>
/// Collection format defined in https://swagger.io/docs/specification/2-0/describing-parameters/
/// </summary>
public enum CollectionFormat
{
/// <summary>
/// Values formatted with <see cref="RefitSettings.UrlParameterFormatter"/> or
/// <see cref="RefitSettings.FormUrlEncodedParameterFormatter"/>.
/// </summary>
RefitParameterFormatter,

/// <summary>
/// Comma-separated values
/// </summary>
Csv,

/// <summary>
/// Space-separated values
/// </summary>
Ssv,

/// <summary>
/// Tab-separated values
/// </summary>
Tsv,

/// <summary>
/// Pipe-separated values
/// </summary>
Pipes,

/// <summary>
/// Multiple parameter instances
/// </summary>
Multi,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Refit.Generator.Configuration;

public class HeaderCollectionConfiguration { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Refit.Generator.Configuration;

public class HeaderConfiguration(string header) : Attribute
{
public string Header { get; } = header;
}
Loading