Beispiel #1
0
        protected override void ClientAppenderImpl(Type element, RtInterface result, TypeResolver resolver)
        {
            SignalRGenerationOptions options = SignalRGenerationOptions.All[element];
            string typeName = element.IsInterface && element.Name.StartsWith('I') ? element.Name.Substring(1) : element.Name;

            Context.AddNewLine();
            string      callbacks  = $"{typeName}_Callbacks";
            RtInterface eventInter = new RtInterface
            {
                Name    = new RtSimpleTypeName(callbacks),
                Members =
                {
                },
                Export = false
            };

            eventInter.Members.AddRange(result.Members.OfType <RtFunction>().Select(m => new RtField()
            {
                AccessModifier = AccessModifier.Public, Identifier = m.Identifier, Type = GetCallbackType(m)
            }));
            Context.Location.CurrentNamespace.CompilationUnits.Add(eventInter);
            Context.AddNewLine();

            Context.AddRawToNamespace($"export type {typeName}_CallbackNames = keyof {callbacks};").AddNewLine();
            Context.AddRawToNamespace($"export type {typeName}_Callback<TKey extends {typeName}_CallbackNames> = {callbacks}[TKey];").AddNewLine();
        }
Beispiel #2
0
        private IEnumerable <RtNode> GetImplementationMembers(RtInterface result, string hub)
        {
            List <RtNode>            members   = new List <RtNode>();
            IEnumerable <RtFunction> functions = result.Members.OfType <RtFunction>();

            foreach (RtFunction function in functions)
            {
                List <string> rtTypeNames = function.Arguments.Select(a => a.Type.ToString()).ToList();
                string        generics    = string.Join(",", rtTypeNames);
                if (rtTypeNames.Count > 1)
                {
                    generics = $"[{generics}]";
                }
                string stringType = rtTypeNames.Count == 0 ? "() => void" : $"SimpleEventDispatcher<{generics}>";

                RtField eventDispatcher = new RtField
                {
                    AccessModifier           = AccessModifier.Public,
                    Identifier               = new RtIdentifier($"on{function.Identifier.ToString().FirstCharToUpper()}"),
                    Type                     = new RtSimpleTypeName(stringType),
                    InitializationExpression = $"new {stringType}()"
                };
                members.Add(eventDispatcher);
            }

            return(members);
        }
Beispiel #3
0
        public override void Visit(RtInterface node)
        {
            if (node == null)
            {
                return;
            }
            Visit(node.Documentation);
            var prev = Context;

            Context = WriterContext.Interface;
            AppendTabs();
            if (node.Export)
            {
                Write("export ");
            }
            Write("interface ");
            Visit(node.Name);
            if (node.Implementees.Count > 0)
            {
                Write(" extends ");
                SequentialVisit(node.Implementees, ", ");
            }
            Br(); AppendTabs();
            Write("{"); Br();
            Tab();
            foreach (var rtMember in node.Members.OrderBy(c => c is RtMember ? ((RtMember)c).Order : (double?)null))
            {
                Visit(rtMember);
            }
            UnTab();
            AppendTabs(); WriteLine("}");
            Context = prev;
        }
 public override void Visit(RtInterface rtInterface)
 {
     foreach (var member in rtInterface.Members)
     {
         Visit(member);
     }
 }
