public BinaryMemberCompositeStruct AddStructToSource(IComponentResolver resolver) { var builder = new CodeSourceFileBuilder(_typeName.Namespace); var genericArgs = _member.ReturnType !.Symbol is ITypeParameterSymbol symbol ? $"<{symbol}>" : ""; var components = _memberComponents.Components; var relyingMembers = _features.SelectMany(x => x.RelyingMembers).ToList(); var constLength = GetConstantLength(); var methods = new List <MethodSignature>(); builder.AttributeHideEditor().AttributeGenerated(typeof(BinaryMemberCompositeBuilder).Assembly); builder.NestType(_typeName, $"internal readonly struct {_typeName.TypeName} {genericArgs}", node => { node.NewLine().Comment("Component Fields"); for (var i = 0; i < components.Count; ++i) { node.State($"private {components[i]} {MemberName.ComponentField(i)}"); } node.Comment("Member Composer Constructor"); { const string parser = "parser"; node.AddNode($"internal {_typeName.TypeName} ({typeof(IBinaryNamespaceDiscovery).FullName} {parser})", node => { // Get's the namespace for us node.State($"{parser} = {parser}.{nameof(INamespaceDiscovery.WithNamespace)}({JoinedNamespaces(_normalNamespaces)})" + $".{nameof(INamespaceDiscovery.WithPrioritizedNamespace)}({JoinedNamespaces(_prioritizedNamespaces)})" + $"as {typeof(IBinaryNamespaceDiscovery).FullName}"); for (var i = 0; i < components.Count; ++i) { node.State($"{MemberName.ComponentField(i)} = {parser}.{nameof(IBinaryNamespaceDiscovery.GetConverter)}<{components[i]}>()"); } }).NewLine();
public IMemberComposer CreateStruct(ITypeComposer typeComposer, IComponentProvider provider, Func <GeneratingTypeName, string, INamedTypeSymbol> symbolProvider) { var components = _componentCollection.Components.Select(x => provider.ProvideComponent(x)).ToList(); var builder = new CodeSourceFileBuilder(_typeName.Namespace); builder.Using("System"); builder.AttributeHideEditor().AttributeGenerated(typeof(MemberComposerSource).Assembly); builder.NestType(_typeName, $"internal readonly struct {_typeName.TypeName} {GetGenericArgs()}", node => { // Fields & Field Initialization AddComponents(builder, components).NewLine(); AddComponentInitializers(builder, provider.DiscoveryType, components).NewLine(); // Construtor AddConstructor(builder, provider.DiscoveryType, components).NewLine(); AddTryConstructor(builder, provider.DiscoveryType, components).NewLine(); // Entry Point builder.Comment("Dependency Member Entry Point"); builder.AddParsingMethods(new ParsingMethodWithTypeBuilder(_member)); // Data Condition Methods for (int i = 0; i < _conditions.Count; i++) { builder.AddFormatterMethods(_conditions[i], _member, i, GetDefaultNames).NewLine(); } // Data Format Method builder.AddFormatterFinalMethods(_format, _member, _conditions.Count, GetDefaultNames).NewLine(); // Data Resolve Method builder.Comment("Data Resolver").NewLine(); for (var i = 0; i < components.Count; i++) { builder.AddParsingBody(components[i], _member, _componentCollection.GetMethodNames(i)).NewLine(); } // Nested Throw Helpers builder.Comment("Throw Helpers (Avoid inlining throw action)"); _throwCollection.AddThrowClass(builder); }); return(new MemberComposer(typeComposer, _member, symbolProvider(_typeName, builder.ToString()), GetMethodSignatures(provider))); }
public TypeComposer AddStructToSource(IBinarySchema schema, IComponentResolver resolver, GeneratingTypeName typeName, Func <MemberMetaInfo, GeneratingTypeName> memberCompositeNameProvider) { const string discovery = nameof(discovery); var mCompositName = schema.SerializeOrder.Select(x => memberCompositeNameProvider(x)).ToList(); var composites = schema.SerializeOrder.Select((x, i) => _memberFactory.AddMemberCompositeStruct(x, mCompositName[i], resolver)).ToList(); var builder = new CodeSourceFileBuilder(typeName.Namespace); builder.Using("System"); builder.Using("System.Collections.Generic"); builder.AttributeHideEditor(); builder.AttributeGenerated(typeof(BinaryTypeCompositeBuilder).Assembly); builder.NestType(typeName, $"internal readonly struct {typeName.TypeName}", node => { for (var i = 0; i < mCompositName.Count; ++i) { node.State($"public {mCompositName[i]} {Property.MemberName(i)} {{ get; }}"); } node.State($"public static IReadOnlyList<int?> BinaryLength {{ get; }} = new int?[] {{ {string.Join(",", composites.Select(x => x.ConstantLength))} }}"); node.NewLine(); node.AddNode($"public {typeName.TypeName} ({typeof(IBinaryNamespaceDiscovery).FullName} {discovery})", node => { for (var i = 0; i < mCompositName.Count; ++i) { node.State($"{Property.MemberName(i)} = new {mCompositName[i]} ({discovery})"); } }).NewLine(); // Serialize Span node.AddMethod(_methods.SerializeSpan, node => { }); node.AddMethod(_methods.SerializeWriter, node => { }); node.AddMethod(_methods.DeserializeSpan, node => { }); node.AddMethod(_methods.DeserializeCursor, node => { }); node.AddMethod(_methods.GetSpanLength, node => { node.Return(string.Join("+ ", composites.Select(stringCompose))); string stringCompose(BinaryMemberCompositeStruct composite) { if (composite.ConstantLength.HasValue) { return(composite.ConstantLength.ToString()); } var typeParams = _methods.GetSpanLength.TypeParameters.Select(x => x.GenericName); var args = _methods.GetSpanLength.Arguments.Select(x => x.ArgName); return(new BinaryConverterMethods(composite.Name, schema.SourceType.Symbol).GetSpanLength.GetInvocationString(typeParams, args)); } }); node.AddMethod(_methods.GetBlockLengthSpan, node => { const string length = nameof(length); var currentIteration = 0; node.State($"var {length} = 0"); for (var i = 0; i < composites.Count; ++i) { } }); }); }