internal Encoder.WriteItem Resolve(DecimalSchema schema)
        {
            return((value, encoder) =>
            {
                if (!(schema.BaseTypeSchema is BytesSchema))
                {
                    throw new AvroTypeMismatchException($"[Decimal] required to write against [decimal] of [Bytes] schema but found [{schema.BaseTypeSchema}]");
                }

                var bytesValue = (byte[])schema.ConvertToBaseValue(value, schema);
                encoder.WriteBytes(bytesValue);
            });
        }
Exemple #2
0
            /// <summary>
            /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
            /// </summary>
            /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
            /// <returns>
            ///     <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
            /// </returns>
            public override bool Equals(object obj)
            {
                if (ReferenceEquals(this, obj))
                {
                    return(true);
                }
                DecimalSchema other = obj as DecimalSchema;

                if (other == null)
                {
                    return(false);
                }
                return(SchemasEqual(other) && MaxValue == other.MaxValue && MinValue == other.MinValue);
            }
Exemple #3
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);
        }
Exemple #4
0
        private static void WriteDecimal(TextWriter writer, DecimalSchema schema, WriterMode mode, string parentNamespace, ISet <string> namedSchemas, bool stripNs)
        {
            switch (mode)
            {
            case WriterMode.Canonical:
                writer.Write(@"{""type"":");
                Write(writer, schema.Type, mode, parentNamespace, namedSchemas, stripNs);
                writer.Write(@",""logicalType"":""");
                writer.Write(schema.LogicalType);
                writer.Write(@"""");
                writer.Write(@",""precision"":");
                writer.Write(schema.Precision);
                writer.Write(@",""scale"":");
                writer.Write(schema.Scale);
                writer.Write(@"}");
                break;

            case WriterMode.Full:
                writer.Write(@"{ ""type"": ");
                Write(writer, schema.Type, mode, parentNamespace, namedSchemas, stripNs);
                writer.Write(@", ""logicalType"": """);
                writer.Write(schema.LogicalType);
                writer.Write(@"""");
                writer.Write(@", ""precision"": ");
                writer.Write(schema.Precision);
                writer.Write(@", ""scale"": ");
                writer.Write(schema.Scale);
                writer.Write(@" }");
                break;

            default:
                writer.Write(@"{ ""type"": ");
                Write(writer, schema.Type, mode, parentNamespace, namedSchemas, stripNs);
                writer.Write(@", ""logicalType"": """);
                writer.Write(schema.LogicalType);
                writer.Write(@"""");
                writer.Write(@", ""precision"": ");
                writer.Write(schema.Precision);
                writer.Write(@", ""scale"": ");
                writer.Write(schema.Scale);
                writer.Write(@" }");
                break;
            }
        }
        private static AvroSchema ParseDecimalSchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace)
        {
            var keys = new HashSet <string>()
            {
                "logicalType", "type", "precision", "scale"
            };

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

            JsonUtil.AssertValue(jToken, "logicalType", "decimal");
            var type           = JsonUtil.GetValue <JToken>(jToken, "type");
            var precision      = JsonUtil.GetValue <int>(jToken, "precision");
            var scale          = JsonUtil.GetValue <int>(jToken, "scale");
            var underlyingType = ParseSchema(type, namedTypes, enclosingNamespace);
            var decimalSchema  = new DecimalSchema(underlyingType, precision, scale);

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