5 using System.Collections.Generic;
7 using System.Reflection;
16 private RecordSchema m_schema;
17 private PropertyInfo[] m_properties;
37 m_properties = this.GetType().GetProperties();
46 var t = type ?? this.GetType();
48 m_properties = this.GetType().GetProperties();
56 public object Get(
int fieldPos)
58 return m_properties[fieldPos].GetValue(
this);
66 public void Put(
int fieldPos,
object fieldValue)
68 m_properties[fieldPos].SetValue(
this, fieldValue);
71 private static string? GetEmbeddedSchema( Type t) {
72 string? schema =
null;
74 FieldInfo? field = t.GetField(
"Schema_", BindingFlags.NonPublic | BindingFlags.Static);
79 schema = (string)field.GetValue(
null);
94 string? jsonType = GetEmbeddedSchema(t);
95 jsonType ??= AvroType( t, ktype );
99 return Schema.Parse(jsonType) as RecordSchema;
102 private static bool IsNullable(Type type)
104 if (type ==
null)
return false;
107 if (Nullable.GetUnderlyingType(type) !=
null)
111 if (!type.IsValueType)
113 var attributes = type.CustomAttributes;
114 return attributes.Any(attr => attr.AttributeType.FullName ==
"System.Runtime.CompilerServices.NullableAttribute");
128 private static string AvroType( System.Type? t, KineticaType? ktype )
131 throw new KineticaException(
"Null type passed to AvroType()" );
135 case "Boolean":
return "\"boolean\"";
136 case "Int32":
return "\"int\"";
137 case "Int64":
return "\"long\"";
138 case "Double":
return "\"double\"";
139 case "Single":
return "\"float\"";
140 case "Byte[]":
return "\"bytes\"";
141 case "String":
return "\"string\"";
142 case "String[]":
return $
"{{ \"type\":\"array\", \"items\":\"string\"}}";
143 case "String[][]":
return $
"{{ \"type\":\"array\", \"items\":{{ \"type\":\"array\", \"items\":\"string\"}}}}";
146 case "Nullable`1":
return AvroType(Nullable.GetUnderlyingType(t), ktype);
150 if ( t.IsGenericType )
152 var genericParams = t.GenericTypeArguments;
153 if (1 == genericParams.Length)
155 return $
"{{ \"type\":\"array\", \"items\":{AvroType( genericParams[0], ktype )}}}";
161 case "IDictionary`2":
164 var genericParams = t.GenericTypeArguments;
165 if (2 == genericParams.Length)
167 return $
"{{ \"type\":\"map\", \"values\":{AvroType( genericParams[1], ktype )}}}";
173 case "Schema":
break;
181 if (t.IsSubclassOf(typeof(Object)))
185 PropertyInfo[] type_properties = t.GetProperties( BindingFlags.DeclaredOnly |
186 BindingFlags.Instance |
187 BindingFlags.Public );
188 Array.Sort( type_properties, delegate ( PropertyInfo p1, PropertyInfo p2 )
189 {
return p1.MetadataToken.CompareTo( p2.MetadataToken ); } );
191 foreach ( var prop
in type_properties )
193 bool is_nullable =
false;
194 var prop_type = prop.PropertyType;
195 if ( prop_type.IsGenericType && prop_type.GetGenericTypeDefinition() == typeof( Nullable<> ) )
199 else if ( (ktype !=
null) && ktype.getColumn( prop.Name ).isNullable() )
205 string avroType = AvroType( prop_type, ktype );
206 if ( !String.IsNullOrWhiteSpace( avroType ) )
210 fields += $
"{{\"name\":\"{prop.Name}\",\"type\":[{avroType},\"null\"]}},";
214 fields += $
"{{\"name\":\"{prop.Name}\",\"type\":{avroType}}},";
220 char[] comma = [
','];
221 fields = fields.TrimEnd( comma );
224 return $
"{{\"type\":\"record\",\"name\":\"{t.Name}\",\"fields\":[{fields}]}}";
226 System.Diagnostics.Debug.WriteLine($
"Unkonwn type: {t.Name}");
break;
static ? RecordSchema SchemaFromType(System.Type t, KineticaType? ktype=null)
Create an Avro Schema from a System.Type and a KineticaType.
KineticaData(System.Type type=null)
Default constructor, with optional System.Type
KineticaData(KineticaType type)
Constructor from Kinetica Type
void Put(int fieldPos, object fieldValue)
Write a specific property to this object
Schema Schema
Avro Schema for this class
KineticaData - class to help with Avro Encoding for Kinetica
object Get(int fieldPos)
Retrieve a specific property from this object