private ReadItem ResolveEnum(EnumSchema writerSchema, EnumSchema readerSchema)
        {
            var enumAccess = GetEnumAccess(readerSchema);

            if (readerSchema.Equals(writerSchema))
            {
                return((r, d) => enumAccess.CreateEnum(r, d.ReadEnum()));
            }

            var translator = new int[writerSchema.Symbols.Count];

            foreach (var symbol in writerSchema.Symbols)
            {
                var writerOrdinal = writerSchema.Ordinal(symbol);
                if (readerSchema.Contains(symbol))
                {
                    translator[writerOrdinal] = readerSchema.Ordinal(symbol);
                }
                else
                {
                    translator[writerOrdinal] = -1;
                }
            }

            return((r, d) =>
            {
                var writerOrdinal = d.ReadEnum();
                var readerOrdinal = translator[writerOrdinal];
                if (readerOrdinal == -1)
                {
                    throw new AvroException("No such symbol: " + writerSchema[writerOrdinal]);
                }
                return enumAccess.CreateEnum(r, readerOrdinal);
            });
        }
Ejemplo n.º 2
0
        protected override WriteItem ResolveEnum(EnumSchema es)
        {
            List <String> enumNames = new List <string>();

            foreach (var field in typeof(EnumSchema).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static))
            {
                if (field.IsLiteral)
                {
                    enumNames.Add(field.Name);
                }
            }
            int length     = enumNames.ToArray().Length;
            var translator = new int[length];

            for (int i = 0; i < length; i++)
            {
                if (es.Contains(enumNames[i]))
                {
                    translator[i] = es.Ordinal(enumNames[i]);
                }
                else
                {
                    translator[i] = -1;
                }
            }

            return((v, e) =>
            {
                if (v == null)
                {
                    throw new BaijiTypeException("value is null in SpecificDatumWriter.WriteEnum");
                }
                e.WriteEnum(es.Ordinal(v.ToString()));
            });
        }
Ejemplo n.º 3
0
        internal Encoder.WriteItem Resolve(EnumSchema schema)
        {
            return((value, e) =>
            {
                if (!schema.Contains(value.ToString()))
                {
                    throw new AvroTypeException(
                        $"[Enum] Provided value is not of the enum [{schema.Name}] members");
                }

                e.WriteEnum(schema.Ordinal(value.ToString()));
            });
        }
Ejemplo n.º 4
0
        /// <inheritdoc/>
        protected override WriteItem ResolveEnum(EnumSchema es)
        {
            var type = ObjectCreator.Instance.GetType(es);

            var enumNames  = Enum.GetNames(type);
            var translator = new int[enumNames.Length];

            for (int i = 0; i < enumNames.Length; i++)
            {
                if (es.Contains(enumNames[i]))
                {
                    translator[i] = es.Ordinal(enumNames[i]);
                }
                else
                {
                    translator[i] = -1;
                }
            }

            return((v, e) =>
            {
                if (v == null)
                {
                    throw new AvroTypeException("value is null in SpecificDefaultWriter.WriteEnum");
                }
                if (v.GetType() == type)
                {
                    int translated = translator[(int)v];
                    if (translated == -1)
                    {
                        throw new AvroTypeException("Unknown enum value:" + v.ToString());
                    }
                    else
                    {
                        e.WriteEnum(translated);
                    }
                }
                else
                {
                    e.WriteEnum(es.Ordinal(v.ToString()));
                }
            });
        }