internal static FieldDeclarationSyntax ToFieldDeclaration(this DataFromEntityFieldDescription dataFromEntityFieldDescription) { var dataFromEntityType = (dataFromEntityFieldDescription.AccessorDataType == PatchableMethod.AccessorDataType.ComponentDataFromEntity) ? "ComponentDataFromEntity" : "BufferFromEntity"; var accessAttribute = dataFromEntityFieldDescription.IsReadOnly ? "[Unity.Collections.ReadOnly]" : string.Empty; var template = $@"{accessAttribute} public {dataFromEntityType}<{dataFromEntityFieldDescription.Type.ToFullName()}> {dataFromEntityFieldDescription.ToFieldName()};"; return((FieldDeclarationSyntax)SyntaxFactory.ParseMemberDeclaration(template)); }
// Gets or created a field declaration for a type as needed. // This will first check if a RW one is available, if that is the case we should use that. // If not it will check to see if a RO one is available, use that and promote to RW if needed. // Finally, if we don't have one at all, let's create one with the appropriate access rights internal DataFromEntityFieldDescription GetOrCreateDataAccessField(ITypeSymbol type, bool asReadOnly, PatchableMethod.AccessorDataType patchableMethodDataType) { if (DataFromEntityFields.TryGetValue(type, out var result)) { if (result.IsReadOnly && !asReadOnly) { DataFromEntityFields[type] = new DataFromEntityFieldDescription(false, type, patchableMethodDataType); } return(DataFromEntityFields[type]); } DataFromEntityFields[type] = new DataFromEntityFieldDescription(asReadOnly, type, patchableMethodDataType); return(DataFromEntityFields[type]); }
internal static string ToFieldName(this DataFromEntityFieldDescription dataFromEntityFieldDescription) => $"__{dataFromEntityFieldDescription.Type.ToValidVariableName()}_FromEntity";