private ICode CreateEntityConstructorCall( ObjectTypeDescriptor objectTypeDescriptor, bool assignDefault) { var propertyLookup = objectTypeDescriptor.Properties.ToDictionary(x => x.Name.Value); MethodCallBuilder newEntity = MethodCallBuilder .Inline() .SetNew() .SetMethodName(objectTypeDescriptor.EntityTypeDescriptor.RuntimeType.ToString()); foreach (PropertyDescriptor property in objectTypeDescriptor.EntityTypeDescriptor.Properties.Values) { if (propertyLookup.TryGetValue(property.Name.Value, out var ownProperty)) { newEntity.AddArgument(BuildUpdateMethodCall(ownProperty)); } else if (assignDefault) { newEntity.AddArgument("default!"); } else { newEntity.AddArgument($"{_entity}.{property.Name}"); } } return(MethodCallBuilder .New() .SetMethodName(_session, "SetEntity") .AddArgument(_entityId) .AddArgument(newEntity)); }
private static ICode RegisterWebSocketConnection(string clientName) => MethodCallBuilder .New() .SetMethodName(TypeNames.AddSingleton) .AddArgument(_services) .AddArgument(LambdaBuilder .New() .AddArgument(_sp) .SetBlock(true) .SetCode(CodeBlockBuilder .New() .AddCode(AssignmentBuilder .New() .SetLefthandSide($"var {_sessionPool}") .SetRighthandSide(MethodCallBuilder .Inline() .SetMethodName(TypeNames.GetRequiredService) .AddGeneric(TypeNames.ISessionPool) .AddArgument(_parentServices))) .AddCode(MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(TypeNames.WebSocketConnection) .AddArgument(LambdaBuilder .New() .SetCode(MethodCallBuilder .Inline() .SetMethodName( _sessionPool, nameof(ISessionPool.CreateAsync)) .AddArgument(clientName.AsStringToken()) .AddArgument("default"))))));
private void AddDataTypeDeserializerMethod( ClassBuilder classBuilder, MethodBuilder methodBuilder, ComplexTypeDescriptor complexTypeDescriptor, HashSet <string> processed) { if (complexTypeDescriptor is InterfaceTypeDescriptor interfaceTypeDescriptor) { AddInterfaceDataTypeDeserializerToMethod(methodBuilder, interfaceTypeDescriptor); } else { MethodCallBuilder returnStatement = MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(complexTypeDescriptor.Name); foreach (PropertyDescriptor property in complexTypeDescriptor.Properties) { returnStatement.AddArgument(BuildUpdateMethodCall(property)); } methodBuilder.AddCode(returnStatement); } AddRequiredDeserializeMethods(complexTypeDescriptor, classBuilder, processed); }
public static MethodCallBuilder AddMethodCall(this CodeBlockBuilder builder) { var methodCallBuilder = MethodCallBuilder.New(); builder.AddCode(methodCallBuilder); return(methodCallBuilder); }
private static ICode RegisterHttpConnection(string clientName) => MethodCallBuilder .New() .SetMethodName(TypeNames.AddSingleton) .AddArgument(_services) .AddArgument(LambdaBuilder .New() .AddArgument(_sp) .SetBlock(true) .SetCode(CodeBlockBuilder .New() .AddCode(AssignmentBuilder .New() .SetLefthandSide($"var {_clientFactory}") .SetRighthandSide(MethodCallBuilder .Inline() .SetMethodName(TypeNames.GetRequiredService) .AddGeneric(TypeNames.IHttpClientFactory) .AddArgument(_parentServices))) .AddCode(MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(TypeNames.HttpConnection) .AddArgument(LambdaBuilder .New() .SetCode(MethodCallBuilder .Inline() .SetMethodName( _clientFactory, nameof(IHttpClientFactory.CreateClient)) .AddArgument(clientName.AsStringToken()))))));
private MethodCallBuilder CreateBuildDataStatement(ObjectTypeDescriptor concreteType) { MethodCallBuilder returnStatement = MethodCallBuilder .New() .SetNew() .SetMethodName( $"{concreteType.RuntimeType.Namespace}.State." + CreateDataTypeName(concreteType.Name)) .AddArgument("typename"); foreach (PropertyDescriptor property in concreteType.Properties) { if (property.Name.Value.EqualsOrdinal(WellKnownNames.TypeName)) { continue; } returnStatement.AddArgument( CodeBlockBuilder .New() .AddCode($"{GetParameterName(property.Name)}: ") .AddCode(BuildUpdateMethodCall(property))); } return(returnStatement); }
private static ICode RegisterSerializerResolver() => MethodCallBuilder .New() .SetMethodName(TypeNames.AddSingleton) .AddGeneric(TypeNames.ISerializerResolver) .AddArgument(_services) .AddArgument(LambdaBuilder .New() .AddArgument(_sp) .SetCode( MethodCallBuilder .Inline() .SetNew() .SetMethodName(TypeNames.SerializerResolver) .SetWrapArguments() .AddArgument(MethodCallBuilder .Inline() .SetMethodName(TypeNames.Concat) .AddArgument( MethodCallBuilder .Inline() .SetMethodName(TypeNames.GetRequiredService) .SetWrapArguments() .AddGeneric( TypeNames.IEnumerable.WithGeneric( TypeNames.ISerializer)) .AddArgument(_parentServices)) .AddArgument(MethodCallBuilder .Inline() .SetMethodName(TypeNames.GetRequiredService) .SetWrapArguments() .AddGeneric( TypeNames.IEnumerable.WithGeneric(TypeNames.ISerializer)) .AddArgument(_sp)))));
private static ICode RegisterSerializerResolver() => MethodCallBuilder.New() .SetMethodName(TypeNames.AddSingleton) .AddGeneric(TypeNames.ISerializerResolver) .AddArgument("services") .AddArgument(LambdaBuilder.New() .AddArgument("sp") .SetCode( MethodCallBuilder.New() .SetPrefix("new ") .SetMethodName(TypeNames.SerializerResolver) .SetDetermineStatement(false) .SetWrapArguments() .AddArgument(MethodCallBuilder.New() .SetMethodName(TypeNames.Concat) .SetDetermineStatement(false) .AddArgument( MethodCallBuilder.New() .SetMethodName(TypeNames.GetRequiredService) .SetDetermineStatement(false) .SetWrapArguments() .AddGeneric( TypeNames.IEnumerable.WithGeneric(TypeNames .ISerializer)) .AddArgument("parentServices")) .AddArgument(MethodCallBuilder.New() .SetMethodName(TypeNames.GetRequiredService) .SetDetermineStatement(false) .SetWrapArguments() .AddGeneric( TypeNames.IEnumerable.WithGeneric(TypeNames.ISerializer)) .AddArgument("sp")))));
protected override void Generate( CodeWriter writer, OperationDescriptor descriptor, out string fileName) { var documentName = CreateDocumentTypeName(descriptor.Name); fileName = documentName; string operationKind = descriptor switch { MutationOperationDescriptor => "Mutation", QueryOperationDescriptor => "Query", SubscriptionOperationDescriptor => "Subscription", _ => throw new ArgumentOutOfRangeException(nameof(descriptor)) }; ClassBuilder classBuilder = ClassBuilder .New() .AddImplements(TypeNames.IDocument) .SetName(fileName); classBuilder .AddConstructor() .SetPrivate(); classBuilder .AddProperty("Instance") .SetStatic() .SetType(documentName) .SetValue($"new {documentName}()"); classBuilder .AddProperty("Kind") .SetType(TypeNames.OperationKind) .AsLambda($"{TypeNames.OperationKind}.{operationKind}"); classBuilder .AddProperty("Body") .SetType(TypeNames.IReadOnlySpan.WithGeneric(TypeNames.Byte)) .AsLambda(GetByteArray(descriptor.BodyString)); classBuilder .AddMethod("ToString") .SetPublic() .SetOverride() .SetReturnType(TypeNames.String) .AddCode(MethodCallBuilder .New() .SetReturn() .SetMethodName(TypeNames.EncodingUtf8, nameof(Encoding.UTF8.GetString)) .AddArgument("Body")); CodeFileBuilder .New() .SetNamespace(descriptor.Namespace) .AddType(classBuilder) .Build(writer); }
private static ICode GenerateEntityHandlerIfClause( ObjectTypeDescriptor objectTypeDescriptor, bool isNonNullable) { var dataMapperName = GetFieldName( CreateEntityMapperName( objectTypeDescriptor.RuntimeType.Name, objectTypeDescriptor.Name)); MethodCallBuilder constructorCall = MethodCallBuilder .New() .SetReturn() .SetWrapArguments() .SetMethodName(dataMapperName, nameof(IEntityMapper <object, object> .Map)); MethodCallBuilder argument = MethodCallBuilder .Inline() .SetMethodName(StoreFieldName, nameof(IEntityStore.GetEntity)) .AddGeneric(CreateEntityTypeName(objectTypeDescriptor.Name)) .AddArgument(isNonNullable ? _entityId : $"{_entityId}.Value"); constructorCall.AddArgument( NullCheckBuilder .New() .SetDetermineStatement(false) .SetCondition(argument) .SetCode(ExceptionBuilder.Inline(TypeNames.GraphQLClientException))); IfBuilder ifCorrectType = IfBuilder .New() .AddCode(constructorCall) .SetCondition( MethodCallBuilder .Inline() .SetMethodName( isNonNullable ? new[] { _entityId, nameof(EntityId.Name), nameof(string.Equals) } : new[] { _entityId, nameof(Nullable <EntityId> .Value), nameof(EntityId.Name), nameof(string.Equals) }) .AddArgument(objectTypeDescriptor.Name.AsStringToken()) .AddArgument(TypeNames.OrdinalStringComparison)); return(CodeBlockBuilder .New() .AddEmptyLine() .AddCode(ifCorrectType)); }
private void AddArrayHandler( ClassBuilder classBuilder, ConstructorBuilder constructorBuilder, MethodBuilder methodBuilder, ListTypeDescriptor listTypeDescriptor, HashSet <string> processed, bool isNonNullable) { methodBuilder.AddParameter( ParameterBuilder.New() .SetType(listTypeDescriptor.ToEntityIdBuilder()) .SetName(ListParamName)); var listVarName = listTypeDescriptor.Name.WithLowerFirstChar() + "s"; if (!isNonNullable) { methodBuilder.AddCode(EnsureProperNullability(ListParamName, isNonNullable)); } methodBuilder.AddCode( AssignmentBuilder.New() .SetLefthandSide($"var {listVarName}") .SetRighthandSide( CodeBlockBuilder.New() .AddCode("new ") .AddCode(TypeNames.List) .AddCode("<") .AddCode( listTypeDescriptor.InnerType.ToBuilder() .SkipTrailingSpace()) .AddCode(">") .AddCode("()"))); methodBuilder.AddEmptyLine(); var loopbuilder = ForEachBuilder.New() .SetLoopHeader( CodeBlockBuilder.New() .AddCode(listTypeDescriptor.InnerType.ToEntityIdBuilder()) .AddCode($"child in {ListParamName}")) .AddCode( MethodCallBuilder.New() .SetPrefix($"{listVarName}.") .SetMethodName("Add") .AddArgument( BuildMapMethodCall( listTypeDescriptor.InnerType, "child"))); methodBuilder.AddCode(loopbuilder); methodBuilder.AddEmptyLine(); methodBuilder.AddCode($"return {listVarName};"); AddMapMethod( listVarName, listTypeDescriptor.InnerType, classBuilder, constructorBuilder, processed); }
private void AddInterfaceDataTypeDeserializerToMethod( MethodBuilder methodBuilder, InterfaceTypeDescriptor interfaceTypeDescriptor) { methodBuilder.AddCode( AssignmentBuilder .New() .SetLefthandSide($"var {_typename}") .SetRighthandSide(MethodCallBuilder .Inline() .SetMethodName( _obj, "Value", nameof(JsonElement.GetProperty)) .AddArgument(WellKnownNames.TypeName.AsStringToken()) .Chain(x => x.SetMethodName(nameof(JsonElement.GetString))))); // If the type is an interface foreach (ObjectTypeDescriptor concreteType in interfaceTypeDescriptor.ImplementedBy) { MethodCallBuilder returnStatement = MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName( $"{concreteType.RuntimeType.Namespace}.State." + CreateDataTypeName(concreteType.Name)) .AddArgument("typename"); foreach (PropertyDescriptor property in concreteType.Properties) { if (property.Name.Value.EqualsOrdinal(WellKnownNames.TypeName)) { continue; } returnStatement.AddArgument( CodeBlockBuilder .New() .AddCode($"{GetParameterName(property.Name)}: ") .AddCode(BuildUpdateMethodCall(property))); } IfBuilder ifStatement = IfBuilder .New() .SetCondition( $"typename?.Equals(\"{concreteType.Name}\", " + $"{TypeNames.OrdinalStringComparison}) ?? false") .AddCode(returnStatement); methodBuilder .AddEmptyLine() .AddCode(ifStatement); } methodBuilder .AddEmptyLine() .AddCode(ExceptionBuilder.New(TypeNames.NotSupportedException)); }
public static CodeBlockBuilder AddMethodCall( this CodeBlockBuilder builder, Action <MethodCallBuilder> configure) { var methodCallBuilder = MethodCallBuilder.New(); configure(methodCallBuilder); return(builder.AddCode(methodCallBuilder)); }
private void AddBuildMethod( InterfaceTypeDescriptor resultNamedType, ClassBuilder classBuilder) { var responseParameterName = "response"; var buildMethod = MethodBuilder .New() .SetAccessModifier(AccessModifier.Public) .SetName("Build") .SetReturnType( TypeReferenceBuilder.New() .SetName(TypeNames.IOperationResult) .AddGeneric(resultNamedType.RuntimeType.Name)) .AddParameter( ParameterBuilder.New() .SetType( TypeReferenceBuilder.New() .SetName(TypeNames.Response) .AddGeneric(TypeNames.JsonDocument) .SetName(TypeNames.Response)) .SetName(responseParameterName)); var concreteResultType = CreateResultInfoName(resultNamedType.ImplementedBy.First().RuntimeType.Name); buildMethod.AddCode( AssignmentBuilder.New() .SetLefthandSide( $"({resultNamedType.RuntimeType.Name} Result, {concreteResultType} " + "Info)? data") .SetRighthandSide("null")); buildMethod.AddEmptyLine(); buildMethod.AddCode( IfBuilder.New() .SetCondition( ConditionBuilder.New() .Set("response.Body is not null") .And("response.Body.RootElement.TryGetProperty(\"data\"," + $" out {TypeNames.JsonElement} obj)")) .AddCode("data = BuildData(obj);")); buildMethod.AddEmptyLine(); buildMethod.AddCode( MethodCallBuilder.New() .SetPrefix("return new ") .SetMethodName( TypeNames.OperationResult.WithGeneric(resultNamedType.RuntimeType.Name)) .AddArgument("data?.Result") .AddArgument("data?.Info") .AddArgument(_resultDataFactoryFieldName) .AddArgument("null")); classBuilder.AddMethod(buildMethod); }
private void AddUpdateEntityMethod( ClassBuilder classBuilder, MethodBuilder methodBuilder, INamedTypeDescriptor namedTypeDescriptor, HashSet <string> processed) { methodBuilder.AddCode( AssignmentBuilder .New() .SetLefthandSide($"{TypeNames.EntityId} {_entityId}") .SetRighthandSide( MethodCallBuilder .Inline() .SetMethodName(_idSerializer, "Parse") .AddArgument($"{_obj}.Value"))); methodBuilder.AddCode( MethodCallBuilder .New() .SetMethodName(_entityIds, nameof(List <object> .Add)) .AddArgument(_entityId)); methodBuilder.AddEmptyLine(); if (namedTypeDescriptor is InterfaceTypeDescriptor interfaceTypeDescriptor) { // If the type is an interface foreach (ObjectTypeDescriptor concreteType in interfaceTypeDescriptor.ImplementedBy) { methodBuilder .AddEmptyLine() .AddCode(CreateUpdateEntityStatement(concreteType) .AddCode($"return {_entityId};")); } methodBuilder.AddEmptyLine(); methodBuilder.AddCode(ExceptionBuilder.New(TypeNames.NotSupportedException)); } else if (namedTypeDescriptor is ObjectTypeDescriptor objectTypeDescriptor) { BuildTryGetEntityIf( CreateEntityType( objectTypeDescriptor.Name, objectTypeDescriptor.RuntimeType.NamespaceWithoutGlobal)) .AddCode(CreateEntityConstructorCall(objectTypeDescriptor, false)) .AddElse(CreateEntityConstructorCall(objectTypeDescriptor, true)); methodBuilder.AddEmptyLine(); methodBuilder.AddCode($"return {_entityId};"); } AddRequiredDeserializeMethods(namedTypeDescriptor, classBuilder, processed); }
protected override void Generate( CodeWriter writer, OperationDescriptor operationDescriptor, out string fileName) { var(classBuilder, constructorBuilder) = CreateClassBuilder(); fileName = operationDescriptor.Name; classBuilder.SetName(fileName); constructorBuilder.SetTypeName(fileName); var resultTypeReference = (INamedTypeDescriptor)operationDescriptor.ResultTypeReference.NamedType(); AddConstructorAssignedField( TypeReferenceBuilder.New() .SetName(TypeNames.IOperationExecutor) .AddGeneric(resultTypeReference.RuntimeType.Name), OperationExecutorFieldName, classBuilder, constructorBuilder); var neededSerializers = operationDescriptor.Arguments .ToLookup(x => x.Type.Name) .Select(x => x.First()) .ToDictionary(x => x.Type.Name); if (neededSerializers.Any()) { constructorBuilder .AddParameter( "serializerResolver", x => x.SetType(TypeNames.ISerializerResolver)); foreach (var property in neededSerializers.Values) { if (property.Type.GetName().Value is { } name) { MethodCallBuilder call = MethodCallBuilder.New() .SetMethodName("serializerResolver." + nameof(ISerializerResolver.GetInputValueFormatter)) .AddArgument(name.AsStringToken() ?? "") .SetPrefix($"{GetFieldName(name)}Formatter = "); constructorBuilder.AddCode(call); } else { throw new InvalidOperationException( $"Serialized for property {operationDescriptor.Name}.{property.Name} " + $"could not be created. GraphQLTypeName was empty"); } }
private static ICode GenerateEntityHandlerIfClause( ObjectTypeDescriptor objectTypeDescriptor, bool isNonNullable) { var dataMapperName = GetFieldName( CreateEntityMapperName( objectTypeDescriptor.RuntimeType.Name, objectTypeDescriptor.Name)); var ifCorrectType = IfBuilder.New(); if (isNonNullable) { ifCorrectType.SetCondition( $"{EntityIdParamName}.Name.Equals(\"" + $"{objectTypeDescriptor.Name}\", " + $"{TypeNames.OrdinalStringComparison})"); } else { ifCorrectType.SetCondition( $"{EntityIdParamName}.Value.Name.Equals(\"" + $"{objectTypeDescriptor.Name}\", " + $"{TypeNames.OrdinalStringComparison})"); } MethodCallBuilder constructorCall = MethodCallBuilder.New() .SetPrefix($"return {dataMapperName}.") .SetWrapArguments() .SetMethodName(nameof(IEntityMapper <object, object> .Map)); MethodCallBuilder argument = MethodCallBuilder.New() .SetMethodName($"{StoreFieldName}.{nameof(IEntityStore.GetEntity)}") .SetDetermineStatement(false) .AddGeneric(CreateEntityTypeName(objectTypeDescriptor.Name)) .AddArgument(isNonNullable ? EntityIdParamName : $"{EntityIdParamName}.Value"); constructorCall.AddArgument( NullCheckBuilder.New() .SetDetermineStatement(false) .SetCondition(argument) .SetCode(ExceptionBuilder .New(TypeNames.GraphQLClientException) .SetDetermineStatement(false))); ifCorrectType.AddCode(constructorCall); return(CodeBlockBuilder.New() .AddEmptyLine() .AddCode(ifCorrectType)); }
private MethodCallBuilder BuildUpdateMethodCall(ITypeDescriptor property, string firstArg) { var deserializeMethodCaller = MethodCallBuilder.New() .SetDetermineStatement(false) .SetMethodName(DeserializerMethodNameFromTypeName(property)); deserializeMethodCaller.AddArgument(firstArg); if (property.IsEntityType() || property.ContainsEntity()) { deserializeMethodCaller.AddArgument(_entityIdsParam); } return(deserializeMethodCaller); }
private static ICode ForwardSingletonToClientServiceProvider(string generic) => MethodCallBuilder.New() .SetMethodName(TypeNames.AddSingleton) .AddArgument("services") .AddArgument(LambdaBuilder.New() .AddArgument("sp") .SetCode(MethodCallBuilder.New() .SetMethodName(TypeNames.GetRequiredService) .SetDetermineStatement(false) .SetWrapArguments() .AddArgument(MethodCallBuilder.New() .SetMethodName(TypeNames.GetRequiredService) .SetDetermineStatement(false) .AddGeneric("ClientServiceProvider") .AddArgument("sp")) .AddGeneric(generic)));
private void AddScalarTypeDeserializerMethod( MethodBuilder methodBuilder, ILeafTypeDescriptor namedType) { string deserializeMethod = JsonUtils.GetParseMethod(namedType.SerializationType); methodBuilder.AddCode( MethodCallBuilder .New() .SetReturn() .SetMethodName(GetFieldName(namedType.Name) + "Parser", "Parse") .AddArgument(MethodCallBuilder .Inline() .SetMethodName(_obj, nameof(Nullable <JsonElement> .Value), deserializeMethod) .SetNullForgiving())); }
private static ICode GenerateMethodBody(DependencyInjectionDescriptor descriptor) => CodeBlockBuilder .New() .AddMethodCall(x => x.SetMethodName(TypeNames.AddSingleton) .AddArgument(_services) .AddArgument(LambdaBuilder .New() .SetBlock(true) .AddArgument(_sp) .SetCode( CodeBlockBuilder .New() .AddCode( AssignmentBuilder .New() .SetLefthandSide($"var {_serviceCollection}") .SetRighthandSide( MethodCallBuilder .Inline() .SetNew() .SetMethodName(TypeNames.ServiceCollection))) .AddEmptyLine() .AddMethodCall(x => x.SetMethodName("ConfigureClient") .AddArgument(_serviceCollection) .AddArgument(_sp) .AddArgument(_strategy)) .AddEmptyLine() .AddCode(MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName("ClientServiceProvider") .SetWrapArguments() .AddArgument(MethodCallBuilder .Inline() .SetMethodName(TypeNames.BuildServiceProvider) .AddArgument(_serviceCollection)))))) .AddEmptyLine() .ForEach( descriptor.Operations, (builder, operation) => builder.AddCode(ForwardSingletonToClientServiceProvider(operation.Name))) .AddEmptyLine() .AddCode(ForwardSingletonToClientServiceProvider(descriptor.Name)) .AddEmptyLine() .AddLine($"return {_services};");
private MethodCallBuilder GetMappingCall( NamedTypeDescriptor namedTypeDescriptor, string idName) { return(MethodCallBuilder.New() .SetMethodName( EntityMapperNameFromGraphQLTypeName( namedTypeDescriptor.Name, namedTypeDescriptor.GraphQLTypeName ?? throw new ArgumentNullException("GraphQLTypeName")) .ToFieldName() + ".Map") .SetDetermineStatement(false) .AddArgument( $"{StoreParamName}.GetEntity<" + $"{EntityTypeNameFromGraphQLTypeName(namedTypeDescriptor.GraphQLTypeName)}>" + $"({idName}) ?? throw new {TypeNames.ArgumentNullException}()")); }
public static MethodCallBuilder AddMethodCall( this AssignmentBuilder builder, string?methodName = null) { MethodCallBuilder methodCallBuilder = MethodCallBuilder .New() .SetDetermineStatement(false); if (methodName is not null) { methodCallBuilder.SetMethodName(methodName); } builder.SetRighthandSide(methodCallBuilder); return(methodCallBuilder); }
private MethodCallBuilder GetMappingCall( ComplexTypeDescriptor complexTypeDescriptor, string idName) { return(MethodCallBuilder.New() .SetMethodName( GetFieldName( CreateEntityMapperName( complexTypeDescriptor.RuntimeType.Name, complexTypeDescriptor.Name)) + ".Map") .SetDetermineStatement(false) .AddArgument( $"{StoreParamName}.GetEntity<" + $"{CreateEntityTypeName(complexTypeDescriptor.Name)}>" + $"({idName}) ?? throw new {TypeNames.ArgumentNullException}()")); }
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 IfBuilder GenerateDataInterfaceIfClause( ObjectTypeDescriptor objectTypeDescriptor, bool isNonNullable, string variableName) { var ifCorrectType = IfBuilder.New(); if (isNonNullable) { ifCorrectType.SetCondition( $"{_dataParameterName}.__typename.Equals(\"" + $"{objectTypeDescriptor.Name}\", " + $"{TypeNames.OrdinalStringComparison})"); } else { ifCorrectType.SetCondition( $"{_dataParameterName}?.__typename.Equals(\"" + $"{objectTypeDescriptor.Name}\", " + $"{TypeNames.OrdinalStringComparison}) ?? false"); } var constructorCall = MethodCallBuilder.New() .SetPrefix($"{variableName} = new ") .SetMethodName(objectTypeDescriptor.RuntimeType.Name); foreach (PropertyDescriptor prop in objectTypeDescriptor.Properties) { var propAccess = $"{_dataParameterName}.{prop.Name}"; if (prop.Type.IsEntityType()) { constructorCall.AddArgument(BuildMapMethodCall(_dataParameterName, prop, true)); } else { constructorCall.AddArgument( $"{propAccess} ?? throw new {TypeNames.ArgumentNullException}()"); } } ifCorrectType.AddCode(constructorCall); return(ifCorrectType); }
private MethodCallBuilder BuildUpdateMethodCall( PropertyDescriptor property, string propertyAccess = ".Value") { var deserializeMethodCaller = MethodCallBuilder .New() .SetDetermineStatement(false) .SetMethodName(DeserializerMethodNameFromTypeName(property.Type)); deserializeMethodCaller.AddArgument( $"{TypeNames.GetPropertyOrNull}({_objParamName}{propertyAccess}, " + $"\"{property.Name.WithLowerFirstChar()}\")"); if (property.Type.IsEntityType() || property.Type.ContainsEntity()) { deserializeMethodCaller.AddArgument(_entityIdsParam); } return(deserializeMethodCaller); }
private void AddScalarTypeDeserializerMethod( MethodBuilder methodBuilder, ILeafTypeDescriptor namedType) { string deserializeMethod = namedType.SerializationType.ToString() switch { TypeNames.String => nameof(JsonElement.GetString), TypeNames.Uri => nameof(JsonElement.GetString), TypeNames.Byte => nameof(JsonElement.GetByte), TypeNames.ByteArray => nameof(JsonElement.GetBytesFromBase64), TypeNames.Int16 => nameof(JsonElement.GetInt16), TypeNames.Int32 => nameof(JsonElement.GetInt32), TypeNames.Int64 => nameof(JsonElement.GetInt64), TypeNames.UInt16 => nameof(JsonElement.GetUInt16), TypeNames.UInt32 => nameof(JsonElement.GetUInt32), TypeNames.UInt64 => nameof(JsonElement.GetUInt64), TypeNames.Single => nameof(JsonElement.GetSingle), TypeNames.Double => nameof(JsonElement.GetDouble), TypeNames.Decimal => nameof(JsonElement.GetDecimal), TypeNames.DateTimeOffset => nameof(JsonElement.GetString), TypeNames.DateTime => nameof(JsonElement.GetString), TypeNames.TimeSpan => nameof(JsonElement.GetString), TypeNames.Boolean => nameof(JsonElement.GetBoolean), TypeNames.Guid => nameof(JsonElement.GetGuid), _ => throw new NotSupportedException("Serialization format not supported.") }; methodBuilder.AddCode( MethodCallBuilder .New() .SetReturn() .SetMethodName( GetFieldName(namedType.Name) + "Parser", nameof(ILeafValueParser <object, object> .Parse)) .AddArgument(MethodCallBuilder .Inline() .SetMethodName(_obj, nameof(Nullable <JsonElement> .Value), deserializeMethod) .SetNullForgiving())); } }
private void AddArrayHandler( ClassBuilder classBuilder, MethodBuilder methodBuilder, ListTypeDescriptor listTypeDescriptor, HashSet <string> processed) { var listVarName = listTypeDescriptor.Name.WithLowerFirstChar() + "s"; methodBuilder.AddCode( AssignmentBuilder.New() .SetLefthandSide($"var {listVarName}") .SetRighthandSide( CodeBlockBuilder.New() .AddCode("new ") .AddCode(TypeNames.List) .AddCode("<") .AddCode(listTypeDescriptor.InnerType.ToEntityIdBuilder() .SkipTrailingSpace()) .AddCode(">") .AddCode("()") )); methodBuilder.AddEmptyLine(); methodBuilder.AddCode( ForEachBuilder.New() .SetLoopHeader( $"{TypeNames.JsonElement} child in {_objParamName}.Value.EnumerateArray()") .AddCode( MethodCallBuilder.New() .SetPrefix($"{listVarName}.") .SetMethodName("Add") .AddArgument( BuildUpdateMethodCall(listTypeDescriptor.InnerType, "child")))); methodBuilder.AddEmptyLine(); methodBuilder.AddCode($"return {listVarName};"); AddDeserializeMethod(listTypeDescriptor.InnerType, classBuilder, processed); }
private IfBuilder GenerateComplexDataInterfaceIfClause( ObjectTypeDescriptor objectTypeDescriptor, string variableName) { var ifCorrectType = IfBuilder.New(); var matchedTypeName = GetParameterName(objectTypeDescriptor.Name); // since we want to create the data name we will need to craft the type name // by hand by using the GraphQL type name and the state namespace. // TODO : state namespace should be available here! var dataTypeName = new RuntimeTypeInfo( CreateDataTypeName(objectTypeDescriptor.Name), $"{objectTypeDescriptor.RuntimeType.Namespace}.State"); ifCorrectType.SetCondition( $"{_dataParameterName} is {dataTypeName} {matchedTypeName}"); var constructorCall = MethodCallBuilder.New() .SetPrefix($"{variableName} = new ") .SetMethodName(objectTypeDescriptor.RuntimeType.ToString()); foreach (PropertyDescriptor prop in objectTypeDescriptor.Properties) { var propAccess = $"{matchedTypeName}.{prop.Name}"; if (prop.Type.IsEntityType()) { constructorCall.AddArgument( BuildMapMethodCall( matchedTypeName, prop)); } else { constructorCall.AddArgument(propAccess); } } return(ifCorrectType.AddCode(constructorCall)); }