|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | + |
| 3 | +namespace JsonApiDotNetCore.SourceGenerators; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Basic outcome from the code analysis. |
| 7 | +/// </summary> |
| 8 | +internal readonly record struct CoreControllerInfo( |
| 9 | + TypeInfo ResourceType, TypeInfo IdType, string ControllerNamespace, JsonApiEndpointsCopy Endpoints, bool WriteNullableEnable) |
| 10 | +{ |
| 11 | + // Using readonly fields, so they can be passed by reference (using 'in' modifier, to avoid making copies) during code generation. |
| 12 | + public readonly TypeInfo ResourceType = ResourceType; |
| 13 | + public readonly TypeInfo IdType = IdType; |
| 14 | + public readonly string ControllerNamespace = ControllerNamespace; |
| 15 | + public readonly JsonApiEndpointsCopy Endpoints = Endpoints; |
| 16 | + public readonly bool WriteNullableEnable = WriteNullableEnable; |
| 17 | + |
| 18 | + public static CoreControllerInfo? TryCreate(INamedTypeSymbol resourceTypeSymbol, ITypeSymbol idTypeSymbol, JsonApiEndpointsCopy endpoints, |
| 19 | + string controllerNamespace) |
| 20 | + { |
| 21 | + TypeInfo? resourceTypeInfo = TypeInfo.TryCreateFrom(resourceTypeSymbol); |
| 22 | + |
| 23 | + if (resourceTypeInfo == null) |
| 24 | + { |
| 25 | + return null; |
| 26 | + } |
| 27 | + |
| 28 | + TypeInfo? idTypeInfo = TypeInfo.TryCreateFrom(idTypeSymbol); |
| 29 | + |
| 30 | + if (idTypeInfo == null) |
| 31 | + { |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + bool writeNullableEnable = idTypeSymbol is { IsReferenceType: true, NullableAnnotation: NullableAnnotation.Annotated }; |
| 36 | + |
| 37 | + return new CoreControllerInfo(resourceTypeInfo.Value, idTypeInfo.Value, controllerNamespace, endpoints, writeNullableEnable); |
| 38 | + } |
| 39 | +} |
0 commit comments