public void TestAddSchema() { var codeGen = new CodeGen(); var intSchema = new IntSchema(); codeGen.AddSchema(intSchema); Assert.AreEqual(0, codeGen.Count); var enumSchema = new EnumSchema("TestEnum", "Test.Namespace", new string[] { "A", "B", "C" }); codeGen.AddSchema(enumSchema); Assert.AreEqual(1, codeGen.Count); var fixedSchema = new FixedSchema("TestFixed", "Test.Namespace", 12); codeGen.AddSchema(fixedSchema); Assert.AreEqual(2, codeGen.Count); var recordSchema = new RecordSchema("TestRecord", "Test.Namespace", new RecordSchema.Field[] { new RecordSchema.Field("A", new IntSchema()) }); codeGen.AddSchema(recordSchema); Assert.AreEqual(3, codeGen.Count); var errorSchema = new RecordSchema("TestError", "Test.Namespace", new RecordSchema.Field[] { new RecordSchema.Field("B", enumSchema) }); codeGen.AddSchema(errorSchema); Assert.AreEqual(4, codeGen.Count); var unionSchema = new UnionSchema(fixedSchema, new BytesSchema(), new EnumSchema("TestEnum2", "Test.Namespace", new string[] { "D", "E", "F" })); codeGen.AddSchema(unionSchema); Assert.AreEqual(5, codeGen.Count); }
public void InvalidGuidLength() { var schema = new FixedSchema("test", 8); Assert.Throws <AggregateException>(() => DeserializerBuilder.BuildDeserializer <Guid>(schema)); Assert.Throws <AggregateException>(() => SerializerBuilder.BuildSerializer <Guid>(schema)); }
/// <summary> /// Reads a schema from a JSON token. /// </summary> /// <param name="token"> /// The token to parse. /// </param> /// <param name="cache"> /// An optional schema cache. The cache is populated as the schema is read and can be used /// to provide schemas for certain names or cache keys. /// </param> /// <param name="scope"> /// The surrounding namespace, if any. /// </param> public override Schema Read(JToken token, ConcurrentDictionary <string, Schema> cache, string scope) { if (!(token is JObject @object && IsMatch(@object))) { throw new ArgumentException("The duration case can only be applied to \"fixed\" schemas with a \"duration\" logical type."); } var schema = new FixedSchema(GetQualifiedName(@object, scope), GetSize(@object)) { Aliases = GetQualifiedAliases(@object, scope) ?? new string[0], LogicalType = new DurationLogicalType() }; if (!cache.TryAdd(schema.FullName, schema)) { throw new InvalidDataException($"Invalid duration name; a definition for {schema.FullName} was already read."); } foreach (var alias in schema.Aliases) { if (!cache.TryAdd(alias, schema)) { throw new InvalidDataException($"Invalid duration alias; a definition for {alias} was already read."); } } return(schema); }
/// <summary> /// Reads a schema from a JSON token. /// </summary> /// <param name="element"> /// The element to parse. /// </param> /// <param name="cache"> /// An optional schema cache. The cache is populated as the schema is read and can be used /// to provide schemas for certain names or cache keys. /// </param> /// <param name="scope"> /// The surrounding namespace, if any. /// </param> public override Schema Read(JsonElement element, ConcurrentDictionary <string, Schema> cache, string scope) { if (GetType(element) != JsonSchemaToken.Fixed) { throw new UnknownSchemaException("The fixed case can only be applied to valid fixed schema representations."); } var schema = new FixedSchema(GetQualifiedName(element, scope), GetSize(element)) { Aliases = GetQualifiedAliases(element, scope) ?? new string[0] }; if (!cache.TryAdd(schema.FullName, schema)) { throw new InvalidDataException($"Invalid fixed name; a definition for {schema.FullName} was already read."); } foreach (var alias in schema.Aliases) { if (!cache.TryAdd(alias, schema)) { throw new InvalidDataException($"Invalid fixed alias; a definition for {alias} was already read."); } } return(schema); }
/// <summary> /// Reads a schema from a JSON token. /// </summary> /// <param name="token"> /// The token to parse. /// </param> /// <param name="cache"> /// An optional schema cache. The cache is populated as the schema is read and can be used /// to provide schemas for certain names or cache keys. /// </param> /// <param name="scope"> /// The surrounding namespace, if any. /// </param> public override Schema Read(JToken token, ConcurrentDictionary <string, Schema> cache, string scope) { if (!(token is JObject @object && (string)token[JsonAttributeToken.Type] == JsonSchemaToken.Fixed)) { throw new ArgumentException("The fixed case can only be applied to valid fixed schema representations."); } var schema = new FixedSchema(GetQualifiedName(@object, scope), GetSize(@object)) { Aliases = GetQualifiedAliases(@object, scope) ?? new string[0] }; if (!cache.TryAdd(schema.FullName, schema)) { throw new InvalidDataException($"Invalid fixed name; a definition for {schema.FullName} was already read."); } foreach (var alias in schema.Aliases) { if (!cache.TryAdd(alias, schema)) { throw new InvalidDataException($"Invalid fixed alias; a definition for {alias} was already read."); } } return(schema); }
/// <summary> /// Reads a schema from a JSON token. /// </summary> /// <param name="element"> /// The element to parse. /// </param> /// <param name="cache"> /// An optional schema cache. The cache is populated as the schema is read and can be used /// to provide schemas for certain names or cache keys. /// </param> /// <param name="scope"> /// The surrounding namespace, if any. /// </param> public override Schema Read(JsonElement element, ConcurrentDictionary <string, Schema> cache, string scope) { if (GetType(element) != JsonSchemaToken.Fixed || GetLogicalType(element) != JsonSchemaToken.Duration) { throw new UnknownSchemaException("The duration case can only be applied to \"fixed\" schemas with a \"duration\" logical type."); } var schema = new FixedSchema(GetQualifiedName(element, scope), GetSize(element)) { Aliases = GetQualifiedAliases(element, scope) ?? new string[0], LogicalType = new DurationLogicalType() }; if (!cache.TryAdd(schema.FullName, schema)) { throw new InvalidDataException($"Invalid duration name; a definition for {schema.FullName} was already read."); } foreach (var alias in schema.Aliases) { if (!cache.TryAdd(alias, schema)) { throw new InvalidDataException($"Invalid duration alias; a definition for {alias} was already read."); } } return(schema); }
protected virtual object ResolveFixed(FixedSchema readerSchema, IReader d) { AvroFixed ru = new AvroFixed(readerSchema); byte[] bb = ((AvroFixed)ru).Value; d.ReadFixed(bb); return(ru.Value); }
private JToken EncodeFixed(FixedSchema schema, GenericFixed value) { var jvalue = new JObject(); jvalue.Add("type", new JValue("fixed")); jvalue.Add("data", new JValue(value.Value)); return(jvalue); }
public void NullableGuidValues(Guid value) { var schema = new FixedSchema("test", 16); var deserializer = DeserializerBuilder.BuildDeserializer <Guid?>(schema); var serializer = SerializerBuilder.BuildSerializer <Guid>(schema); Assert.Equal(value, deserializer.Deserialize(serializer.Serialize(value))); }
public void SetSize() { var schema = new FixedSchema("test", 0); Assert.Equal(0, schema.Size); schema.Size = 4; Assert.Equal(4, schema.Size); }
public void ByteArrayValues(byte[] value) { var schema = new FixedSchema("test", value.Length); var deserializer = DeserializerBuilder.BuildDeserializer <byte[]>(schema); var serializer = SerializerBuilder.BuildSerializer <byte[]>(schema); Assert.Equal(value, deserializer.Deserialize(serializer.Serialize(value))); }
/// <summary> /// Creates a class declaration for fixed schema /// </summary> /// <param name="schema">fixed schema</param> /// <param name="ns">namespace object</param> protected virtual void processFixed(Schema schema) { FixedSchema fixedSchema = schema as FixedSchema; if (null == fixedSchema) { throw new CodeGenException("Unable to cast schema into a fixed"); } CodeTypeDeclaration ctd = new CodeTypeDeclaration(); ctd.Name = CodeGenUtil.Instance.Mangle(fixedSchema.Name); ctd.IsClass = true; ctd.IsPartial = true; ctd.Attributes = MemberAttributes.Public; ctd.BaseTypes.Add("SpecificFixed"); // create static schema field createSchemaField(schema, ctd, true); // Add Size field string sizefname = "fixedSize"; var ctrfield = new CodeTypeReference(typeof(uint)); var codeField = new CodeMemberField(ctrfield, sizefname); codeField.Attributes = MemberAttributes.Private | MemberAttributes.Static; codeField.InitExpression = new CodePrimitiveExpression(fixedSchema.Size); ctd.Members.Add(codeField); // Add Size property var fieldRef = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), sizefname); var property = new CodeMemberProperty(); property.Attributes = MemberAttributes.Public | MemberAttributes.Static; property.Name = "FixedSize"; property.Type = ctrfield; property.GetStatements.Add(new CodeMethodReturnStatement(new CodeTypeReferenceExpression(schema.Name + "." + sizefname))); ctd.Members.Add(property); // create constructor to initiate base class SpecificFixed CodeConstructor cc = new CodeConstructor(); cc.Attributes = MemberAttributes.Public; cc.BaseConstructorArgs.Add(new CodeVariableReferenceExpression(sizefname)); ctd.Members.Add(cc); string nspace = fixedSchema.Namespace; if (string.IsNullOrEmpty(nspace)) { throw new CodeGenException("Namespace required for enum schema " + fixedSchema.Name); } CodeNamespace codens = addNamespace(nspace); codens.Types.Add(ctd); }
private static JObject EncodeFixed(FixedSchema schema) { var jobject = new JObject(); jobject.Add(new JProperty("type", "fixed")); jobject.Add(new JProperty("name", schema.Name)); jobject.Add(new JProperty("size", schema.Size)); return(jobject); }
/// <inheritdoc/> protected override void WriteFixed(FixedSchema es, object value, Encoder encoder) { if (value == null || !(value is GenericFixed) || !(value as GenericFixed).Schema.Equals(es)) { throw TypeMismatch(value, "fixed", "GenericFixed"); } GenericFixed ba = (GenericFixed)value; encoder.WriteFixed(ba.Value); }
public GenericFixed(FixedSchema schema, byte[] value) { if (value.Length != schema.Size) { throw new ArgumentException($"Array size mismatch, schema: {schema.Size}, value: {value.Length}"); } Schema = schema; Size = schema.Size; _value = value; }
/// <inheritdoc/> protected override void WriteFixed(FixedSchema schema, object value, Encoder encoder) { var fixedrec = value as SpecificFixed; if (fixedrec == null) { throw new AvroTypeException("Fixed object is not derived from SpecificFixed"); } encoder.WriteFixed(fixedrec.Value); }
private ReadItem ResolveFixed(FixedSchema writerSchema, FixedSchema readerSchema) { if (readerSchema.Size != writerSchema.Size) { throw new AvroException("Size mismatch between reader and writer fixed schemas. Writer: " + writerSchema + ", reader: " + readerSchema); } var fixedAccess = GetFixedAccess(readerSchema); return((r, d) => ReadFixed(r, d, fixedAccess)); }
public void NegativeTimeSpanValues() { var schema = new FixedSchema("duration", DurationLogicalType.DurationSize) { LogicalType = new DurationLogicalType() }; var serializer = SerializerBuilder.BuildSerializer <TimeSpan>(schema); Assert.Throws <OverflowException>(() => serializer.Serialize(TimeSpan.FromMilliseconds(-1))); }
private void VisitCore(FixedSchema s) { if (s.RuntimeType == typeof(Guid)) { s.Serializer = new GuidSerializer(s); } else { s.Serializer = new FixedSerializer(s); } }
public void TestFixed(string s, int size) { Schema sc = Schema.Parse(s); Assert.AreEqual(Schema.Type.Fixed, sc.Tag); FixedSchema fs = sc as FixedSchema; Assert.AreEqual(size, fs.Size); testEquality(s, sc); testToString(sc); }
internal Encoder.WriteItem Resolve(FixedSchema es) { return((value, encoder) => { if (!(value is Fixed) || !((AvroFixed)value).Schema.Equals(es)) { throw new AvroTypeMismatchException("[GenericFixed] required to write against [Fixed] schema but found " + value.GetType()); } AvroFixed ba = (AvroFixed)value; encoder.WriteFixed(ba.Value); }); }
public void NegativeTimeSpanValues() { var schema = new FixedSchema("duration", DurationLogicalType.DurationSize) { LogicalType = new DurationLogicalType(), }; var serialize = serializerBuilder.BuildDelegate <TimeSpan>(schema); using (stream) { Assert.Throws <OverflowException>(() => serialize(TimeSpan.FromMilliseconds(-1), new Utf8JsonWriter(stream))); } }
private JToken EncodeFixed(FixedSchema schema) { if (_schemaNameSet.Add(schema.Name)) { var jobject = new JObject(); jobject.Add(new JProperty("type", "fixed")); jobject.Add(new JProperty("name", schema.Name)); jobject.Add(new JProperty("size", schema.Size)); return(jobject); } return(new JValue(schema.Name)); }
public void NullableTimeSpanValues(TimeSpan value, byte[] encoding) { var schema = new FixedSchema("duration", DurationLogicalType.DurationSize) { LogicalType = new DurationLogicalType() }; var serializer = SerializerBuilder.BuildSerializer <TimeSpan>(schema); Assert.Equal(encoding, serializer.Serialize(value)); var deserializer = DeserializerBuilder.BuildDeserializer <TimeSpan?>(schema); Assert.Equal(value, deserializer.Deserialize(encoding)); }
/// <summary> /// Deserializes a fixed object and returns the object. The default implementation uses CreateFixed() /// and GetFixedBuffer() and returns what CreateFixed() returned. /// </summary> /// <param name="reuse">If appropriate, uses this object instead of creating a new one.</param> /// <param name="writerSchema">The FixedSchema the writer used during serialization.</param> /// <param name="readerSchema">The schema that the readr uses. Must be a FixedSchema with the same /// size as the writerSchema.</param> /// <param name="d">The decoder for deserialization.</param> /// <returns>The deserilized object.</returns> protected override object ReadFixed(object reuse, FixedSchema writerSchema, Schema readerSchema, Decoder d) { FixedSchema rs = readerSchema as FixedSchema; if (rs.Size != writerSchema.Size) { throw new AvroException("Size mismatch between reader and writer fixed schemas. Writer: " + writerSchema + ", reader: " + readerSchema); } SpecificFixed fixedrec = (reuse != null ? reuse : ObjectCreator.Instance.New(rs.Fullname, Schema.Type.Fixed)) as SpecificFixed; d.ReadFixed(fixedrec.Value); return(fixedrec); }
protected virtual object ResolveFixed(FixedSchema writerSchema, TypeSchema readerSchema, IReader d, Type type) { FixedSchema rs = (FixedSchema)readerSchema; AvroFixed ru = new AvroFixed(rs); byte[] bb = ((AvroFixed)ru).Value; d.ReadFixed(bb); if (type == typeof(Guid)) { return(new Guid(ru.Value)); } return(ru.Value); }
public void DynamicByteArrayValues(byte[] value) { var schema = new FixedSchema("test", value.Length); var deserialize = deserializerBuilder.BuildDelegate <dynamic>(schema); var serialize = serializerBuilder.BuildDelegate <dynamic>(schema); using (stream) { serialize(value, new Utf8JsonWriter(stream)); } var reader = new Utf8JsonReader(stream.ToArray()); Assert.Equal(value, deserialize(ref reader)); }
/// <summary> /// Deserializes a fixed object and returns the object. The default implementation uses CreateFixed() /// and GetFixedBuffer() and returns what CreateFixed() returned. /// </summary> /// <param name="reuse">If appropriate, uses this object instead of creating a new one.</param> /// <param name="writerSchema">The FixedSchema the writer used during serialization.</param> /// <param name="readerSchema">The schema that the readr uses. Must be a FixedSchema with the same /// size as the writerSchema.</param> /// <param name="d">The decoder for deserialization.</param> /// <returns>The deserilized object.</returns> protected virtual object ReadFixed(object reuse, FixedSchema writerSchema, Schema readerSchema, Decoder d) { FixedSchema rs = (FixedSchema)readerSchema; if (rs.Size != writerSchema.Size) { throw new AvroException("Size mismatch between reader and writer fixed schemas. Writer: " + writerSchema + ", reader: " + readerSchema); } object ru = CreateFixed(reuse, rs); byte[] bb = GetFixedBuffer(ru); d.ReadFixed(bb); return(ru); }
protected virtual object ReadFixed(FixedSchema writerSchema, Schema readerSchema, IReader d) { FixedSchema rs = (FixedSchema)readerSchema; if (rs.Size != writerSchema.Size) { throw new AvroException("Size mismatch between reader and writer fixed schemas. Encoder: " + writerSchema + ", reader: " + readerSchema); } object ru = new Fixed(rs); byte[] bb = ((Fixed)ru).Value; d.ReadFixed(bb); return(ru); }
public void NullableGuidValues(Guid value) { var schema = new FixedSchema("test", 16); var deserialize = deserializerBuilder.BuildDelegate <Guid?>(schema); var serialize = serializerBuilder.BuildDelegate <Guid>(schema); using (stream) { serialize(value, new Utf8JsonWriter(stream)); } var reader = new Utf8JsonReader(stream.ToArray()); Assert.Equal(value, deserialize(ref reader)); }
public GenericFixed(FixedSchema schema) { value = new byte[schema.Size]; this.Schema = schema; }
public GenericFixed(FixedSchema schema, byte[] value) { this.value = new byte[schema.Size]; this.Schema = schema; Value = value; }