private void GenerateIfForEachImplementedBy( MethodBuilder method, ComplexTypeDescriptor complexTypeDescriptor, Func <ObjectTypeDescriptor, IfBuilder> generator) { if (!(complexTypeDescriptor is InterfaceTypeDescriptor interfaceTypeDescriptor) || !interfaceTypeDescriptor.ImplementedBy.Any()) { return; } var ifChain = generator(interfaceTypeDescriptor.ImplementedBy.First()); foreach (ObjectTypeDescriptor objectTypeDescriptor in interfaceTypeDescriptor.ImplementedBy.Skip(1)) { ifChain.AddIfElse( generator(objectTypeDescriptor) .SkipIndents()); } ifChain.AddElse( CodeInlineBuilder.New() .SetText($"throw new {TypeNames.NotSupportedException}();")); method.AddCode(ifChain); }
private void AddArrayHandler( ClassBuilder classBuilder, MethodBuilder methodBuilder, ListTypeDescriptor listTypeDescriptor, HashSet <string> processed) { var listVarName = GetParameterName(listTypeDescriptor.Name) + "s"; methodBuilder .AddCode( AssignmentBuilder .New() .SetLefthandSide($"var {listVarName}") .SetRighthandSide( CodeBlockBuilder .New() .AddCode("new ") .AddCode(TypeNames.List) .AddCode("<") .AddCode( listTypeDescriptor.InnerType .ToStateTypeReference() .SkipTrailingSpace()) .AddCode(">") .AddCode("()"))) .AddEmptyLine() .AddCode( ForEachBuilder .New() .SetLoopHeader( $"{TypeNames.JsonElement} {_child} in {_obj}.Value.EnumerateArray()") .AddCode( MethodCallBuilder .New() .SetMethodName(listVarName, nameof(List <object> .Add)) .AddArgument( BuildUpdateMethodCall( listTypeDescriptor.InnerType, CodeInlineBuilder.From(_child))))) .AddEmptyLine() .AddCode($"return {listVarName};"); AddDeserializeMethod(listTypeDescriptor.InnerType, classBuilder, processed); }
private static ICode BuildPropertyComparison( ITypeDescriptor type, string propertyName) { const string other = nameof(other); return(BuildPropertyInternal(type, true)); ICode BuildPropertyInternal( ITypeDescriptor currentType, bool isNullable) { return(currentType switch { NonNullTypeDescriptor d => BuildPropertyInternal(d.InnerType, false), ILeafTypeDescriptor d when d.SerializationType.IsValueType => CodeInlineBuilder .New() .SetText($"{propertyName} == {other}.{propertyName}"), INamedTypeDescriptor when isNullable => ConditionBuilder .New() .Set($"({propertyName} is null && {other}.{propertyName} is null) ||" + $"{propertyName} != null && {propertyName}.{nameof(Equals)}(" + $"{other}.{propertyName})"), INamedTypeDescriptor => MethodCallBuilder .Inline() .SetMethodName(propertyName, nameof(Equals)) .AddArgument($"{other}.{propertyName}"), ListTypeDescriptor => MethodCallBuilder .Inline() .SetMethodName(TypeNames.SequenceEqual) .AddArgument(propertyName) .AddArgument($"{other}.{propertyName}"), _ => throw new ArgumentOutOfRangeException() }); }
protected override void Generate(ITypeDescriptor typeDescriptor, CSharpSyntaxGeneratorSettings settings, CodeWriter writer, out string fileName, out string?path, out string ns) { ComplexTypeDescriptor complexTypeDescriptor = typeDescriptor as ComplexTypeDescriptor ?? throw new InvalidOperationException( "A result entity mapper can only be generated for complex types"); var className = CreateResultInfoName(complexTypeDescriptor.RuntimeType.Name); fileName = className; path = State; ns = CreateStateNamespace(complexTypeDescriptor.RuntimeType.NamespaceWithoutGlobal); ClassBuilder classBuilder = ClassBuilder .New() .AddImplements(TypeNames.IOperationResultDataInfo) .SetName(fileName); ConstructorBuilder constructorBuilder = classBuilder .AddConstructor() .SetTypeName(complexTypeDescriptor.RuntimeType.Name); foreach (var prop in complexTypeDescriptor.Properties) { TypeReferenceBuilder propTypeBuilder = prop.Type.ToStateTypeReference(); // Add Property to class classBuilder .AddProperty(prop.Name) .SetComment(prop.Description) .SetType(propTypeBuilder) .SetPublic(); // Add initialization of property to the constructor var paramName = GetParameterName(prop.Name); constructorBuilder.AddParameter(paramName).SetType(propTypeBuilder); constructorBuilder.AddCode( AssignmentBuilder .New() .SetLefthandSide(prop.Name) .SetRighthandSide(paramName)); } classBuilder .AddProperty("EntityIds") .SetType(TypeNames.IReadOnlyCollection.WithGeneric(TypeNames.EntityId)) .AsLambda(settings.IsStoreEnabled() ? CodeInlineBuilder.From(_entityIds) : MethodCallBuilder.Inline() .SetMethodName(TypeNames.Array, "Empty") .AddGeneric(TypeNames.EntityId)); classBuilder .AddProperty("Version") .SetType(TypeNames.UInt64) .AsLambda(settings.IsStoreEnabled() ? _version : "0"); if (settings.IsStoreEnabled()) { AddConstructorAssignedField( TypeNames.IReadOnlyCollection.WithGeneric(TypeNames.EntityId), _entityIds, entityIds, classBuilder, constructorBuilder); AddConstructorAssignedField( TypeNames.UInt64, _version, version, classBuilder, constructorBuilder, true); } // WithVersion classBuilder .AddMethod("WithVersion") .SetAccessModifier(AccessModifier.Public) .SetReturnType(TypeNames.IOperationResultDataInfo) .AddParameter(version, x => x.SetType(TypeNames.UInt64)) .AddCode(MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(className) .AddArgumentRange( complexTypeDescriptor.Properties.Select(x => x.Name.Value)) .If(settings.IsStoreEnabled(), x => x.AddArgument(_entityIds).AddArgument(version))); classBuilder.Build(writer); }
private void AddDataHandler( ClassBuilder classBuilder, ConstructorBuilder constructorBuilder, MethodBuilder method, NamedTypeDescriptor namedTypeDescriptor, HashSet <string> processed, bool isNonNullable) { method.AddParameter( ParameterBuilder.New() .SetType( $"global::{namedTypeDescriptor.Namespace}.State." + DataTypeNameFromTypeName(namedTypeDescriptor.GraphQLTypeName)) .SetName(DataParamName)); if (!isNonNullable) { method.AddCode( EnsureProperNullability( DataParamName, isNonNullable)); } var variableName = "returnValue"; method.AddCode($"{namedTypeDescriptor.Name} {variableName} = default!;"); method.AddEmptyLine(); if (namedTypeDescriptor.ImplementedBy.Any()) { var ifChain = InterfaceImplementeeIf(namedTypeDescriptor.ImplementedBy[0]); foreach (NamedTypeDescriptor interfaceImplementee in namedTypeDescriptor.ImplementedBy.Skip(1)) { var singleIf = InterfaceImplementeeIf(interfaceImplementee).SkipIndents(); ifChain.AddIfElse(singleIf); } ifChain.AddElse( CodeInlineBuilder.New() .SetText($"throw new {TypeNames.NotSupportedException}();")); method.AddCode(ifChain); } IfBuilder InterfaceImplementeeIf(NamedTypeDescriptor interfaceImplementee) { var ifCorrectType = IfBuilder.New(); if (isNonNullable) { ifCorrectType.SetCondition( $"{DataParamName}.__typename.Equals(\"" + $"{interfaceImplementee.GraphQLTypeName}\", " + $"{TypeNames.OrdinalStringComparisson})"); } else { ifCorrectType.SetCondition( $"{DataParamName}?.__typename.Equals(\"" + $"{interfaceImplementee.GraphQLTypeName}\", " + $"{TypeNames.OrdinalStringComparisson}) ?? false"); } var constructorCall = MethodCallBuilder.New() .SetPrefix($"{variableName} = new ") .SetMethodName(interfaceImplementee.Name); foreach (PropertyDescriptor prop in interfaceImplementee.Properties) { var propAccess = $"{DataParamName}.{prop.Name}"; if (prop.Type.IsEntityType()) { constructorCall.AddArgument( BuildMapMethodCall( DataParamName, prop, true)); } else { constructorCall.AddArgument( $"{propAccess} ?? throw new {TypeNames.ArgumentNullException}()"); } } ifCorrectType.AddCode(constructorCall); return(ifCorrectType); } method.AddCode($"return {variableName};"); AddRequiredMapMethods( DataParamName, namedTypeDescriptor, classBuilder, constructorBuilder, processed); }
public static LambdaBuilder SetCode(this LambdaBuilder builder, string code) { return(builder.SetCode(CodeInlineBuilder.From(code))); }
protected override void Generate(EntityIdFactoryDescriptor descriptor, CSharpSyntaxGeneratorSettings settings, CodeWriter writer, out string fileName, out string?path, out string ns) { fileName = descriptor.Name; path = State; ns = descriptor.Namespace; ClassBuilder classBuilder = ClassBuilder .New() .SetAccessModifier(AccessModifier.Public) .AddImplements(TypeNames.IEntityIdSerializer) .SetName(fileName); classBuilder .AddField(_options) .SetStatic() .SetReadOnly() .SetType(TypeNames.JsonWriterOptions) .SetValue(CodeBlockBuilder .New() .AddCode(MethodCallBuilder .Inline() .SetNew() .SetMethodName(TypeNames.JsonWriterOptions)) .AddCode(CodeInlineBuilder.From("{ Indented = false }"))); classBuilder .AddMethod("Parse") .SetAccessModifier(AccessModifier.Public) .SetReturnType(TypeNames.EntityId) .AddParameter(_obj, x => x.SetType(TypeNames.JsonElement)) .AddCode(ParseEntityIdBody(descriptor)); classBuilder .AddMethod("Format") .SetAccessModifier(AccessModifier.Public) .SetReturnType(TypeNames.String) .AddParameter(_entityId, x => x.SetType(TypeNames.EntityId)) .AddCode(FormatEntityIdBody(descriptor)); foreach (var entity in descriptor.Entities) { classBuilder .AddMethod($"Parse{entity.Name}EntityId") .SetAccessModifier(AccessModifier.Private) .SetReturnType(TypeNames.EntityId) .AddParameter(_obj, x => x.SetType(TypeNames.JsonElement)) .AddParameter(_type, x => x.SetType(TypeNames.String)) .AddCode(ParseSpecificEntityIdBody(entity)); classBuilder .AddMethod($"Format{entity.Name}EntityId") .SetAccessModifier(AccessModifier.Private) .SetReturnType(TypeNames.String) .AddParameter(_entityId, x => x.SetType(TypeNames.EntityId)) .AddCode(FormatSpecificEntityIdBody(entity)); } classBuilder.Build(writer); }