Example #1
0
 public ServiceSchemaGreen(string name, string owner, string codeName, ImmutableDictionary <string, EventSchemaGreen> events,
                           ImmutableDictionary <string, CallSchemaGreen> calls, ImmutableDictionary <int, TypeSchemaGreen> types, ContentType contentType = null,
                           ExchangeSchema exchange = null, ExchangeSchema responseExchange = null)
 {
     if (string.IsNullOrWhiteSpace(name))
     {
         throw new ArgumentException("Value cannot be null or whitespace.", nameof(name));
     }
     if (string.IsNullOrWhiteSpace(owner))
     {
         throw new ArgumentException("Value cannot be null or whitespace.", nameof(owner));
     }
     Name        = name;
     Owner       = owner;
     CodeName    = codeName;
     Events      = events ?? throw new ArgumentNullException(nameof(events));
     Calls       = calls ?? throw new ArgumentNullException(nameof(calls));
     ContentType = contentType;
     if (exchange != null && exchange.Type == ExchangeKind.Fanout)
     {
         throw new SchemaException($"Invalid exchange type on service level in service {Name} - cannot be Fanout");
     }
     Exchange         = exchange;
     ResponseExchange = responseExchange;
     if (responseExchange != null && responseExchange.Type == ExchangeKind.Fanout)
     {
         throw new SchemaException($"Invalid response exchange type on service level in service {Name} - cannot be Fanout");
     }
     if (string.IsNullOrWhiteSpace(codeName))
     {
         throw new ArgumentException("Value cannot be null or whitespace.", nameof(codeName));
     }
     Types = types ?? throw new ArgumentNullException(nameof(types));
 }
Example #2
0
 protected EndpointSchemaGreen(EndpointSchemaGreen @base, string name, string codeName, ContentType contentType = null, string routingKey = null, ExchangeSchema exchange = null) : base(@base)
 {
     Name        = name;
     ContentType = contentType;
     RoutingKey  = routingKey;
     Exchange    = exchange;
     CodeName    = codeName;
 }
Example #3
0
        private static void WriteExchange(ExchangeSchema exchange, StringBuilder builder, bool response)
        {
            var notFirst = false;

            if (!string.IsNullOrWhiteSpace(exchange.Name) && !response ||
                exchange.Name != null && response)
            {
                builder.Append($"Name = \"{exchange.Name}\"");
                notFirst = true;
            }
            if (exchange.Type != ExchangeKind.Direct)
            {
                if (notFirst)
                {
                    builder.Append(", ");
                }
                builder.Append($"Kind = ExchangeKind.{exchange.Type}");
                notFirst = true;
            }
            if (!exchange.Durable)
            {
                if (notFirst)
                {
                    builder.Append(", ");
                }
                builder.Append("Durable = false");
                notFirst = true;
            }
            if (exchange.AutoDelete)
            {
                if (notFirst)
                {
                    builder.Append(", ");
                }
                builder.Append("AutoDelete = true");
                notFirst = true;
            }
            if (exchange.Delayed)
            {
                if (notFirst)
                {
                    builder.Append(", ");
                }
                builder.Append("Delayed = false");
                notFirst = true;
            }

            if (exchange.Alternate != null)
            {
                if (notFirst)
                {
                    builder.Append(", ");
                }
                builder.Append($"Alternate = \"{exchange.Alternate}\"");
            }
        }
Example #4
0
 public CallSchemaGreen(CallSchemaGreen @base, string name, string codeName, int requestTypeId, int?responseTypeId = null,
                        ContentType contentType = null, string routingKey = null, RequestQueueSchema requestQueue = null,
                        ExchangeSchema exchange = null, ExchangeSchema responseExchange = null) : base(name, codeName, contentType,
                                                                                                       routingKey, exchange)
 {
     ResponseExchange = responseExchange;
     RequestQueue     = requestQueue;
     RequestTypeId    = requestTypeId;
     ResponseTypeId   = responseTypeId;
 }
Example #5
0
            private void LinkExchange(Definition[] definitions)
            {
                if (string.IsNullOrEmpty(ExchangeSchema))
                {
                    return;
                }

                var parts = ExchangeSchema.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                Recipes = parts.Select(x => Recipe.FromString(x, definitions, this)).ToArray();
            }
Example #6
0
 public CallSchemaGreen(string name, string codeName, int requestTypeId, int?responseTypeId = null,
                        ContentType contentType = null, string routingKey = null, RequestQueueSchema requestQueue = null,
                        ExchangeSchema exchange = null, ExchangeSchema responseExchange = null) : base(name, codeName, contentType,
                                                                                                       routingKey, exchange)
 {
     if (responseExchange != null && responseExchange.Type == ExchangeKind.Fanout)
     {
         throw new SchemaException($"Cannot set fanount response exchange in endpoint {name}");
     }
     ResponseExchange = responseExchange;
     RequestQueue     = requestQueue;
     RequestTypeId    = requestTypeId;
     ResponseTypeId   = responseTypeId;
 }
