protected object ResolveDictionary(RecordSchema writerSchema, RecordSchema readerSchema, IReader d, Type type) { var containingTypes = type.GetGenericArguments(); dynamic resultDictionary = Activator.CreateInstance(type); for (int n = (int)d.ReadArrayStart(); n != 0; n = (int)d.ReadArrayNext()) { for (int j = 0; j < n; j++) { dynamic key = Resolve(writerSchema.GetField("Key").Schema, readerSchema.GetField("Key").Schema, d, containingTypes[0]); dynamic value = Resolve(writerSchema.GetField("Value").Schema, readerSchema.GetField("Value").Schema, d, containingTypes[1]); resultDictionary.Add(key, value); } } return(resultDictionary); }
protected virtual object ResolveRecord(RecordSchema writerSchema, RecordSchema readerSchema, IReader dec, Type type) { object result = FormatterServices.GetUninitializedObject(type); foreach (Field wf in writerSchema) { if (readerSchema.Contains(wf.Name)) { Field rf = readerSchema.GetField(wf.Name); string name = rf.aliases?[0] ?? wf.Name; PropertyInfo propertyInfo = type.GetProperty(name); if (propertyInfo != null) { object value = Resolve(wf.Schema, rf.Schema, dec, propertyInfo.PropertyType) ?? wf.DefaultValue?.ToObject(typeof(object)); propertyInfo.SetValue(result, value, null); } FieldInfo fieldInfo = type.GetField(name); if (fieldInfo != null) { object value = Resolve(wf.Schema, rf.Schema, dec, fieldInfo.FieldType) ?? wf.DefaultValue?.ToObject(typeof(object)); fieldInfo.SetValue(result, value); } } else { _skipper.Skip(wf.Schema, dec); } } return(result); }
protected virtual object ResolveRecord(RecordSchema writerSchema, RecordSchema readerSchema, IReader dec, Type type) { object result = FormatterServices.GetUninitializedObject(type); var typeHash = type.GetHashCode(); var typeMembers = new Dictionary <string, MemberInfo>(StringComparer.InvariantCultureIgnoreCase); if (!cachedRecordMembers.ContainsKey(typeHash)) { foreach (var propertyInfo in type.GetProperties()) { typeMembers.Add(propertyInfo.Name, propertyInfo); } foreach (var fieldInfo in type.GetFields()) { typeMembers.Add(fieldInfo.Name, fieldInfo); } cachedRecordMembers.Add(typeHash, typeMembers); } else { typeMembers = cachedRecordMembers[typeHash]; } foreach (Field wf in writerSchema) { if (readerSchema.Contains(wf.Name)) { Field rf = readerSchema.GetField(wf.Name); string name = rf.aliases?[0] ?? wf.Name; var memberInfo = typeMembers[name]; object value; switch (memberInfo) { case FieldInfo fieldInfo: value = Resolve(wf.Schema, rf.Schema, dec, fieldInfo.FieldType) ?? wf.DefaultValue?.ToObject(typeof(object)); fieldInfo.SetValue(result, value); break; case PropertyInfo propertyInfo: value = Resolve(wf.Schema, rf.Schema, dec, propertyInfo.PropertyType) ?? wf.DefaultValue?.ToObject(typeof(object)); propertyInfo.SetValue(result, value, null); break; } } else { _skipper.Skip(wf.Schema, dec); } } return(result); }
public EventPropertyGetterMapped GetGetterMapped(string mappedPropertyName) { PropertySetDescriptorItem desc = _propertyItems.Get(mappedPropertyName); if (desc == null || !desc.PropertyDescriptor.IsMapped) { return null; } Field field = _avroSchema.GetField(mappedPropertyName); return new AvroEventBeanGetterMappedRuntimeKeyed(field); }
protected virtual IDictionary <string, object> ResolveRecord(RecordSchema writerSchema, RecordSchema readerSchema, IReader dec) { Record result = new Record(readerSchema); foreach (Field wf in writerSchema) { if (readerSchema.Contains(wf.Name)) { Field rf = readerSchema.GetField(wf.Name); object value = Resolve(wf.Schema, rf.Schema, dec) ?? wf.DefaultValue?.ToObject(typeof(object)); AddField(result, rf.aliases?[0] ?? wf.Name, rf.Pos, value); } else { _skipper.Skip(wf.Schema, dec); } } return(result.Contents); }
/// <summary> /// Ctor. /// </summary> /// <param name="eventType">type to create</param> /// <param name="eventAdapterService">event factory</param> /// <param name="properties">written properties</param> public EventBeanManufacturerAvro( AvroSchemaEventType eventType, EventAdapterService eventAdapterService, IList<WriteablePropertyDescriptor> properties) { _eventAdapterService = eventAdapterService; _eventType = (AvroEventType) eventType; _schema = _eventType.SchemaAvro; _indexPerWritable = new Field[properties.Count]; for (int i = 0; i < properties.Count; i++) { string propertyName = properties[i].PropertyName; var field = _schema.GetField(propertyName); if (field == null) { throw new IllegalStateException( "Failed to find property '" + propertyName + "' among the array indexes"); } _indexPerWritable[i] = field; } }
public void ReadAvroAsParameter() { EdmEntityType personType = new EdmEntityType("TestNS", "Person"); personType.AddStructuralProperty("Id", EdmPrimitiveTypeKind.Int32); personType.AddStructuralProperty("Title", EdmPrimitiveTypeKind.String); var operation = new EdmAction("NS", "op1", null); operation.AddParameter("p1", EdmCoreModel.Instance.GetString(false)); operation.AddParameter("p2", new EdmEntityTypeReference(personType, false)); const string Schema = @"{ ""type"":""record"", ""name"":""NS.op1Parameter"", ""fields"": [ { ""name"":""p1"", ""type"":""string"" }, { ""name"":""p2"", ""type"": {""type"":""record"", ""name"":""TestNS.Person"", ""fields"": [ { ""name"":""Id"", ""type"":""int"" }, { ""name"":""Title"", ""type"":""string"" }, ] } } ] }"; var stream = new MemoryStream(); using (var writer = AvroContainer.CreateGenericWriter(Schema, stream, /*leave open*/ true, Codec.Null)) using (var seqWriter = new SequentialWriter <object>(writer, 24)) { RecordSchema parameterSchema = (RecordSchema)AvroSerializer.CreateGeneric(Schema).WriterSchema; AvroRecord ar = new AvroRecord(parameterSchema); ar["p1"] = "dat"; var personSchema = parameterSchema.GetField("p2").TypeSchema; AvroRecord person = new AvroRecord(personSchema); person["Id"] = 5; person["Title"] = "per1"; ar["p2"] = person; seqWriter.Write(ar); seqWriter.Flush(); } stream.Flush(); stream.Seek(0, SeekOrigin.Begin); var reader = new ODataAvroParameterReader(this.CreateODataInputContext(stream), operation); Assert.AreEqual(ODataParameterReaderState.Start, reader.State); Assert.IsTrue(reader.Read()); Assert.AreEqual(ODataParameterReaderState.Value, reader.State); Assert.AreEqual("p1", reader.Name); Assert.AreEqual("dat", reader.Value); Assert.IsTrue(reader.Read()); Assert.AreEqual(ODataParameterReaderState.Entry, reader.State); Assert.AreEqual("p2", reader.Name); var ew = reader.CreateEntryReader(); Assert.AreEqual(ODataReaderState.Start, ew.State); Assert.IsTrue(ew.Read()); Assert.AreEqual(ODataReaderState.EntryStart, ew.State); Assert.IsTrue(ew.Read()); Assert.AreEqual(ODataReaderState.EntryEnd, ew.State); var entry = ew.Item as ODataEntry; Assert.IsFalse(ew.Read()); Assert.AreEqual(ODataReaderState.Completed, ew.State); Assert.IsNotNull(entry); var properties = entry.Properties.ToList(); Assert.AreEqual(2, properties.Count); Assert.AreEqual("Id", properties[0].Name); Assert.AreEqual(5, properties[0].Value); Assert.AreEqual("Title", properties[1].Name); Assert.AreEqual("per1", properties[1].Value); Assert.IsFalse(reader.Read()); Assert.AreEqual(ODataParameterReaderState.Completed, reader.State); }