Ejemplo n.º 1
0
 public EnumTypeSchemaGreen(TypeSchemaGreen @base, Option <Type> dotNetType,
                            string schemaName, string codeName, string contractName, int baseTypeId, bool isFlags, ImmutableDictionary <string, long> values) : base(@base, dotNetType, false)
 {
     SchemaName   = schemaName;
     CodeName     = codeName;
     ContractName = contractName;
     BaseTypeId   = baseTypeId;
     IsFlags      = isFlags;
     Values       = values;
 }
Ejemplo n.º 2
0
 public ComplexTypeSchemaGreen(TypeSchemaGreen @base, Option <Type> dotNetType, string schemaName,
                               string codeName, string contractName, int?baseTypeId, bool isStruct,
                               ImmutableDictionary <string, int> fields) : base(@base, dotNetType, false)
 {
     SchemaName   = schemaName;
     CodeName     = codeName;
     ContractName = contractName;
     BaseTypeId   = baseTypeId;
     IsStruct     = isStruct;
     Fields       = fields;
 }
Ejemplo n.º 3
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));
        }
Ejemplo n.º 4
0
        private static TypeSchemaGreen JsonToType(JProperty typeProp, IDictionary <string, TypeSchemaGreen> known)
        {
            if (!(typeProp.Value is JObject jobj))
            {
                throw new SchemaFormatException($"Invalid type description as {typeProp.Path}");
            }

            if (!known.TryGetValue(typeProp.Name, out var type))
            {
                type = new RefTypeSchemaGreen();
                known.Add(typeProp.Name, type);
            }
            var codeName = (string)null;

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

            var contract = (string)null;

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

            if (jobj.TryGetValue("fields", StringComparison.InvariantCultureIgnoreCase, out var fieldToken))
            {
                if (!(fieldToken is JObject fields))
                {
                    throw new SchemaFormatException($"Invalid fields description as path {fieldToken.Path}");
                }

                TypeSchemaGreen @base = null;
                if (jobj.TryGetValue("base", StringComparison.InvariantCultureIgnoreCase, out tkn))
                {
                    try
                    {
                        var baseName = tkn.ToObject <string>();
                        if (!known.TryGetValue(baseName, out @base))
                        {
                            @base = new ComplexTypeSchemaGreen(Option.None, baseName, null, null, null, false,
                                                               ImmutableDictionary <string, int> .Empty);
                            known.Add(baseName, @base);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new SchemaFormatException(
                                  $"Invalid token in base type description at path {tkn.Path}", ex);
                    }
                }

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

                var flds = new Dictionary <string, int>();

                foreach (var property in fields.Properties())
                {
                    var fieldName = property.Name;

                    try
                    {
                        var fieldTypeName = property.Value.ToObject <string>();
                        var fieldType     = GetFieldType(fieldTypeName, known);
                        flds.Add(fieldName, fieldType.Id);
                    }
                    catch (Exception ex)
                    {
                        throw new SchemaFormatException(
                                  $"Invalid token in type field description at path {property.Path}", ex);
                    }
                }

                type = new ComplexTypeSchemaGreen(type, Option.None, typeProp.Name, codeName, contract, @base?.Id,
                                                  isStruct,
                                                  ImmutableDictionary.CreateRange(flds));
                known[typeProp.Name] = type;
                return(type);
            }

            if (jobj.TryGetValue("values", StringComparison.InvariantCultureIgnoreCase, out var valuesToken))
            {
                if (!(valuesToken is JObject values))
                {
                    throw new SchemaFormatException($"Invalid values description as path {fieldToken.Path}");
                }

                TypeSchemaGreen @base = WellKnownTypeSchemaGreen.ByType[typeof(int)];
                if (jobj.TryGetValue("base", StringComparison.InvariantCultureIgnoreCase, out tkn))
                {
                    try
                    {
                        var baseName = tkn.ToObject <string>();
                        if (!known.TryGetValue(baseName, out @base))
                        {
                            @base = WellKnownTypeSchemaGreen.ByCode[baseName];
                            known.Add(baseName, @base);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new SchemaFormatException(
                                  $"Invalid token in based on description at path {tkn.Path}", ex);
                    }
                }

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

                var vls = new Dictionary <string, long>();

                foreach (var property in values.Properties())
                {
                    var fieldName = property.Name;

                    try
                    {
                        var fieldValue = property.Value.ToObject <long>();
                        vls.Add(fieldName, fieldValue);
                    }
                    catch (Exception ex)
                    {
                        throw new SchemaFormatException(
                                  $"Invalid token in type field description at path {property.Path}", ex);
                    }
                }

                type = new EnumTypeSchemaGreen(type, Option.None, typeProp.Name, codeName, contract, @base.Id, isFlags,
                                               ImmutableDictionary.CreateRange(vls));
                known[typeProp.Name] = type;
                return(type);
            }

            throw new SchemaFormatException($"Unknown type schema at {typeProp.Path}");
        }
Ejemplo n.º 5
0
 // ReSharper disable once SuggestBaseTypeForParameter
 public ArrayTypeSchemaGreen(TypeSchemaGreen schema, Option <Type> dotNetType, int elementId) : base(schema, dotNetType, true)
 {
     ElementId = elementId;
 }
Ejemplo n.º 6
0
 public NullableTypeSchemaGreen(TypeSchemaGreen @base, Option <Type> dotNetType, int elementTypeId) : base(@base, dotNetType, true)
 {
     ElementTypeId = elementTypeId;
 }