Example #7
0
 protected EndpointSchemaGreen(string name, string codeName, ContentType contentType = null, string routingKey = null, ExchangeSchema exchange = null)
 {
     if (string.IsNullOrWhiteSpace(codeName))
     {
         throw new ArgumentException("Value cannot be null or whitespace.", nameof(codeName));
     }
     if (string.IsNullOrWhiteSpace(name))
     {
         throw new ArgumentException("Value cannot be null or whitespace.", nameof(name));
     }
     Name        = name;
     ContentType = contentType;
     if (routingKey != null && string.IsNullOrWhiteSpace(routingKey))
     {
         throw new ArgumentException("Routing key cannot be whitespace.", nameof(routingKey));
     }
     RoutingKey = routingKey;
     Exchange   = exchange;
     CodeName   = codeName;
 }
Example #8
0
 public EventSchemaGreen(string name, string codeName, int typeId, ContentType contentType = null,
                         string routingKey = null, ExchangeSchema exchange = null) : base(name, codeName, contentType, routingKey,
                                                                                          exchange)
 {
     TypeId = typeId;
 }
Example #9
0
        public static ServiceSchema FromJObject(JToken token)
        {
            if (!(token is JObject jobj))
            {
                throw new SchemaFormatException("Invalid service schema");
            }
            string name;

            if (jobj.TryGetValue("name", StringComparison.InvariantCultureIgnoreCase, out var tkn))
            {
                try
                {
                    name = tkn.ToObject <string>();
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in service name at path {tkn.Path}", ex);
                }
            }
            else
            {
                throw new SchemaFormatException("No service name specified");
            }

            string owner;

            if (jobj.TryGetValue("owner", StringComparison.InvariantCultureIgnoreCase, out tkn))
            {
                try
                {
                    owner = tkn.ToObject <string>();
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in service owner at path {tkn.Path}", ex);
                }
            }
            else
            {
                throw new SchemaFormatException("No service owner specified");
            }

            var codeName = (string)null;

            if (jobj.TryGetValue("title", StringComparison.InvariantCultureIgnoreCase, out tkn))
            {
                try
                {
                    codeName = tkn.ToObject <string>();
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in codeName description at path {tkn.Path}", ex);
                }
            }

            ContentType contentType = null;

            if (jobj.TryGetValue("contentType", StringComparison.InvariantCultureIgnoreCase, out tkn))
            {
                try
                {
                    contentType = new ContentType(tkn.ToObject <string>());
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in content type description at path {tkn.Path}", ex);
                }
            }

            ExchangeSchema exchange = null;

            if (jobj.TryGetValue("exchange", out tkn))
            {
                exchange = JsonToExchange(tkn);
            }

            ExchangeSchema responseExchange = null;

            if (jobj.TryGetValue("responseExchange", out tkn))
            {
                responseExchange = JsonToExchange(tkn);
            }


            var known  = new Dictionary <string, TypeSchemaGreen>();
            var events = new List <EventSchemaGreen>();

            if (jobj.TryGetValue("events", out tkn))
            {
                if (tkn is JObject obj)
                {
                    foreach (var property in obj.Properties())
                    {
                        events.Add(JsonToEvent(property, known));
                    }
                }
            }
            var calls = new List <CallSchemaGreen>();

            if (jobj.TryGetValue("calls", out tkn))
            {
                if (tkn is JObject obj)
                {
                    foreach (var property in obj.Properties())
                    {
                        calls.Add(JsonToCall(property, known));
                    }
                }
            }

            if (jobj.TryGetValue("enums", out tkn))
            {
                if (tkn is JObject obj)
                {
                    foreach (var property in obj.Properties())
                    {
                        JsonToType(property, known);
                    }
                }
            }

            if (jobj.TryGetValue("types", out tkn))
            {
                if (tkn is JObject obj)
                {
                    foreach (var property in obj.Properties())
                    {
                        JsonToType(property, known);
                    }
                }
            }

            return(new ServiceSchema(new ServiceSchemaGreen(name, owner, codeName,
                                                            ImmutableDictionary.CreateRange(events.Select(p => new KeyValuePair <string, EventSchemaGreen>(p.Name, p))),
                                                            ImmutableDictionary.CreateRange(calls.Select(p => new KeyValuePair <string, CallSchemaGreen>(p.Name, p))),
                                                            ImmutableDictionary.CreateRange(known.Select(p => new KeyValuePair <int, TypeSchemaGreen>(p.Value.Id, p.Value))),
                                                            contentType, exchange, responseExchange)));
        }
