2021-12-24 20:06:06 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Immutable;
|
|
|
|
|
using MapTo.Extensions;
|
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
|
|
|
|
|
|
|
|
namespace MapTo
|
|
|
|
|
{
|
|
|
|
|
internal enum AccessModifier
|
|
|
|
|
{
|
|
|
|
|
Public,
|
|
|
|
|
Internal,
|
|
|
|
|
Private
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal enum NullStaticAnalysisState
|
|
|
|
|
{
|
|
|
|
|
Default,
|
|
|
|
|
Enabled,
|
|
|
|
|
Disabled
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal record SourceCode(string Text, string HintName);
|
|
|
|
|
|
|
|
|
|
internal record MappedMember(
|
|
|
|
|
string Name,
|
|
|
|
|
string FullyQualifiedType,
|
|
|
|
|
string Type,
|
|
|
|
|
string? TypeConverter,
|
|
|
|
|
ImmutableArray<string> TypeConverterParameters,
|
|
|
|
|
string SourcePropertyName,
|
|
|
|
|
string? MappedSourcePropertyTypeName,
|
|
|
|
|
string? EnumerableTypeArgument,
|
|
|
|
|
ISymbol ActualSymbol,
|
|
|
|
|
INamedTypeSymbol? NamedTypeSymbol,
|
|
|
|
|
bool isEnumerable,
|
|
|
|
|
bool isReadOnly)
|
|
|
|
|
{
|
|
|
|
|
public bool IsEnumerable => EnumerableTypeArgument is not null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 18:48:44 +03:00
|
|
|
|
internal record MappedSourceType
|
|
|
|
|
(
|
2021-12-24 20:06:06 +03:00
|
|
|
|
string SourceNamespace,
|
|
|
|
|
string SourceTypeIdentifierName,
|
|
|
|
|
string SourceTypeFullName,
|
|
|
|
|
ImmutableArray<MappedMember> SourceProperties,
|
|
|
|
|
ImmutableArray<MappedMember> SourceFields,
|
2022-08-18 18:48:44 +03:00
|
|
|
|
ImmutableArray<MappedMember> TypeProperties,
|
2021-12-24 20:06:06 +03:00
|
|
|
|
ImmutableArray<MappedMember> TypeFields,
|
|
|
|
|
bool GenerateSecondaryConstructor
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
public string SourceType => SourceTypeFullName;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 18:48:44 +03:00
|
|
|
|
|
|
|
|
|
internal record MappingModel(
|
|
|
|
|
SourceGenerationOptions Options,
|
|
|
|
|
string? Namespace,
|
|
|
|
|
SyntaxTokenList Modifiers,
|
|
|
|
|
string Type,
|
|
|
|
|
string TypeIdentifierName,
|
|
|
|
|
bool IsTypeUpdatable,
|
|
|
|
|
bool IsJsonExtension,
|
|
|
|
|
ImmutableArray<MappedSourceType> MappedSourceTypes,
|
|
|
|
|
bool HasMappedBaseClass,
|
|
|
|
|
ImmutableArray<string> Usings
|
|
|
|
|
);
|
|
|
|
|
|
2021-12-24 20:06:06 +03:00
|
|
|
|
internal record SourceGenerationOptions(
|
|
|
|
|
AccessModifier ConstructorAccessModifier,
|
|
|
|
|
AccessModifier GeneratedMethodsAccessModifier,
|
|
|
|
|
bool GenerateXmlDocument,
|
|
|
|
|
bool SupportNullableReferenceTypes,
|
|
|
|
|
bool SupportNullableStaticAnalysis)
|
|
|
|
|
{
|
|
|
|
|
internal static SourceGenerationOptions From(GeneratorExecutionContext context)
|
|
|
|
|
{
|
|
|
|
|
const string allowNullAttributeName = "System.Diagnostics.CodeAnalysis.AllowNullAttribute";
|
|
|
|
|
var supportNullableStaticAnalysis = context.GetBuildGlobalOption(propertyName: nameof(SupportNullableStaticAnalysis), NullStaticAnalysisState.Default);
|
|
|
|
|
var supportNullableReferenceTypes = context.Compilation.Options.NullableContextOptions is NullableContextOptions.Warnings or NullableContextOptions.Enable;
|
|
|
|
|
|
|
|
|
|
return new(
|
|
|
|
|
ConstructorAccessModifier: context.GetBuildGlobalOption(propertyName: nameof(ConstructorAccessModifier), AccessModifier.Public),
|
|
|
|
|
GeneratedMethodsAccessModifier: context.GetBuildGlobalOption(propertyName: nameof(GeneratedMethodsAccessModifier), AccessModifier.Public),
|
|
|
|
|
GenerateXmlDocument: context.GetBuildGlobalOption(propertyName: nameof(GenerateXmlDocument), true),
|
|
|
|
|
SupportNullableReferenceTypes: supportNullableReferenceTypes,
|
|
|
|
|
SupportNullableStaticAnalysis: supportNullableStaticAnalysis switch
|
|
|
|
|
{
|
|
|
|
|
NullStaticAnalysisState.Enabled => true,
|
|
|
|
|
NullStaticAnalysisState.Disabled => false,
|
|
|
|
|
_ => context.Compilation is CSharpCompilation { LanguageVersion: >= LanguageVersion.CSharp8 } cs && cs.TypeByMetadataNameExists(allowNullAttributeName)
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string NullableReferenceSyntax => SupportNullableReferenceTypes ? "?" : string.Empty;
|
|
|
|
|
}
|
2021-01-21 11:19:17 +03:00
|
|
|
|
}
|