public void ThenTheSecondarySingletonServiceDescriptorIsReturned()
        {
            ServiceDeclaration serviceDeclaration = result.Single(x => x.ServiceType == typeof(ISecondSingletonTestInterface));

            Assert.That(serviceDeclaration.DeclaringType, Is.EqualTo(typeof(TestImplementation)));
            Assert.That(serviceDeclaration.Scope, Is.EqualTo(ServiceScope.Singleton));
        }
Esempio n. 2
0
        public ServiceGenInfo(ServiceDeclaration service, OutputOption outputOption, ProtoFile proto)
        {
            Service = service;

            Name      = service.Name.Pascal();
            Namespace = $"{proto.Option.Namespace}.{outputOption.Namespace ?? "Services"}";
        }
Esempio n. 3
0
        public void WithValidParameters_ThenThePropertiesMapVerbatim()
        {
            ServiceDeclaration serviceDeclaration = new ServiceDeclaration(typeof(ITransientInterface), typeof(TestImplementation), ServiceScope.Singleton);

            Assert.AreEqual(typeof(ITransientInterface), serviceDeclaration.ServiceType);
            Assert.AreEqual(typeof(TestImplementation), serviceDeclaration.DeclaringType);
            Assert.AreEqual(ServiceScope.Singleton, serviceDeclaration.Scope);
        }
        public void Setup()
        {
            ServiceCollection subject = new ServiceCollection();

            ServiceDeclaration singletonOneDeclaration = new ServiceDeclaration(typeof(TestImplementation), typeof(TestImplementation), ServiceScope.Singleton);
            ServiceDeclaration singletonTwoDeclaration = new ServiceDeclaration(typeof(TestImplementation), typeof(TestImplementation), ServiceScope.Singleton);

            ServiceExtensions.AddService(subject, singletonOneDeclaration);
            ServiceExtensions.AddService(subject, singletonTwoDeclaration);

            result = subject.BuildServiceProvider();
        }
Esempio n. 5
0
        public void Setup()
        {
            IEnumerable <ServiceDescriptor> serviceDescriptors = new ServiceDescriptor[0];

            services = new Mock <IServiceCollection>();
            services
            .Setup(instance => instance.GetEnumerator())
            .Returns(serviceDescriptors.GetEnumerator());

            ServiceDeclaration declaration = new ServiceDeclaration(typeof(ITransientInterface), typeof(TestImplementation), declartionScope);

            ServiceExtensions.AddService(services.Object, declaration);
        }
Esempio n. 6
0
 public ServiceVariable(Instance instance, Frame creator, ServiceDeclaration declaration = ServiceDeclaration.ImplementationType)
     : base(declaration == ServiceDeclaration.ImplementationType ? instance.ImplementationType : instance.ServiceType, instance.Name.Sanitize(), creator)
 {
     Instance = instance;
 }
Esempio n. 7
0
 public SetterWrappedServiceVariable(Setter setter, Instance instance, Frame creator, ServiceDeclaration declaration = ServiceDeclaration.ImplementationType) : base(instance, creator, declaration)
 {
     _setter = setter;
 }
Esempio n. 8
0
 public void Setup()
 {
     declaration = new ServiceDeclaration(typeof(ITransientInterface), typeof(TestImplementation), ServiceScope.Scoped);
 }
Esempio n. 9
0
 /// <summary>
 /// Registers a service instance with the local Consul agent.
 /// </summary>
 /// <param name="service">Service to register.</param>
 public void RegisterServiceInstance(ServiceDeclaration service)
 {
     //TODO: implement RegisterServiceInstance
     throw new NotImplementedException();
 }
