Example #1
0
        internal Encoder.WriteItem Resolve(DurationSchema schema)
        {
            return((value, encoder) =>
            {
                if (!(value is TimeSpan))
                {
                    throw new AvroTypeMismatchException($"[Duration] required to write against [TimeSpan] of [fixed] schema but found [{value.GetType()}]");
                }

                byte[] bytes = (byte[])schema.ConvertToBaseValue(value, schema);

                encoder.WriteFixed(bytes);
            });
        }
Example #2
0
        private TypeSchema ParseLogicalType(JObject token, NamedSchema parent, Dictionary <string, NamedSchema> namedSchemas, string logicalType)
        {
            TypeSchema result;

            switch (logicalType)
            {
            case LogicalTypeSchema.LogicalTypeEnum.Uuid:
                result = new UuidSchema();
                break;

            case LogicalTypeSchema.LogicalTypeEnum.Decimal:
                var scale     = token.OptionalProperty <int>(nameof(DecimalSchema.Scale).ToLower());
                var precision = token.RequiredProperty <int>(nameof(DecimalSchema.Precision).ToLower());
                result = new DecimalSchema(typeof(decimal), precision, scale);
                break;

            case LogicalTypeSchema.LogicalTypeEnum.Duration:
                result = new DurationSchema();
                break;

            case LogicalTypeSchema.LogicalTypeEnum.TimestampMilliseconds:
                result = new TimestampMillisecondsSchema();
                break;

            case LogicalTypeSchema.LogicalTypeEnum.TimestampMicroseconds:
                result = new TimestampMicrosecondsSchema();
                break;

            case LogicalTypeSchema.LogicalTypeEnum.TimeMilliseconds:
                result = new TimeMillisecondsSchema();
                break;

            case LogicalTypeSchema.LogicalTypeEnum.TimeMicrosecond:
                result = new TimeMicrosecondsSchema();
                break;

            case LogicalTypeSchema.LogicalTypeEnum.Date:
                result = new DateSchema();
                break;

            default:
                throw new SerializationException(
                          string.Format(CultureInfo.InvariantCulture, "Unknown LogicalType schema :'{0}'.", logicalType));
            }

            return(result);
        }
        private static AvroSchema ParseDurationSchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace)
        {
            var keys = new HashSet <string>()
            {
                "logicalType", "type"
            };

            JsonUtil.AssertKeys(jToken, keys, null, out var tags);

            JsonUtil.AssertValue(jToken, "logicalType", "duration");
            var type           = JsonUtil.GetValue <JToken>(jToken, "type");
            var underlyingType = ParseSchema(type, namedTypes, enclosingNamespace);
            var durationSchema = new DurationSchema(underlyingType);

            durationSchema.AddTags(tags);
            return(durationSchema);
        }