5 using System.Collections.Generic;
7 using System.Reflection;
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);
109 internal static string NormalizeSchemaJson(
string? schemaJson)
111 if (
string.IsNullOrWhiteSpace(schemaJson))
112 return schemaJson ??
string.Empty;
117 var token = Newtonsoft.Json.Linq.JToken.
Parse(schemaJson);
118 return token.
ToString(Newtonsoft.Json.Formatting.None);
120 catch (Newtonsoft.Json.JsonException)
128 private static bool IsNullable(
Type type)
130 if (type ==
null)
return false;
133 if (Nullable.GetUnderlyingType(type) !=
null)
137 if (!type.IsValueType)
139 var attributes = type.CustomAttributes;
140 return attributes.Any(attr => attr.AttributeType.FullName ==
"System.Runtime.CompilerServices.NullableAttribute");
161 case "Boolean":
return "\"boolean\"";
162 case "Int32":
return "\"int\"";
163 case "Int64":
return "\"long\"";
164 case "Double":
return "\"double\"";
165 case "Single":
return "\"float\"";
166 case "Byte[]":
return "\"bytes\"";
167 case "String":
return "\"string\"";
168 case "String[]":
return $
"{{ \"type\":\"array\", \"items\":\"string\"}}";
169 case "String[][]":
return $
"{{ \"type\":\"array\", \"items\":{{ \"type\":\"array\", \"items\":\"string\"}}}}";
172 case "Nullable`1":
return AvroType(Nullable.GetUnderlyingType(t), ktype);
176 if ( t.IsGenericType )
178 var genericParams = t.GenericTypeArguments;
179 if (1 == genericParams.Length)
181 return $
"{{ \"type\":\"array\", \"items\":{AvroType( genericParams[0], ktype )}}}";
187 case "IDictionary`2":
190 var genericParams = t.GenericTypeArguments;
191 if (2 == genericParams.Length)
193 return $
"{{ \"type\":\"map\", \"values\":{AvroType( genericParams[1], ktype )}}}";
199 case "Schema":
break;
207 if (t.IsSubclassOf(typeof(Object)))
211 PropertyInfo[] type_properties = t.GetProperties( BindingFlags.DeclaredOnly |
212 BindingFlags.Instance |
213 BindingFlags.Public );
214 Array.Sort( type_properties, delegate ( PropertyInfo p1, PropertyInfo p2 )
215 {
return p1.MetadataToken.CompareTo( p2.MetadataToken ); } );
217 foreach ( var prop
in type_properties )
219 bool is_nullable =
false;
220 var prop_type = prop.PropertyType;
221 if ( prop_type.IsGenericType && prop_type.GetGenericTypeDefinition() == typeof( Nullable<> ) )
231 string avroType =
AvroType( prop_type, ktype );
232 if ( !
String.IsNullOrWhiteSpace( avroType ) )
236 fields += $
"{{\"name\":\"{prop.Name}\",\"type\":[{avroType},\"null\"]}},";
240 fields += $
"{{\"name\":\"{prop.Name}\",\"type\":{avroType}}},";
246 char[] comma = [
','];
247 fields = fields.TrimEnd( comma );
250 return $
"{{\"type\":\"record\",\"name\":\"{t.Name}\",\"fields\":[{fields}]}}";
252 System.Diagnostics.Debug.WriteLine($
"Unkonwn type: {t.Name}");
break;
AvroType
Avro primitive types for schema building.
Base class for all schema types
KineticaData - class to help with Avro Encoding for Kinetica
bool isNullable()
Returns if the column is nullable.
KineticaData(KineticaType type)
Constructor from Kinetica Type
Interface class for generated classes
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
override string ToString()
Returns the canonical JSON representation of this schema.
object Get(int fieldPos)
Retrieve a specific property from this object
Column getColumn(int index)
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
Immutable collection of metadata about a Kinetica type.
static Schema Parse(string json)
Parses a given JSON string to create a new schema object
void Put(int fieldPos, object fieldValue)
Write a specific property to this object