Beispiel #5
0
        protected override void ClientAppenderImpl(Type element, RtInterface result, TypeResolver resolver)
        {
            var clientImpl = new RtClass()
            {
                Name         = new RtSimpleTypeName($"{element.Name}Client"),
                Export       = true,
                Decorators   = { new RtDecorator("Injectable()\r\n") },
                Implementees = { result.Name },
                Members      =
                {
                    new RtField
                    {
                        AccessModifier = AccessModifier.Private,
                        Identifier     = new RtIdentifier("hubConnection"),
                        Type           = new RtSimpleTypeName("Promise<HubConnection>")
                    },
                    new RtConstructor
                    {
                        Arguments =   { new RtArgument
                                        {
                                            Type       = new RtSimpleTypeName("HubConnectionProvider"),
                                            Identifier = new RtIdentifier("hubConnectionProvider")
                                        } },
                        Body = new RtRaw("this.hubConnection = hubConnectionProvider.getHubConnection(\"/hub\");"),
                    }
                },
            };

            clientImpl.Members.AddRange(GetImplementationMembers(result));

            Context.Location.CurrentNamespace.CompilationUnits.Add(clientImpl);
        }
        public override RtInterface GenerateNode(Type element, RtInterface node, TypeResolver resolver)
        {
            var n = node;

            if (n == null)
            {
                return(null);
            }
            else
            {
                n = new RtInterface
                {
                    Name = new RtSimpleTypeName
                           (
                        $"I{(element.IsGenericType ? element.Name.Substring(0, element.Name.IndexOf("`")) : element.Name)}",
                        element.IsGenericType ? element.GetGenericArguments().Select(_garg => new RtSimpleTypeName(_garg.Name)).ToArray() : new RtSimpleTypeName[0]
                           )
                }
            };

            foreach (var m in element.GetProperties().Where(_p => _p.DeclaringType == element || element == typeof(BaseModel <>)))
            {
                var generator = resolver.GeneratorFor(m, Context);
                var member    = generator.Generate(m, resolver);

                if (m.PropertyType == typeof(DateTime) || m.PropertyType == typeof(DateTime?))
                {
                    member.As <RtField>().Type = new RtSimpleTypeName("Apollo.Models.JsonDateTime");
                    if (m.PropertyType == typeof(DateTime?))
                    {
                        member.As <RtField>().Identifier.IsNullable = true;
                    }
                }

                else if (m.PropertyType == typeof(TimeSpan) || m.PropertyType == typeof(TimeSpan?))
                {
                    member.As <RtField>().Type = new RtSimpleTypeName(new RtTypeName[0], "Apollo.Models", "JsonDateTime");
                    if (m.PropertyType == typeof(TimeSpan?))
                    {
                        member.As <RtField>().Identifier.IsNullable = true;
                    }
                }

                else if (typeof(IEnumerable <byte>).IsAssignableFrom(m.PropertyType))
                {
                    member.As <RtField>().Type = new RtSimpleTypeName("string");
                }

                n.Members.Add(member);
            }

            if (!element.IsGenericType)
            {
                n.Implementees.Add(new RtSimpleTypeName("IBaseModel", new[] { new RtSimpleTypeName(ReinforcedTypings.ToTypeScriptType(element.BaseType.GetGenericArguments()[0])) }));
            }

            return(n);
        }
    }
Beispiel #7
0
        private IEnumerable <RtNode> GetImplementationMembers(RtInterface result)
        {
            var functions = result.Members.OfType <RtFuncion>();

            foreach (var function in functions)
            {
                var arguments = function.Arguments.Select(a => a.Identifier.ToString());
                function.Body = new RtRaw($"return this.hubConnection.then(hub => hub.invoke(\"{function.Identifier.IdentifierName}\",{string.Join(",", arguments)}));");
            }

            return(functions);
        }
Beispiel #8
0
        protected override void ClientAppenderImpl(Type element, RtInterface result, TypeResolver resolver)
        {
            var typeName   = element.IsInterface && element.Name.StartsWith('I') ? element.Name.Substring(1) : element.Name;
            var clientImpl = new RtClass
            {
                Name       = new RtSimpleTypeName(typeName),
                Export     = true,
                Decorators = { new RtDecorator("Injectable()\r\n") },
            };

            clientImpl.Members.AddRange(GetImplementationMembers(result));

            Context.Location.CurrentNamespace.CompilationUnits.Add(clientImpl);
        }
Beispiel #9
0
        public override void Visit(RtInterface node)
        {
            if (node.Name.TypeName != "ApiResponse")
            {
                base.Visit(node);
                return;
            }

            var sb = new StringBuilder();

            sb.AppendLine("  export interface ApiResponse<T = any> {");
            sb.AppendLine("    successful: boolean;");
            sb.AppendLine("    message: string;");
            sb.AppendLine("    data: T");
            sb.AppendLine("  }");

            Writer.Write(sb);
        }
Beispiel #10
0
        public override RtInterface GenerateNode(Type element, RtInterface result, TypeResolver resolver)
        {
            var existing = base.GenerateNode(element, result, resolver);

            if (existing == null)
            {
                return(null);
            }

            if (Context.Location.CurrentNamespace == null)
            {
                return(existing);
            }

            ClientAppenderImpl(element, result, resolver);

            return(ReturnExisting() ? existing : null);
        }
Beispiel #11
0
        public override RtInterface GenerateNode(Type element, RtInterface result, TypeResolver resolver)
        {
            Context.CurrentBlueprint.Substitutions.Add(typeof(DateTime), new RtSimpleTypeName("Date"));
            // could add nullables for all simple types but that is really not good enough
            // need a way to provide own resolver but this does not appear possible
            var nullableSubstitutions = new Type[]
            {
                typeof(bool?),
                typeof(char?),
                typeof(byte?),
                typeof(sbyte?),
                typeof(short?),
                typeof(ushort?),
                typeof(int?),
                typeof(uint?),
                typeof(long?),
                typeof(ulong?),
                typeof(float?),
                typeof(double?),
                typeof(decimal?),
                typeof(DateTime?)
            };

            foreach (var nullableSubstitution in nullableSubstitutions)
            {
                Context.CurrentBlueprint.Substitutions.Add(nullableSubstitution, new RtSimpleTypeName($"Nullable<{resolver.ResolveTypeName(nullableSubstitution)}>"));
            }

            // with this method do not know if there has been any substitutions

            if (!ProducedNullableType && Context.Location.CurrentNamespace != null)
            {
                Context.Location.CurrentNamespace.CompilationUnits.Add(new RtRaw("type Nullable<T> = T | null"));
                ProducedNullableType = true;
            }

            return(base.GenerateNode(element, result, resolver));
        }
        protected override void ClientAppenderImpl(Type element, RtInterface result, TypeResolver resolver)
        {
            var options    = SignalRGenerationOptions.All[element];
            var hub        = options.HubPath;
            var typeName   = element.IsInterface && element.Name.StartsWith('I') ? element.Name.Substring(1) : element.Name;
            var clientImpl = new RtClass()
            {
                Name         = new RtSimpleTypeName($"{typeName}Invoker"), //Naming becomes hard with a strongly typed "client" API on the server already
                Export       = true,
                Decorators   = {  },
                Implementees = { result.Name },
                Members      =
                {
                    new RtField
                    {
                        AccessModifier = AccessModifier.Private,
                        Identifier     = new RtIdentifier("hubConnection"),
                        Type           = new RtSimpleTypeName("Promise<HubConnection>")
                    },
                    new RtConstructor
                    {
                        Arguments =   { new RtArgument
                                        {
                                            Type       = new RtSimpleTypeName(options.HubConnectionProviderType),
                                            Identifier = new RtIdentifier("hubConnectionProvider")
                                        } },
                        Body      = new RtRaw($"this.hubConnection = hubConnectionProvider.getHubConnection(\"{hub}\");"),
                        LineAfter = " "
                    }
                },
            };

            clientImpl.Members.AddRange(GetImplementationMembers(result));

            Context.Location.CurrentNamespace.CompilationUnits.Add(clientImpl);
            Context.AddNewLine();
        }
Beispiel #13
0
        private IEnumerable <RtNode> GetImplementationMembers(RtInterface result)
        {
            var members   = new List <RtNode>();
            var functions = result.Members.OfType <RtFuncion>();

            foreach (var function in functions)
            {
                var rtTypeNames = function.Arguments.Select(a => a.Type.ToString()).ToList();
                var generics    = string.Join(",", rtTypeNames);
                if (rtTypeNames.Count > 1)
                {
                    generics = $"[{generics}]";
                }
                var stringType      = rtTypeNames.Count == 0 ? "SignalDispatcher" : $"SimpleEventDispatcher<{generics}>";
                var eventDispatcher = new RtField
                {
                    AccessModifier           = AccessModifier.Public,
                    Identifier               = new RtIdentifier($"on{function.Identifier.ToString().FirstCharToUpper()}"),
                    Type                     = new RtSimpleTypeName(stringType),
                    InitializationExpression = $"new {stringType}()"
                };
                members.Add(eventDispatcher);
            }

            members.Add(new RtConstructor
            {
                Arguments = { new RtArgument
                              {
                                  Type       = new RtSimpleTypeName("HubConnectionProvider"),
                                  Identifier = new RtIdentifier("hubConnectionProvider")
                              } },
                Body = GetEventRegistrationBody(functions)
            });

            return(members);
        }
Beispiel #14
0
 public abstract void Visit(RtInterface node);
Beispiel #15
0
 public abstract T Visit(RtInterface node);
Beispiel #16
0
        private void BuildClientClass(string typeName, SignalRGenerationOptions options, Type element, RtInterface result, TypeResolver resolver)
        {
            RtClass clientImpl = new RtClass
            {
                Name       = new RtSimpleTypeName(typeName),
                Export     = true,
                Decorators = { },
                Members    =
                {
                    new RtConstructor
                    {
                        Arguments =   { new RtArgument
                                        {
                                            Type       = new RtSimpleTypeName(options.HubConnectionProviderType),
                                            Identifier = new RtIdentifier("hubConnectionProvider")
                                        } },
                        Body      = new RtRaw($"this.hubConnection = hubConnectionProvider.getHubConnection(\"{options.HubPath}\");"),
                        LineAfter = " "
                    }
                }
            };

            clientImpl.Members.AddRange(GetImplementationMembers(result, options.HubPath));

            Context.Location.CurrentNamespace.CompilationUnits.Add(clientImpl);
            Context.AddNewLine();
        }
Beispiel #17
0
 protected abstract void ClientAppenderImpl(Type element, RtInterface result, TypeResolver resolver);