Example #10
0
        private static CallSchemaGreen JsonToCall(JProperty evProp, IDictionary <string, TypeSchemaGreen> known)
        {
            if (!(evProp.Value is JObject jobj))
            {
                throw new SchemaFormatException($"Invalid call description at path {evProp.Path}");
            }
            TypeSchemaGreen requestType;

            if (jobj.TryGetValue("request", out var tkn))
            {
                try
                {
                    requestType = GetFieldType(tkn.ToObject <string>(), known);
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException($"Invalid call request type found at path {tkn.Path}", ex);
                }
            }
            else
            {
                throw new SchemaFormatException($"No call request type found at path {evProp.Path}");
            }

            TypeSchemaGreen responseType = null;

            if (jobj.TryGetValue("response", out tkn))
            {
                try
                {
                    responseType = GetFieldType(tkn.ToObject <string>(), known);
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException($"Invalid call request type found at path {tkn.Path}", ex);
                }
            }



            var codeName = (string)null;

            if (jobj.TryGetValue("title", StringComparison.InvariantCultureIgnoreCase, out tkn))
            {
                try
                {
                    codeName = tkn.ToObject <string>();
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in codeName description at path {tkn.Path}", ex);
                }
            }
            ExchangeSchema exchange = null;

            if (jobj.TryGetValue("exchange", out tkn))
            {
                exchange = JsonToExchange(tkn);
            }

            ExchangeSchema responseExchange = null;

            if (jobj.TryGetValue("responseExchange", out tkn))
            {
                responseExchange = JsonToExchange(tkn);
            }

            var routingKey = (string)null;

            if (jobj.TryGetValue("routingKey", StringComparison.InvariantCultureIgnoreCase, out tkn))
            {
                try
                {
                    routingKey = tkn.ToObject <string>();
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in routing description at path {tkn.Path}", ex);
                }
            }



            RequestQueueSchema queue = null;

            if (jobj.TryGetValue("queue", out tkn))
            {
                queue = JsonToRequestQueue(tkn);
            }


            ContentType contentType = null;

            if (jobj.TryGetValue("contentType", StringComparison.InvariantCultureIgnoreCase, out tkn))
            {
                try
                {
                    contentType = new ContentType(tkn.ToObject <string>());
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in content type description at path {tkn.Path}", ex);
                }
            }

            return(new CallSchemaGreen(evProp.Name, codeName, requestType.Id,
                                       responseType?.Id, contentType, routingKey, queue, exchange, responseExchange));
        }
Example #11
0
        private static EventSchemaGreen JsonToEvent(JProperty evProp, IDictionary <string, TypeSchemaGreen> known)
        {
            if (!(evProp.Value is JObject jobj))
            {
                throw new SchemaFormatException($"Invalid event description at path {evProp.Path}");
            }
            TypeSchemaGreen contract;

            if (jobj.TryGetValue("contract", out var tkn))
            {
                try
                {
                    contract = GetFieldType(tkn.ToObject <string>(), known);
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException($"Invalid event contract found at path {tkn.Path}", ex);
                }
            }
            else
            {
                throw new SchemaFormatException($"No event contract found at path {evProp.Path}");
            }

            var codeName = (string)null;

            if (jobj.TryGetValue("title", StringComparison.InvariantCultureIgnoreCase, out tkn))
            {
                try
                {
                    codeName = tkn.ToObject <string>();
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in codeName description at path {tkn.Path}", ex);
                }
            }
            ExchangeSchema exchange = null;

            if (jobj.TryGetValue("exchange", out tkn))
            {
                exchange = JsonToExchange(tkn);
            }
            var routingKey = (string)null;

            if (jobj.TryGetValue("routingKey", StringComparison.InvariantCultureIgnoreCase, out tkn))
            {
                try
                {
                    routingKey = tkn.ToObject <string>();
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in routing description at path {tkn.Path}", ex);
                }
            }

            ContentType contentType = null;

            if (jobj.TryGetValue("contentType", StringComparison.InvariantCultureIgnoreCase, out tkn))
            {
                try
                {
                    contentType = new ContentType(tkn.ToObject <string>());
                }
                catch (Exception ex)
                {
                    throw new SchemaFormatException(
                              $"Invalid token in content type description at path {tkn.Path}", ex);
                }
            }

            return(new EventSchemaGreen(evProp.Name, codeName, contract.Id, contentType, routingKey, exchange));
        }