private string ProcessList(INamedTypeSymbol type) { if (CheckExistingMethod(n => n == $"IList<{_type.Name}>") is { } existing) { return(existing); } var method = _builder.AddMethod($"Write{type.Name}List", Accessibility.Private) .AddParameter("IBigEndianWriter", "writer") .AddParameter($"IList<{type.Name}>", "list") .MakeStaticMethod(); var processor = new TypeProcessor(_builder, type, "element"); string step = processor.Process(); method.WithBody(w => { w.AppendLine("writer.WriteU2((ushort) list.Count);"); using (w.Block("foreach (var element in list)")) { w.AppendLine(step); } }); return($"{method.Name}(writer, {_access});"); }
private string ProcessList(INamedTypeSymbol type) { if (CheckExistingMethod(n => n == $"IList<{_type.Name}>") is { } existing) { return(existing); } var method = _builder.AddMethod($"Read{type.Name}List", Accessibility.Private) .WithReturnType($"IList<{type.Name}>") .AddParameter("IBigEndianReader", "reader") .MakeStaticMethod(); var processor = new TypeProcessor(_builder, type, "var element = "); string step = processor.Process(); method.WithBody(w => { w.AppendLine("ushort count = reader.ReadU2();"); w.AppendLine($"var list = new List<{type.Name}>(count);"); using (w.Block("for (int i = 0; i < count; i++)")) { w.AppendLine(step); w.AppendLine("list.Add(element);"); } w.AppendLine("return list;"); }); return($"{_access}{method.Name}(reader);"); }
private static void AddMethod(CodeTypeDeclaration CurrentClass, XElement Method) { var MethodName = Method.Attribute("name").Value; var SignatureCount = Method.Elements("signature").Count(); var type = "void"; if (Method.Attribute("return") != null) { type = TranslateType(Method.Attribute("return").Value); } if (SignatureCount == 0) { var method = Builder.AddMethod(CurrentClass, MethodName, type); if (Method.Element("desc") != null) { AddMemberComment(CurrentClass.Name, Method, method.Comments); } if (Method.Element("longdesc") != null) { XNamespace ns = "http://www.w3.org/2003/XInclude"; if (Method.Element("longdesc").Elements(ns + "include").Count() > 0) { MergeIncludes(Method.Element("longdesc"), ns); } SplitCommentLines(GetInnerXML(Method.Element("longdesc")), method.Comments); } } else { Method.Elements("signature").ToList().ForEach(s => AddMethodFromSignature(CurrentClass, MethodName, s, type)); } }
static void Main(string[] args) { ImportLookup["Element"] = "randori.webkit.dom.Element"; ImportLookup["Document"] = "randori.webkit.dom.Document"; ImportLookup["XMLHttpRequest"] = "randori.webkit.xml.XMLHttpRequest"; ImportLookup["Vector.<Element>"] = "randori.webkit.dom.Element"; if (Directory.Exists(OutputDirectory) == false) { Directory.CreateDirectory(OutputDirectory); } else { var files = Directory.EnumerateFiles(OutputDirectory).ToList <string>(); files.ForEach(f => File.Delete(f)); } ReadSourceDirectory().ForEach(e => Entry2Class(e)); if (ClassLookup.ContainsKey("Promise")) { var classDefPromise = ClassLookup["Promise"]; var classDefDeferred = ClassLookup["Deferred"]; AddMethodsToPromise(classDefPromise, classDefDeferred); } foreach (var key in ClassLookup.Keys) { var Class = ClassLookup[key]; AddImports(Class, Builder.Class2Unit[key]); if (Class.Name == "JQueryStatic") { var method = Builder.AddMethod(Class, "J", "JQuery"); var parameter = Builder.AddParameter("list", "object[]", method, null, false); parameter.UserData["IsRestParams"] = true; Builder.AddMethodAttributeArgument(method, "name", ""); Class.Members.Cast <CodeTypeMember>().ToList <CodeTypeMember>().ForEach(m => m.Attributes = m.Attributes | MemberAttributes.Static); } } var provider = new AS3CodeProvider(Builder); foreach (var unit in Builder.Units) { SerializeClass(unit, provider); } Console.WriteLine("Finished, press any key..."); Console.ReadKey(); }
private void MethodAddButton_Click(object sender, EventArgs e) { MemberAttributes attr = MemberAttributes.Public; switch (PropertyAccessBox.Text) { case "public": attr = MemberAttributes.Public; break; case "protected": attr = MemberAttributes.Family; break; case "private": attr = MemberAttributes.Private; break; default: break; } switch (MethodTypeBox.Text) { case "string": _currentClassBuilder.AddMethod <string>(MethodNameBox.Text, new ParameterItem[] { }, attr); break; case "int": _currentClassBuilder.AddMethod <int>(MethodNameBox.Text, new ParameterItem[] { }, attr); break; case "bool": _currentClassBuilder.AddMethod <bool>(MethodNameBox.Text, new ParameterItem[] { }, attr); break; case "void": _currentClassBuilder.AddVoidMethod(name: MethodNameBox.Text, parameterItems: new ParameterItem[] { }, attr: attr); break; default: break; } UpdateClassMembers(); MethodNameBox.Text = ""; }
protected override void Generate( StoreAccessorDescriptor descriptor, CSharpSyntaxGeneratorSettings settings, CodeWriter writer, out string fileName, out string?path, out string ns) { fileName = descriptor.Name; path = State; ns = descriptor.RuntimeType.NamespaceWithoutGlobal; ClassBuilder factory = ClassBuilder .New(fileName) .SetAccessModifier(AccessModifier.Public) .AddImplements(TypeNames.IStoreAccessor); AddThrowNotValidWithoutStore(factory, "OperationStore", TypeNames.IOperationStore); AddThrowNotValidWithoutStore(factory, "EntityStore", TypeNames.IEntityStore); AddThrowNotValidWithoutStore(factory, "EntityIdSerializer", TypeNames.IEntityIdSerializer); factory .AddMethod("GetOperationRequestFactory") .SetPublic() .SetReturnType(TypeNames.IOperationRequestFactory) .AddParameter("resultType", x => x.SetType(TypeNames.Type)) .AddCode(ExceptionBuilder .New(TypeNames.NotSupportedException) .AddArgument( "\"GetOperationRequestFactory is not supported in store less mode\"")); factory .AddMethod("GetOperationResultDataFactory") .SetPublic() .SetReturnType(TypeNames.IOperationResultDataFactory) .AddParameter("resultType", x => x.SetType(TypeNames.Type)) .AddCode(ExceptionBuilder .New(TypeNames.NotSupportedException) .AddArgument( "\"GetOperationResultDataFactory is not supported in store less mode\"")); factory.Build(writer); }
protected override void Generate( InputObjectTypeDescriptor descriptor, CSharpSyntaxGeneratorSettings settings, CodeWriter writer, out string fileName, out string?path, out string ns) { const string serializerResolver = nameof(serializerResolver); const string runtimeValue = nameof(runtimeValue); const string input = nameof(input); const string inputInfo = nameof(inputInfo); const string fields = nameof(fields); fileName = CreateInputValueFormatter(descriptor); path = Serialization; ns = descriptor.RuntimeType.NamespaceWithoutGlobal; string stateNamespace = $"{descriptor.RuntimeType.Namespace}.{State}"; string infoInterfaceType = $"{stateNamespace}.{CreateInputValueInfo(descriptor.Name)}"; ClassBuilder classBuilder = ClassBuilder .New() .SetName(fileName) .AddImplements(TypeNames.IInputObjectFormatter); var neededSerializers = descriptor .Properties .GroupBy(x => x.Type.Name) .ToDictionary(x => x, x => x.First()); // Initialize Method CodeBlockBuilder initialize = classBuilder .AddMethod("Initialize") .SetPublic() .AddParameter(serializerResolver, x => x.SetType(TypeNames.ISerializerResolver)) .AddBody(); foreach (var property in neededSerializers.Values) { if (property.Type.GetName().Value is { } name) { var propertyName = GetFieldName(name) + "Formatter"; initialize .AddAssigment(propertyName) .AddMethodCall() .SetMethodName(serializerResolver, "GetInputValueFormatter") .AddArgument(name.AsStringToken()); classBuilder .AddField(propertyName) .SetAccessModifier(AccessModifier.Private) .SetType(TypeNames.IInputValueFormatter) .SetValue("default!"); }
protected override void Generate( CodeWriter writer, EnumTypeDescriptor descriptor, out string fileName, out string?path) { const string serializedValue = nameof(serializedValue); const string runtimeValue = nameof(runtimeValue); fileName = CreateEnumParserName(descriptor.Name); path = Serialization; ClassBuilder classBuilder = ClassBuilder .New(fileName) .AddImplements(IInputValueFormatter) .AddImplements(ILeafValueParser.WithGeneric(String, descriptor.Name)); classBuilder .AddMethod("Parse") .AddParameter(serializedValue, x => x.SetType(String)) .SetAccessModifier(AccessModifier.Public) .SetReturnType(descriptor.Name) .AddCode(CreateEnumParsingSwitch(serializedValue, descriptor)); classBuilder .AddMethod("Format") .SetAccessModifier(AccessModifier.Public) .SetReturnType(Object) .AddParameter(runtimeValue, x => x.SetType(Object.MakeNullable())) .AddCode(CreateEnumFormattingSwitch(runtimeValue, descriptor)); classBuilder .AddProperty("TypeName") .AsLambda(descriptor.Name.AsStringToken()) .SetPublic() .SetType(String); CodeFileBuilder .New() .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal) .AddType(classBuilder) .Build(writer); }
public void IfAddMethodIsCalledShouldAddNewFunctionWithGivenName() { var builder = new ClassBuilder(ClassName); var target = builder.AddMethod <string>(FunctionName, new ParameterItem[] {}).GetDeclaration(); Assert.AreEqual(1, target.Members.Count); var member = target.Members[0]; Assert.AreEqual(FunctionName, member.Name); }
private static void CreateStaticMethod(CodeTypeDeclaration type, string name, List <String> description, JToken SignatureToken) { var method = Builder.AddMethod(type, name, "void"); Builder.MakeStatic(method); foreach (var line in description) { method.Comments.Add(new CodeCommentStatement(line, true)); } SignatureToken["params"].ToList <JToken>().ForEach(p => AddParam(method, p)); }
protected override void Generate( CodeWriter writer, InputObjectTypeDescriptor namedTypeDescriptor, out string fileName, out string?path) { const string serializerResolver = nameof(serializerResolver); const string runtimeValue = nameof(runtimeValue); const string value = nameof(value); fileName = CreateInputValueFormatter(namedTypeDescriptor); path = Serialization; NameString typeName = namedTypeDescriptor.Name; ClassBuilder classBuilder = ClassBuilder .New() .SetName(fileName) .AddImplements(TypeNames.IInputObjectFormatter); var neededSerializers = namedTypeDescriptor .Properties .GroupBy(x => x.Type.Name) .ToDictionary(x => x, x => x.First()); // Initialize Method CodeBlockBuilder initialize = classBuilder .AddMethod("Initialize") .SetPublic() .AddParameter(serializerResolver, x => x.SetType(TypeNames.ISerializerResolver)) .AddBody(); foreach (var property in neededSerializers.Values) { if (property.Type.GetName().Value is { } name) { var propertyName = GetFieldName(name) + "Formatter"; initialize .AddAssigment(propertyName) .AddMethodCall() .SetMethodName( serializerResolver, "GetInputValueFormatter") .AddArgument(name.AsStringToken()); classBuilder .AddField(propertyName) .SetAccessModifier(AccessModifier.Private) .SetType(TypeNames.IInputValueFormatter) .SetValue("default!"); }
protected override void Generate( CodeWriter writer, DependencyInjectionDescriptor descriptor, out string fileName, out string?path) { fileName = CreateServiceCollectionExtensions(descriptor.Name); path = DependencyInjection; ClassBuilder factory = ClassBuilder .New(fileName) .SetStatic() .SetAccessModifier(AccessModifier.Public); MethodBuilder addClientMethod = factory .AddMethod($"Add{descriptor.Name}") .SetPublic() .SetStatic() .SetReturnType( TypeNames.IClientBuilder.WithGeneric(descriptor.StoreAccessor.RuntimeType)) .AddParameter( _services, x => x.SetThis().SetType(TypeNames.IServiceCollection)) .AddParameter( _strategy, x => x.SetType(TypeNames.ExecutionStrategy) .SetDefault(TypeNames.ExecutionStrategy + "." + "NetworkOnly")) .AddCode(GenerateMethodBody(descriptor)); if (descriptor.TransportProfiles.Count > 1) { addClientMethod .AddParameter(_profile) .SetType(CreateProfileEnumReference(descriptor)) .SetDefault(CreateProfileEnumReference(descriptor) + "." + descriptor.TransportProfiles[0].Name); } foreach (var profile in descriptor.TransportProfiles) { GenerateClientForProfile(factory, descriptor, profile); } factory.AddClass(_clientServiceProvider); CodeFileBuilder .New() .SetNamespace(TypeNames.DependencyInjectionNamespace) .AddType(factory) .Build(writer); }
private static void AddFormatMethods( OperationDescriptor descriptor, ClassBuilder classBuilder) { foreach (var argument in descriptor.Arguments) { classBuilder .AddMethod() .SetPrivate() .SetReturnType(TypeNames.Object.MakeNullable()) .SetName("Format" + GetPropertyName(argument.Name)) .AddParameter(_value, x => x.SetType(argument.Type.ToTypeReference())) .AddCode(GenerateSerializer(argument.Type, _value)); } }
private void AddDeserializeMethod( ITypeDescriptor typeReference, ClassBuilder classBuilder, HashSet <string> processed) { string methodName = DeserializerMethodNameFromTypeName(typeReference); if (processed.Add(methodName)) { MethodBuilder methodBuilder = classBuilder .AddMethod() .SetPrivate() .SetReturnType(typeReference.ToStateTypeReference()) .SetName(methodName); if (typeReference.IsOrContainsEntityType()) { methodBuilder .AddParameter(_session, x => x.SetType(TypeNames.IEntityStoreUpdateSession)) .AddParameter(_obj, x => x.SetType(TypeNames.JsonElement.MakeNullable())) .AddParameter( _entityIds, x => x.SetType(TypeNames.ISet.WithGeneric(TypeNames.EntityId))); } else { methodBuilder .AddParameter(_obj) .SetType(TypeNames.JsonElement.MakeNullable()); } IfBuilder jsonElementNullCheck = IfBuilder .New() .SetCondition($"!{_obj}.HasValue") .AddCode( typeReference.IsNonNullableType() ? ExceptionBuilder.New(TypeNames.ArgumentNullException) : CodeLineBuilder.From("return null;")); methodBuilder .AddCode(jsonElementNullCheck) .AddEmptyLine(); AddDeserializeMethodBody(classBuilder, methodBuilder, typeReference, processed); } }
private static void GenerateClientForProfile( ClassBuilder factory, DependencyInjectionDescriptor descriptor, TransportProfile profile) { factory .AddMethod("ConfigureClient" + profile.Name) .SetPrivate() .SetStatic() .SetReturnType(TypeNames.IServiceCollection) .AddParameter(_parentServices, x => x.SetType(TypeNames.IServiceProvider)) .AddParameter( _strategy, x => x.SetType(TypeNames.ExecutionStrategy) .SetDefault(TypeNames.ExecutionStrategy + "." + "NetworkOnly")) .AddCode(GenerateInternalMethodBody(descriptor, profile)); }
public override void Build(RuntimeState state, ClassBuilder builder) { List <string> parameters = new List <string>(); foreach (Name parameter in this.parameters) { parameters.Add(parameter.name); } Method method = new Method( builder.TypeBuilder.Name + "." + name.name, name.name, true, parameters); method.SetCodeTree(body.BuildCodeTree(state)); builder.AddMethod(method); }
static void Main() { var cb = new ClassBuilder("EntryPoint", TypeAttributes.Public, false); var pi = new ParameterItem(); pi.Name = "s"; pi.Type = typeof(string); IStatementLine[] Statements = new IStatementLine[] { new ExpressionStatementLine(), new ReturnStatementLine() }; var methodInvoke = new MethodInvokeExpressionLine(); methodInvoke.MethodName = "WriteLine"; var test = new TypeReferenceExpressionLine(); test.Type = "System.Console"; methodInvoke.TargetObject = test; var methodInvokeParam = new VariableReferenceExpressionLine(); methodInvokeParam.VariableName = "s"; methodInvoke.Parameters = new IExpressionLine[] { methodInvokeParam }; Statements[1].Expressions.Add(0, new VariableReferenceExpressionLine() { VariableName = "s" }); Statements[0].Expressions.Add(0, methodInvoke); cb.AddMethod <string>("Print", new ParameterItem[] { pi }, MemberAttributes.Public | MemberAttributes.Static, Statements); var cub = new CompileUnitBuilder("Bla"); var typeDecl = cb.GetDeclaration(); cub.AddClass(typeDecl); cub.PublishCode("c:\\Users\\Sytse\\Documents\\GitHubVisualStudio\\Minor_CodeGen\\CodeGen\\test.cs"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new CodeGenStudio()); }
protected override void Generate(ITypeDescriptor typeDescriptor, CSharpSyntaxGeneratorSettings settings, CodeWriter writer, out string fileName, out string?path, out string ns) { ComplexTypeDescriptor descriptor = typeDescriptor as ComplexTypeDescriptor ?? throw new InvalidOperationException( "A result data factory can only be generated for complex types"); fileName = CreateResultFactoryName(descriptor.RuntimeType.Name); path = State; ns = CreateStateNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal); ClassBuilder classBuilder = ClassBuilder .New() .SetName(fileName) .AddImplements( TypeNames.IOperationResultDataFactory.WithGeneric(descriptor.RuntimeType)); ConstructorBuilder constructorBuilder = classBuilder .AddConstructor() .SetTypeName(descriptor.Name); if (settings.IsStoreEnabled()) { AddConstructorAssignedField( TypeNames.IEntityStore, _entityStore, entityStore, classBuilder, constructorBuilder); } MethodCallBuilder returnStatement = MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(descriptor.RuntimeType.Name); foreach (PropertyDescriptor property in descriptor.Properties) { returnStatement .AddArgument(BuildMapMethodCall(settings, _info, property)); } IfBuilder ifHasCorrectType = IfBuilder .New() .SetCondition( $"{_dataInfo} is {CreateResultInfoName(descriptor.RuntimeType.Name)} {_info}") .AddCode(returnStatement); MethodBuilder createMethod = classBuilder .AddMethod("Create") .SetAccessModifier(AccessModifier.Public) .SetReturnType(descriptor.RuntimeType.Name) .AddParameter(_dataInfo, b => b.SetType(TypeNames.IOperationResultDataInfo)) .AddParameter( _snapshot, b => b.SetDefault("null") .SetType(TypeNames.IEntityStoreSnapshot.MakeNullable())); if (settings.IsStoreEnabled()) { createMethod .AddCode( IfBuilder.New() .SetCondition($"{_snapshot} is null") .AddCode( AssignmentBuilder .New() .SetLefthandSide(_snapshot) .SetRighthandSide($"{_entityStore}.CurrentSnapshot"))) .AddEmptyLine(); } createMethod.AddCode(ifHasCorrectType) .AddEmptyLine() .AddCode( ExceptionBuilder .New(TypeNames.ArgumentException) .AddArgument( $"\"{CreateResultInfoName(descriptor.RuntimeType.Name)} expected.\"")); var processed = new HashSet <string>(); AddRequiredMapMethods( settings, _info, descriptor, classBuilder, constructorBuilder, processed, true); classBuilder .AddProperty("ResultType") .SetType(TypeNames.Type) .AsLambda($"typeof({descriptor.RuntimeType.Namespace}.{descriptor.Implements[0]})") .SetInterface(TypeNames.IOperationResultDataFactory); classBuilder .AddMethod("Create") .SetInterface(TypeNames.IOperationResultDataFactory) .SetReturnType(TypeNames.Object) .AddParameter(_dataInfo, b => b.SetType(TypeNames.IOperationResultDataInfo)) .AddParameter( _snapshot, b => b.SetType(TypeNames.IEntityStoreSnapshot.MakeNullable())) .AddCode( MethodCallBuilder .New() .SetReturn() .SetMethodName("Create") .AddArgument(_dataInfo) .AddArgument(_snapshot)); classBuilder.Build(writer); }
private void AddBuildDataMethod( CSharpSyntaxGeneratorSettings settings, InterfaceTypeDescriptor resultNamedType, ClassBuilder classBuilder) { var concreteType = CreateResultInfoName( resultNamedType.ImplementedBy.First().RuntimeType.Name); MethodBuilder buildDataMethod = classBuilder .AddMethod() .SetPrivate() .SetName("BuildData") .SetReturnType($"({resultNamedType.RuntimeType.Name}, {concreteType})") .AddParameter(_obj, x => x.SetType(TypeNames.JsonElement)); if (settings.IsStoreEnabled()) { buildDataMethod.AddCode( AssignmentBuilder .New() .SetLefthandSide($"var {_entityIds}") .SetRighthandSide(MethodCallBuilder .Inline() .SetNew() .SetMethodName(TypeNames.HashSet) .AddGeneric(TypeNames.EntityId))) .AddCode( AssignmentBuilder .New() .SetLefthandSide($"{TypeNames.IEntityStoreSnapshot} {_snapshot}") .SetRighthandSide("default!")); } buildDataMethod.AddEmptyLine(); CodeBlockBuilder storeUpdateBody = CodeBlockBuilder.New(); if (settings.IsStoreEnabled()) { foreach (PropertyDescriptor property in resultNamedType.Properties.Where(prop => prop.Type.IsOrContainsEntity())) { var variableName = $"{GetParameterName(property.Name)}Id"; buildDataMethod .AddCode(AssignmentBuilder .New() .SetLefthandSide(CodeBlockBuilder .New() .AddCode(property.Type.ToStateTypeReference()) .AddCode(variableName)) .SetRighthandSide("default!")); storeUpdateBody .AddCode(AssignmentBuilder .New() .SetLefthandSide(variableName) .SetRighthandSide(BuildUpdateMethodCall(property))); } storeUpdateBody .AddEmptyLine() .AddCode(AssignmentBuilder .New() .SetLefthandSide(_snapshot) .SetRighthandSide($"{_session}.CurrentSnapshot")); buildDataMethod .AddCode(MethodCallBuilder .New() .SetMethodName(_entityStore, "Update") .AddArgument(LambdaBuilder .New() .AddArgument(_session) .SetBlock(true) .SetCode(storeUpdateBody))); } buildDataMethod .AddEmptyLine() .AddCode( AssignmentBuilder .New() .SetLefthandSide($"var {_resultInfo}") .SetRighthandSide( CreateResultInfoMethodCall(settings, resultNamedType, concreteType))) .AddEmptyLine() .AddCode( TupleBuilder .Inline() .SetDetermineStatement(true) .SetReturn() .AddMember(MethodCallBuilder .Inline() .SetMethodName(_resultDataFactory, "Create") .AddArgument(_resultInfo)) .AddMember(_resultInfo)); }
private void AddBuildMethod( InterfaceTypeDescriptor resultNamedType, ClassBuilder classBuilder) { var buildMethod = classBuilder .AddMethod() .SetAccessModifier(AccessModifier.Public) .SetName("Build") .SetReturnType( TypeReferenceBuilder .New() .SetName(TypeNames.IOperationResult) .AddGeneric(resultNamedType.RuntimeType.Name)); buildMethod .AddParameter(_response) .SetType(TypeNames.Response.WithGeneric(TypeNames.JsonDocument)); var concreteResultType = CreateResultInfoName(resultNamedType.ImplementedBy.First().RuntimeType.Name); buildMethod.AddCode( AssignmentBuilder .New() .SetLefthandSide( $"({resultNamedType.RuntimeType.Name} Result, {concreteResultType} " + "Info)? data") .SetRighthandSide("null")); buildMethod.AddCode( AssignmentBuilder .New() .SetLefthandSide( TypeNames.IReadOnlyList .WithGeneric(TypeNames.IClientError) .MakeNullable() + " errors") .SetRighthandSide("null")); buildMethod.AddEmptyLine(); buildMethod.AddCode( TryCatchBuilder .New() .AddTryCode( IfBuilder .New() .SetCondition( ConditionBuilder .New() .Set("response.Body != null")) .AddCode( IfBuilder .New() .SetCondition( ConditionBuilder .New() .Set("response.Body.RootElement.TryGetProperty(" + $"\"data\", out {TypeNames.JsonElement} " + "dataElement) && dataElement.ValueKind == " + $"{TypeNames.JsonValueKind}.Object")) .AddCode("data = BuildData(dataElement);")) .AddCode( IfBuilder .New() .SetCondition( ConditionBuilder .New() .Set( "response.Body.RootElement.TryGetProperty(" + $"\"errors\", out {TypeNames.JsonElement} " + "errorsElement)")) .AddCode($"errors = {TypeNames.ParseError}(errorsElement);"))) .AddCatchBlock( CatchBlockBuilder .New() .SetExceptionVariable("ex") .AddCode( AssignmentBuilder.New() .SetLefthandSide("errors") .SetRighthandSide( ArrayBuilder.New() .SetDetermineStatement(false) .SetType(TypeNames.IClientError) .AddAssigment( MethodCallBuilder .Inline() .SetNew() .SetMethodName(TypeNames.ClientError) .AddArgument("ex.Message") .AddArgument("exception: ex")))))); buildMethod.AddEmptyLine(); buildMethod.AddCode( MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(TypeNames.OperationResult) .AddGeneric(resultNamedType.RuntimeType.Name) .AddArgument("data?.Result") .AddArgument("data?.Info") .AddArgument(_resultDataFactory) .AddArgument("errors")); }
private void AddBuildMethod( InterfaceTypeDescriptor resultNamedType, ClassBuilder classBuilder) { var buildMethod = classBuilder .AddMethod() .SetAccessModifier(AccessModifier.Public) .SetName("Build") .SetReturnType( TypeReferenceBuilder .New() .SetName(TypeNames.IOperationResult) .AddGeneric(resultNamedType.RuntimeType.Name)); buildMethod .AddParameter(_response) .SetType(TypeNames.Response.WithGeneric(TypeNames.JsonDocument)); var concreteResultType = CreateResultInfoName(resultNamedType.ImplementedBy.First().RuntimeType.Name); // (IGetFooResult Result, GetFooResultInfo Info)? data = null; buildMethod.AddCode( AssignmentBuilder .New() .SetLefthandSide( $"({resultNamedType.RuntimeType.Name} Result, {concreteResultType} " + "Info)? data") .SetRighthandSide("null")); // IReadOnlyList<IClientError>? errors = null; buildMethod.AddCode( AssignmentBuilder .New() .SetLefthandSide( TypeNames.IReadOnlyList .WithGeneric(TypeNames.IClientError) .MakeNullable() + " errors") .SetRighthandSide("null")); buildMethod.AddEmptyLine(); buildMethod.AddEmptyLine(); buildMethod.AddCode( IfBuilder.New() .SetCondition("response.Exception is null") .AddCode(CreateBuildDataSerialization()) .AddElse(CreateDataError("response.Exception")) ); buildMethod.AddEmptyLine(); buildMethod.AddCode( MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(TypeNames.OperationResult) .AddGeneric(resultNamedType.RuntimeType.Name) .AddArgument("data?.Result") .AddArgument("data?.Info") .AddArgument(_resultDataFactory) .AddArgument("errors")); }
protected override void Generate(ITypeDescriptor typeDescriptor, CSharpSyntaxGeneratorSettings settings, CodeWriter writer, out string fileName, out string?path, out string ns) { // Setup class ComplexTypeDescriptor descriptor = typeDescriptor as ComplexTypeDescriptor ?? throw new InvalidOperationException( "A result entity mapper can only be generated for complex types"); fileName = descriptor.ExtractMapperName(); path = State; ns = CreateStateNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal); ClassBuilder classBuilder = ClassBuilder .New() .AddImplements( TypeNames.IEntityMapper .WithGeneric( descriptor.ExtractType().ToString(), descriptor.RuntimeType.Name)) .SetName(fileName); ConstructorBuilder constructorBuilder = ConstructorBuilder .New() .SetTypeName(descriptor.Name); AddConstructorAssignedField( TypeNames.IEntityStore, _entityStore, entityStore, classBuilder, constructorBuilder); // Define map method MethodBuilder mapMethod = MethodBuilder .New() .SetName(_map) .SetAccessModifier(AccessModifier.Public) .SetReturnType(descriptor.RuntimeType.Name) .AddParameter( ParameterBuilder .New() .SetType( descriptor.Kind == TypeKind.Entity ? CreateEntityType( descriptor.Name, descriptor.RuntimeType.NamespaceWithoutGlobal) .ToString() : descriptor.Name) .SetName(_entity)) .AddParameter( _snapshot, b => b.SetDefault("null") .SetType(TypeNames.IEntityStoreSnapshot.MakeNullable())); mapMethod .AddCode(IfBuilder .New() .SetCondition($"{_snapshot} is null") .AddCode(AssignmentBuilder .New() .SetLefthandSide(_snapshot) .SetRighthandSide($"{_entityStore}.CurrentSnapshot"))) .AddEmptyLine(); MethodCallBuilder constructorCall = MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(descriptor.RuntimeType.Name); if (typeDescriptor is ComplexTypeDescriptor complexTypeDescriptor) { foreach (PropertyDescriptor property in complexTypeDescriptor.Properties) { constructorCall.AddArgument(BuildMapMethodCall(settings, _entity, property)); } } mapMethod.AddCode(constructorCall); if (constructorBuilder.HasParameters()) { classBuilder.AddConstructor(constructorBuilder); } classBuilder.AddMethod(mapMethod); AddRequiredMapMethods( settings, _entity, descriptor, classBuilder, constructorBuilder, new HashSet <string>()); classBuilder.Build(writer); }
protected override void Generate(OperationDescriptor descriptor, CSharpSyntaxGeneratorSettings settings, CodeWriter writer, out string fileName, out string?path, out string ns) { var documentName = CreateDocumentTypeName(descriptor.RuntimeType.Name); fileName = documentName; path = null; ns = descriptor.RuntimeType.NamespaceWithoutGlobal; string operationKind = descriptor switch { MutationOperationDescriptor => "Mutation", QueryOperationDescriptor => "Query", SubscriptionOperationDescriptor => "Subscription", _ => throw new ArgumentOutOfRangeException(nameof(descriptor)) }; ClassBuilder classBuilder = ClassBuilder .New() .SetName(fileName) .AddImplements(TypeNames.IDocument) .SetComment( XmlCommentBuilder .New() .SetSummary( string.Format( CodeGenerationResources.OperationServiceDescriptor_Description, descriptor.Name)) .AddCode(descriptor.BodyString)); classBuilder .AddConstructor() .SetPrivate(); classBuilder .AddProperty("Instance") .SetStatic() .SetType(documentName) .SetValue($"new {documentName}()"); classBuilder .AddProperty("Kind") .SetType(TypeNames.OperationKind) .AsLambda($"{TypeNames.OperationKind}.{operationKind}"); if (descriptor.Strategy == RequestStrategy.PersistedQuery) { classBuilder .AddProperty("Body") .SetType(TypeNames.IReadOnlySpan.WithGeneric(TypeNames.Byte)) .AsLambda($"new {TypeNames.Byte}[0]"); } else { classBuilder .AddProperty("Body") .SetType(TypeNames.IReadOnlySpan.WithGeneric(TypeNames.Byte)) .AsLambda(GetByteArray(descriptor.Body)); } classBuilder .AddProperty("Hash") .SetType(TypeNames.DocumentHash) .SetValue( $@"new {TypeNames.DocumentHash}(" + $@"""{descriptor.HashAlgorithm}"", " + $@"""{descriptor.HashValue}"")"); classBuilder .AddMethod("ToString") .SetPublic() .SetOverride() .SetReturnType(TypeNames.String) .AddCode("#if NETSTANDARD2_0") .AddCode(MethodCallBuilder .New() .SetReturn() .SetMethodName(TypeNames.EncodingUtf8, nameof(Encoding.UTF8.GetString)) .AddArgument("Body.ToArray()")) .AddCode("#else") .AddCode(MethodCallBuilder .New() .SetReturn() .SetMethodName(TypeNames.EncodingUtf8, nameof(Encoding.UTF8.GetString)) .AddArgument("Body")) .AddCode("#endif"); classBuilder.Build(writer); }
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); }
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); }
public void IfAddMethodIsCalledTwiceWithTheSameNameAnExceptionShouldBeenThrown() { var builder = new ClassBuilder(ClassName); builder.AddMethod <string>(FunctionName, new ParameterItem[] { }).AddMethod <string>(FunctionName, new ParameterItem[] { }).GetDeclaration(); }
protected override void Generate( CodeWriter writer, OperationDescriptor descriptor, out string fileName, out string?path) { fileName = descriptor.RuntimeType.Name; path = null; ClassBuilder classBuilder = ClassBuilder .New() .SetComment( XmlCommentBuilder .New() .SetSummary( string.Format( CodeGenerationResources.OperationServiceDescriptor_Description, descriptor.Name)) .AddCode(descriptor.BodyString)) .AddImplements(descriptor.InterfaceType.ToString()) .SetName(fileName); ConstructorBuilder constructorBuilder = classBuilder .AddConstructor() .SetTypeName(fileName); var runtimeTypeName = descriptor.ResultTypeReference.GetRuntimeType().Name; AddConstructorAssignedField( TypeNames.IOperationExecutor.WithGeneric(runtimeTypeName), _operationExecutor, classBuilder, constructorBuilder); AddInjectedSerializers(descriptor, constructorBuilder, classBuilder); if (descriptor is not SubscriptionOperationDescriptor) { classBuilder.AddMethod(CreateExecuteMethod(descriptor, runtimeTypeName)); } classBuilder.AddMethod(CreateWatchMethod(descriptor, runtimeTypeName)); classBuilder.AddMethod(CreateRequestMethod(descriptor)); classBuilder.AddMethod(CreateRequestVariablesMethod(descriptor)); AddFormatMethods(descriptor, classBuilder); classBuilder .AddProperty("ResultType") .SetType(TypeNames.Type) .AsLambda($"typeof({runtimeTypeName})") .SetInterface(TypeNames.IOperationRequestFactory); MethodCallBuilder createRequestCall = MethodCallBuilder .New() .SetReturn() .SetMethodName(_createRequest); if (descriptor.Arguments.Count > 0) { createRequestCall.AddArgument($"{_variables}!"); } classBuilder .AddMethod("Create") .SetReturnType(TypeNames.OperationRequest) .SetInterface(TypeNames.IOperationRequestFactory) .AddParameter( _variables, x => x.SetType( TypeNames.IReadOnlyDictionary .WithGeneric(TypeNames.String, TypeNames.Object.MakeNullable()) .MakeNullable())) .AddCode(createRequestCall); CodeFileBuilder .New() .SetNamespace(descriptor.RuntimeType.NamespaceWithoutGlobal) .AddType(classBuilder) .Build(writer); }
private void AddEntityOrUnionDataHandler( CSharpSyntaxGeneratorSettings settings, ClassBuilder classBuilder, ConstructorBuilder constructorBuilder, MethodBuilder method, ComplexTypeDescriptor complexTypeDescriptor, HashSet <string> processed, bool isNonNullable) { method .AddParameter(_dataParameterName) .SetType(TypeNames.EntityIdOrData.MakeNullable(!isNonNullable)) .SetName(_dataParameterName); method .AddParameter(_snapshot) .SetType(TypeNames.IEntityStoreSnapshot) .SetName(_snapshot); if (!isNonNullable) { method.AddCode(EnsureProperNullability(_dataParameterName, isNonNullable)); } var dataHandlerMethodName = MapMethodNameFromTypeName(complexTypeDescriptor) + "Entity"; MethodBuilder complexDataHandler = MethodBuilder .New() .SetReturnType( complexTypeDescriptor.RuntimeType.ToString().MakeNullable(!isNonNullable)) .SetName(dataHandlerMethodName); AddComplexDataHandler(settings, classBuilder, constructorBuilder, complexDataHandler, complexTypeDescriptor, processed, isNonNullable); classBuilder.AddMethod(complexDataHandler); var entityDataHandlerMethodName = MapMethodNameFromTypeName(complexTypeDescriptor) + "Data"; MethodBuilder entityDataHandler = MethodBuilder .New() .SetReturnType( complexTypeDescriptor.RuntimeType.ToString().MakeNullable(!isNonNullable)) .SetName(entityDataHandlerMethodName); AddEntityHandler( classBuilder, constructorBuilder, entityDataHandler, complexTypeDescriptor, processed, isNonNullable); classBuilder.AddMethod(entityDataHandler); method.AddEmptyLine(); var parameterName = isNonNullable ? _dataParameterName : $"{_dataParameterName}.Value"; IfBuilder ifBuilder = IfBuilder .New() .SetCondition($"{parameterName}.EntityId is {{ }} id") .AddCode(MethodCallBuilder .New() .SetReturn() .SetMethodName(entityDataHandlerMethodName) .AddArgument("id") .AddArgument(_snapshot)) .AddIfElse(IfBuilder .New() .SetCondition( $"{parameterName}.Data is {complexTypeDescriptor.ParentRuntimeType!} d") .AddCode(MethodCallBuilder .New() .SetReturn() .SetMethodName(dataHandlerMethodName) .AddArgument("d") .AddArgument(_snapshot))) .AddElse(ExceptionBuilder.New(TypeNames.ArgumentOutOfRangeException)); method.AddCode(ifBuilder); AddRequiredMapMethods( settings, _dataParameterName, complexTypeDescriptor, classBuilder, constructorBuilder, processed); }