private ICode CreateEnumFormattingSwitch( string runtimeValue, EnumTypeDescriptor descriptor) { SwitchExpressionBuilder switchExpression = SwitchExpressionBuilder.New() .SetReturn() .SetExpression(runtimeValue) .SetDefaultCase(ExceptionBuilder.Inline(TypeNames.GraphQLClientException)); foreach (var enumValue in descriptor.Values) { switchExpression.AddCase( $"{descriptor.Name}.{enumValue.RuntimeValue}", enumValue.Name.AsStringToken()); } return(switchExpression); }
private ICode ParseEntityIdBody(EntityIdFactoryDescriptor descriptor) { AssignmentBuilder typeNameAssignment = AssignmentBuilder .New() .SetLefthandSide($"{TypeNames.String} {WellKnownNames.TypeName}") .SetRighthandSide( MethodCallBuilder .Inline() .SetMethodName(_obj, nameof(JsonElement.GetProperty)) .AddArgument(WellKnownNames.TypeName.AsStringToken()) .Chain(x => x .SetMethodName(nameof(JsonElement.GetString)) .SetNullForgiving())); SwitchExpressionBuilder typeNameSwitch = SwitchExpressionBuilder .New() .SetReturn() .SetExpression(WellKnownNames.TypeName) .SetDefaultCase(ExceptionBuilder.Inline(TypeNames.NotSupportedException)); foreach (var entity in descriptor.Entities) { typeNameSwitch.AddCase( entity.Name.AsStringToken(), MethodCallBuilder .Inline() .SetMethodName($"Parse{entity.Name}EntityId") .AddArgument(_obj) .AddArgument(WellKnownNames.TypeName)); } return(CodeBlockBuilder .New() .AddCode(typeNameAssignment) .AddEmptyLine() .AddCode(typeNameSwitch)); }
private ICode FormatEntityIdBody(EntityIdFactoryDescriptor descriptor) { SwitchExpressionBuilder typeNameSwitch = SwitchExpressionBuilder .New() .SetReturn() .SetExpression($"{_entityId}.Name") .SetDefaultCase(ExceptionBuilder.Inline(TypeNames.NotSupportedException)); foreach (var entity in descriptor.Entities) { typeNameSwitch.AddCase( entity.Name.AsStringToken(), MethodCallBuilder .Inline() .SetMethodName($"Format{entity.Name}EntityId") .AddArgument(_entityId)); } return(CodeBlockBuilder .New() .AddCode(typeNameSwitch)); }
private static ICode GenerateClientServiceProviderFactory( DependencyInjectionDescriptor descriptor) { if (descriptor.TransportProfiles.Count == 1) { return(CodeBlockBuilder .New() .AddCode( AssignmentBuilder .New() .SetLefthandSide($"var {_serviceCollection}") .SetRighthandSide( MethodCallBuilder .Inline() .SetMethodName( "ConfigureClient" + descriptor.TransportProfiles[0].Name) .AddArgument(_sp) .AddArgument(_strategy))) .AddEmptyLine() .AddCode(MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName("ClientServiceProvider") .SetWrapArguments() .AddArgument(MethodCallBuilder .Inline() .SetMethodName(TypeNames.BuildServiceProvider) .AddArgument(_serviceCollection)))); } SwitchExpressionBuilder switchBuilder = SwitchExpressionBuilder .New() .SetExpression(_profile) .SetDetermineStatement(false) .SetDefaultCase(ExceptionBuilder.Inline(TypeNames.ArgumentOutOfRangeException)); var enumName = CreateProfileEnumReference(descriptor); foreach (var profile in descriptor.TransportProfiles) { switchBuilder .AddCase( $"{enumName}.{profile.Name}", MethodCallBuilder .Inline() .SetMethodName("ConfigureClient" + profile.Name) .AddArgument(_sp) .AddArgument(_strategy)); } return(CodeBlockBuilder .New() .AddCode( AssignmentBuilder .New() .SetLefthandSide($"var {_serviceCollection}") .SetRighthandSide(switchBuilder)) .AddEmptyLine() .AddCode(MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName("ClientServiceProvider") .SetWrapArguments() .AddArgument(MethodCallBuilder .Inline() .SetMethodName(TypeNames.BuildServiceProvider) .AddArgument(_serviceCollection)))); }