Esempio n. 10
0
        private string GenerateCode(ServiceDeclaration srv, ProtoFile proto)
        {
            var writer = new CodeWriter();

            writer.AppendLine();

            CodeWriter methods = new CodeWriter();

            foreach (var rpc in srv.Rpcs)
            {
                methods.AppendLine();

                var options  = rpc.Option;
                var request  = rpc.RequestType;
                var response = rpc.ResponseType;

                if (!request.IsBuildIn)
                {
                    _types.Add(request.Name.Pascal());
                }

                if (!response.IsBuildIn)
                {
                    _types.Add(response.Name.Pascal());
                }

                string url = $"this._options.baseUrl+`/{srv.Option.Prefix}";
                List <FieldDeclaration> path = null;

                if (options.Template != null)
                {
                    url += $"/{options.Template.Replace("{", "@{")}";

                    path = request is MessageDeclaration ? ((MessageDeclaration)request).GetPathBinding(options.Template) : null;
                }

                url += "`";

                if (rpc.Option.Description != null)
                {
                    methods.Append($"/** {rpc.Option.Description} */").AppendLine();
                }

                methods.Append($"{rpc.Name.Camel()}");
                if (request == PrimitiveType.Void)
                {
                    methods.Append("()");
                }
                else
                {
                    methods.Append($"(request:{request.GetTypeName()})");
                }

                var responseType =
                    response.HasStreams() ? "Response" :
                    response == PrimitiveType.Void ? "ErrorInfo|void" :
                    response.GetTypeName();

                methods.Append($" : Promise<{responseType}>");

                methods.Append(" {").AppendLine();

                var body = methods.Append('\t', 1).Block(rpc.Name);

                var headers = new Dictionary <string, string>();

                if (!response.HasStreams())
                {
                    headers["Accept"] = "application/json";
                }

                if (srv.Option.RequiredAuthorization || options.RequiredAuthorization)
                {
                    headers["Authorization"] = "Bearer";
                }
                if (!request.HasStreams() && (options.Method == "POST" || options.Method == "PUT"))
                {
                    headers["'Content-Type'"] = "application/json";
                }

                var headersString = string.Join(", ", headers.Select(x => $"{x.Key}: '{x.Value}'"));
                body.Append($"let options:RequestInit = {{ method: '{options.Method}', headers: {{ {headersString} }}}};");
                body.AppendLine();

                if (options.Method == "POST" || options.Method == "PUT")
                {
                    if (request.HasStreams())
                    {
                        if (request == PrimitiveType.Stream)
                        {
                            body.Append("options.body = this.getFormData({blob:request});").AppendLine();
                        }
                        else
                        {
                            body.Append("options.body = this.getFormData(request);").AppendLine();
                        }
                    }
                    else
                    {
                        body.Append("options.body = JSON.stringify(request);").AppendLine();
                    }
                }

                if (path != null)
                {
                    body.AppendTemplate($"let endpoint = {url}", path.ToDictionary(x => x.Name, x => (object)$"${{request.{x.Name.Camel()}}}"));
                }
                else
                {
                    body.Append($"let endpoint = {url}");
                }

                if ((options.Method == "GET" || options.Method == "DELETE") && request is MessageDeclaration msg)
                {
                    if (path != null)
                    {
                        var queryFields = msg.Fields.Except(path);
                        if (queryFields.Any())
                        {
                            var queryObj = string.Join(",", queryFields.Select(x => $"{x.Name.Camel()}: request.{x.Name.Camel()}"));
                            body.Append($"+this.getQueryString({{ {queryObj}}})");
                        }
                    }
                    else
                    {
                        body.Append("+this.getQueryString(request)");
                    }
                }

                body.Append(";").AppendLine();

                if (response.HasStreams())
                {
                    body.Append($"return this._fetch(endpoint, options).then((response:Response) => this.getBlob(response));");
                }
                else if (response == PrimitiveType.Void)
                {
                    body.Append($"return this._fetch(endpoint, options).then((response:Response) => this.ensureSuccess(response));");
                }
                else
                {
                    body.Append($"return this._fetch(endpoint, options).then((response:Response) => this.getObject(response));");
                }

                body.AppendLine();

                methods.Append("}");

                methods.AppendLine();
            }

            if (srv.Option.Description != null)
            {
                writer.Append($"/** {srv.Option.Description} */").AppendLine();
            }

            writer.AppendTemplate(serviceTemplate, new Dictionary <string, object>
            {
                ["SERVICE"] = proto.Package.Name.Pascal(),
                ["NAME"]    = srv.Name.Pascal(),
                ["METHODS"] = methods.ToString()
            });

            return(writer.ToString());
        }
        private string GenerateCode(ServiceDeclaration srv)
        {
            var writer = new CodeWriter();

            writer.AppendLine();

            CodeWriter methods = new CodeWriter();

            foreach (var rpc in srv.Rpcs)
            {
                methods.AppendLine();

                var options  = rpc.Option;
                var request  = rpc.RequestType;
                var response = rpc.ResponseType;

                if (!request.IsBuildIn)
                {
                    _types.Add(request.Name.Pascal());
                }

                if (!response.IsBuildIn)
                {
                    _types.Add(response.Name.Pascal());
                }

                string url = srv.Option.Prefix != null ? $"/{srv.Option.Prefix}" : "";
                List <FieldDeclaration> path = null;

                if (options.Template != null)
                {
                    url += $"/{options.Template.Replace("{", "@{")}";

                    path = request is MessageDeclaration ? ((MessageDeclaration)request).GetPathBinding(options.Template) : null;
                }

                if (rpc.Option.Description != null)
                {
                    methods.Append($"/** {rpc.Option.Description} */").AppendLine();
                }

                methods.Append($"{rpc.Name.Camel()}");
                if (request == PrimitiveType.Void)
                {
                    methods.Append("()");
                }
                else
                {
                    methods.Append($"(request: {request.GetTypeName()})");
                }

                var responseType =
                    response.HasStreams() ? "HttpResponse<Blob>" :
                    response == PrimitiveType.Void ? "{}" :
                    response.GetTypeName();

                methods.Append($": Observable<{responseType}>");

                methods.Append(" {").AppendLine();

                var body = methods.Append(' ', 2).Block(rpc.Name);

                body.Append($"return this.http.{options.Method.ToLowerInvariant()}");

                if (responseType != "HttpResponse<Blob>")
                {
                    body.Append($"<{responseType}>(");
                }
                else
                {
                    body.Append($"(");
                }

                if (path != null)
                {
                    body.AppendTemplate($"`{url}", path.ToDictionary(x => x.Name, x => (object)$"${{request.{x.Name.Camel()}}}"));
                }
                else
                {
                    body.Append($"`{url}");
                }

                if ((options.Method == "GET" || options.Method == "DELETE") && request is MessageDeclaration msg)
                {
                    if (path != null)
                    {
                        var queryFields = msg.Fields.Except(path);
                        if (queryFields.Any())
                        {
                            var queryObj = string.Join(", ", queryFields.Select(x => $"{x.Name.Camel()}: request.{x.Name.Camel()}"));
                            body.Append($"${{ getQueryString({{ {queryObj} }}) }}");
                        }
                    }
                    else
                    {
                        body.Append($"${{ getQueryString(request) }}");
                    }
                }

                body.Append("`");

                if (options.Method == "POST" || options.Method == "PUT")
                {
                    body.Append(", ");
                    if (request.HasStreams())
                    {
                        if (request == PrimitiveType.Stream)
                        {
                            body.Append("getFormData({ blob: request })");
                        }
                        else
                        {
                            body.Append("getFormData(request)");
                        }
                    }
                    else
                    {
                        body.Append("request");
                    }
                }

                Dictionary <string, string> headers = new Dictionary <string, string>();
                if (srv.Option.RequiredAuthorization || options.RequiredAuthorization)
                {
                    headers.Add("Authorization", "Bearer");
                }

                if (!response.HasStreams())
                {
                    headers["Accept"] = "application/json";
                }

                if (!request.HasStreams() && (options.Method == "POST" || options.Method == "PUT"))
                {
                    headers["'Content-Type'"] = "application/json";
                }

                if (headers.Any() || responseType == "HttpResponse<Blob>")
                {
                    body.Append(", {").AppendLine();

                    if (headers.Any())
                    {
                        var headerValues = headers.Select(x => $"{x.Key}: '{x.Value}'").Aggregate((x, y) => $"{x}, {y}");
                        body.Append(' ', 4).Append($"headers: new HttpHeaders({{ {headerValues} }}),").AppendLine();
                    }

                    if (responseType == "HttpResponse<Blob>")
                    {
                        body.Append(' ', 4).Append("observe: 'response',").AppendLine()
                        .Append(' ', 4).Append("responseType: 'blob',").AppendLine();
                    }

                    body.Append("}");
                }

                body.Append(");");

                body.AppendLine();

                methods.Append("}");

                methods.AppendLine();
            }

            if (srv.Option.Description != null)
            {
                writer.Append($"/** {srv.Option.Description} */").AppendLine();
            }
            writer.AppendTemplate(serviceTemplate, new Dictionary <string, object>
            {
                ["NAME"]    = srv.Name.Pascal(),
                ["METHODS"] = methods.ToString()
            });

            return(writer.ToString());
        }