using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace GameCult.Caching { /// /// Registers a generated CultCache document metadata provider with an assembly. /// [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] public sealed class CultGeneratedDocumentMetadataProviderAttribute : Attribute { /// /// Creates an assembly metadata provider registration. /// public CultGeneratedDocumentMetadataProviderAttribute(Type providerType) { ProviderType = providerType ?? throw new ArgumentNullException(nameof(providerType)); } /// /// Gets the provider type that can create generated document definitions. /// public Type ProviderType { get; } } /// /// Supplies generated CultCache document metadata for an assembly. /// public interface ICultGeneratedDocumentMetadataProvider { /// /// Gets the generated document definitions exposed by the provider. /// IEnumerable GetDocumentDefinitions(); } /// /// Describes one generated CultCache document contract. /// public sealed class CultGeneratedDocumentDefinition { /// /// Creates a generated document contract definition. /// public CultGeneratedDocumentDefinition( Type documentType, string schemaName, string schemaVersion, bool isGlobal, string? nameMember, Func? nameAccessor, Func? serializePayload, Func? deserializePayload, IReadOnlyList indexAccessors, IReadOnlyList members) { DocumentType = documentType ?? throw new ArgumentNullException(nameof(documentType)); SchemaName = string.IsNullOrWhiteSpace(schemaName) ? throw new ArgumentException("Schema name must be non-empty.", nameof(schemaName)) : schemaName; SchemaVersion = string.IsNullOrWhiteSpace(schemaVersion) ? throw new ArgumentException("Schema version must be non-empty.", nameof(schemaVersion)) : schemaVersion; IsGlobal = isGlobal; NameMember = nameMember; NameAccessor = nameAccessor; SerializePayload = serializePayload; DeserializePayload = deserializePayload; IndexAccessors = indexAccessors ?? Array.Empty(); Members = members ?? Array.Empty(); } /// /// Gets the CLR document type. /// public Type DocumentType { get; } /// /// Gets the stable CultCache schema name. /// public string SchemaName { get; } /// /// Gets the schema version string. /// public string SchemaVersion { get; } /// /// Gets whether this document type stores one global record. /// public bool IsGlobal { get; } /// /// Gets the member used as the document name, if any. /// public string? NameMember { get; } /// /// Gets the generated name accessor, if any. /// public Func? NameAccessor { get; } /// /// Gets the generated payload serializer, if one was emitted. /// public Func? SerializePayload { get; } /// /// Gets the generated payload deserializer, if one was emitted. /// public Func? DeserializePayload { get; } /// /// Gets the generated index accessors. /// public IReadOnlyList IndexAccessors { get; } /// /// Gets the generated persisted member definitions. /// public IReadOnlyList Members { get; } } /// /// Describes a generated accessor for one CultCache index. /// public sealed class CultGeneratedDocumentIndexAccessor { /// /// Creates a generated index accessor. /// public CultGeneratedDocumentIndexAccessor(string alias, Func accessor) { Alias = string.IsNullOrWhiteSpace(alias) ? throw new ArgumentException("Alias must be non-empty.", nameof(alias)) : alias; Accessor = accessor ?? throw new ArgumentNullException(nameof(accessor)); } /// /// Gets the index alias. /// public string Alias { get; } /// /// Gets the function that extracts the index value. /// public Func Accessor { get; } } /// /// Describes one generated persisted document member. /// public sealed class CultGeneratedDocumentMemberDefinition { /// /// Creates a generated persisted member definition. /// public CultGeneratedDocumentMemberDefinition( string memberName, int slot, string typeName, bool isReference, bool isMany, string? targetSchemaName, bool isName, string? indexAlias) { MemberName = string.IsNullOrWhiteSpace(memberName) ? throw new ArgumentException("Member name must be non-empty.", nameof(memberName)) : memberName; Slot = slot; TypeName = string.IsNullOrWhiteSpace(typeName) ? throw new ArgumentException("Type name must be non-empty.", nameof(typeName)) : typeName; IsReference = isReference; IsMany = isMany; TargetSchemaName = targetSchemaName; IsName = isName; IndexAlias = indexAlias; } /// /// Gets the CLR member name. /// public string MemberName { get; } /// /// Gets the persisted slot number. /// public int Slot { get; } /// /// Gets the persisted type name. /// public string TypeName { get; } /// /// Gets whether this member stores a document reference. /// public bool IsReference { get; } /// /// Gets whether this member stores many references. /// public bool IsMany { get; } /// /// Gets the target schema name for reference members. /// public string? TargetSchemaName { get; } /// /// Gets whether this member is the document name. /// public bool IsName { get; } /// /// Gets the index alias for indexed members. /// public string? IndexAlias { get; } } internal static class CultGeneratedDocumentMetadataLoader { public static IEnumerable LoadDefinitions() { return AppDomain.CurrentDomain.GetAssemblies() .Where(assembly => !assembly.IsDynamic) .SelectMany(LoadDefinitions); } public static IEnumerable LoadDefinitions(Assembly assembly) { foreach (var attribute in assembly.GetCustomAttributes()) { if (!typeof(ICultGeneratedDocumentMetadataProvider).IsAssignableFrom(attribute.ProviderType)) { continue; } if (Activator.CreateInstance(attribute.ProviderType) is not ICultGeneratedDocumentMetadataProvider provider) { continue; } foreach (var definition in provider.GetDocumentDefinitions()) { yield return definition; } } } } internal static class CultSchemaTypeNames { public static string FromType(Type type) { if (type.IsArray) { return FromType(type.GetElementType()!) + "[]"; } if (type.IsGenericType) { var definition = type.GetGenericTypeDefinition(); var name = definition.FullName ?? definition.Name; var tickIndex = name.IndexOf('`'); if (tickIndex >= 0) { name = name[..tickIndex]; } var arguments = string.Join(", ", type.GetGenericArguments().Select(FromType)); return $"{name}<{arguments}>"; } return type.FullName ?? type.Name; } public static string EscapeForLiteral(string value) { if (string.IsNullOrEmpty(value)) { return value; } var builder = new StringBuilder(value.Length); foreach (var character in value) { builder.Append(character switch { '\\' => "\\\\", '"' => "\\\"", '\r' => "\\r", '\n' => "\\n", '\t' => "\\t", _ => character.ToString() }); } return builder.ToString(); } } }