internal Type GetTypeFromSchema()

in lang/csharp/src/apache/main/Reflect/ReflectDefaultReader.cs [82:191]


        internal Type GetTypeFromSchema(Schema schema, bool nullable)
        {
            switch (schema.Tag)
            {
                case Schema.Type.Null:
                    return typeof(object);

                case Schema.Type.Boolean:
                    return nullable ? typeof(bool?) : typeof(bool);

                case Schema.Type.Int:
                    return nullable ? typeof(int?) : typeof(int);

                case Schema.Type.Long:
                    return nullable ? typeof(long?) : typeof(long);

                case Schema.Type.Float:
                    return nullable ? typeof(float?) : typeof(float);

                case Schema.Type.Double:
                    return nullable ? typeof(double?) : typeof(double);

                case Schema.Type.Fixed:
                case Schema.Type.Bytes:
                    return typeof(byte[]);

                case Schema.Type.String:
                    return typeof(string);

                case Schema.Type.Enumeration:
                    var namedSchema = schema as NamedSchema;
                    if (namedSchema == null)
                    {
                        throw new Exception("Unable to cast schema into a named schema");
                    }

                    Type enumType = null;
                    enumType = EnumCache.GetEnumeration(namedSchema);
                    if (enumType == null)
                    {
                        throw new Exception(string.Format(CultureInfo.InvariantCulture,
                            "Couldn't find type matching enum name {0}", namedSchema.Fullname));
                    }

                    if (nullable)
                    {
                        return typeof(Nullable<>).MakeGenericType(new Type[] { enumType });
                    }
                    else
                    {
                        return enumType;
                    }

                case Schema.Type.Record:
                case Schema.Type.Error:
                    var recordSchema = schema as RecordSchema;
                    if (recordSchema == null)
                    {
                        throw new Exception("Unable to cast schema into a named schema");
                    }

                    Type recordtype = null;
                    recordtype = _classCache.GetClass(recordSchema).GetClassType();
                    if (recordtype == null)
                    {
                        throw new Exception(string.Format(CultureInfo.InvariantCulture,
                            "Couldn't find type matching schema name {0}", recordSchema.Fullname));
                    }

                    return recordtype;

                case Schema.Type.Array:
                    var arraySchema = schema as ArraySchema;
                    if (arraySchema == null)
                    {
                        throw new Exception("Unable to cast schema into an array schema");
                    }

                    var arrayHelper = _classCache.GetArrayHelper(arraySchema, null);
                    return arrayHelper.ArrayType.MakeGenericType(new Type[] { GetTypeFromSchema(arraySchema.ItemSchema, false) });

                case Schema.Type.Map:
                    var mapSchema = schema as MapSchema;
                    if (mapSchema == null)
                    {
                        throw new Exception("Unable to cast schema into a map schema");
                    }

                    return MapType.MakeGenericType(new Type[] { typeof(string), GetTypeFromSchema(mapSchema.ValueSchema, false) });

                case Schema.Type.Union:
                    var unionSchema = schema as UnionSchema;
                    if (unionSchema == null)
                    {
                        throw new Exception("Unable to cast schema into a union schema");
                    }

                    Schema nullibleType = CodeGen.GetNullableType(unionSchema);
                    if (nullibleType == null)
                    {
                        return typeof(object);
                    }
                    else
                    {
                        return GetTypeFromSchema(nullibleType, true);
                    }
            }

            throw new Exception("Unable to generate CodeTypeReference for " + schema.Name + " type " + schema.Tag);
        }