2 using System.Collections.Generic;
5 namespace kinetica.SchemaBuilder;
12 public string Name {
get; }
15 public string?
Doc {
get; }
44 private readonly
string _name;
45 private string? _namespace;
47 private readonly List<FieldDef> _fields;
55 _name = name ??
throw new ArgumentNullException(nameof(name));
56 _fields =
new List<FieldDef>();
77 #region Generic Field Methods 84 _fields.Add(
new FieldDef(name, type,
false));
93 _fields.Add(
new FieldDef(name, type,
false, doc));
102 _fields.Add(
new FieldDef(name, type,
true));
108 #region Convenience Type Methods 168 #region Nullable Convenience Methods 228 #region Build Methods 235 var sb =
new StringBuilder();
237 sb.Append(
"\"type\":\"record\"");
238 sb.Append(
",\"name\":\"");
239 sb.Append(EscapeJsonString(_name));
242 if (!
string.IsNullOrEmpty(_namespace))
244 sb.Append(
",\"namespace\":\"");
245 sb.Append(EscapeJsonString(_namespace!));
249 if (!
string.IsNullOrEmpty(_doc))
251 sb.Append(
",\"doc\":\"");
252 sb.Append(EscapeJsonString(_doc!));
256 sb.Append(
",\"fields\":[");
258 for (
int i = 0; i < _fields.Count; i++)
260 if (i > 0) sb.Append(
',');
262 var field = _fields[i];
263 sb.Append(
"{\"name\":\"");
264 sb.Append(EscapeJsonString(field.Name));
265 sb.Append(
"\",\"type\":");
267 var typeName = field.Type.GetTypeName();
268 if (field.IsNullable)
270 sb.Append(
"[\"null\",\"");
281 if (!
string.IsNullOrEmpty(field.Doc))
283 sb.Append(
",\"doc\":\"");
284 sb.Append(EscapeJsonString(field.Doc!));
292 return sb.ToString();
295 private static string EscapeJsonString(
string s)
297 if (s.IndexOfAny(
new[] {
'"',
'\\',
'\n',
'\r',
'\t' }) < 0)
300 var sb =
new StringBuilder(s.Length + 10);
305 case '"': sb.Append(
"\\\"");
break;
306 case '\\': sb.Append(
"\\\\");
break;
307 case '\n': sb.Append(
"\\n");
break;
308 case '\r': sb.Append(
"\\r");
break;
309 case '\t': sb.Append(
"\\t");
break;
310 default: sb.Append(c);
break;
313 return sb.ToString();
AvroSchemaBuilder NullableBytesField(string name)
Adds a nullable bytes field.
AvroSchemaBuilder FloatField(string name)
Adds a float field.
AvroSchemaBuilder NullableField(string name, AvroType type)
Adds a nullable field with the specified name and type.
AvroType
Avro primitive types for schema building.
AvroSchemaBuilder BytesField(string name)
Adds a bytes field.
AvroSchemaBuilder Field(string name, AvroType type)
Adds a field with the specified name and type.
AvroSchemaBuilder NullableIntField(string name)
Adds a nullable integer field.
AvroSchemaBuilder TimestampField(string name)
Adds a timestamp field (long).
AvroSchemaBuilder(string name)
Creates a new AvroSchemaBuilder with the specified record name.
AvroSchemaBuilder FieldWithDoc(string name, AvroType type, string doc)
Adds a field with documentation.
AvroSchemaBuilder NullableStringField(string name)
Adds a nullable string field.
AvroSchemaBuilder NullableLongField(string name)
Adds a nullable long field.
AvroSchemaBuilder IntField(string name)
Adds an integer field.
string Build()
Builds the Avro schema JSON string.
AvroSchemaBuilder Doc(string doc)
Sets the documentation for the schema.
Builder for constructing Avro schema JSON strings.
Represents a field definition in an Avro schema.
AvroSchemaBuilder NullableDoubleField(string name)
Adds a nullable double field.
AvroSchemaBuilder NullableFloatField(string name)
Adds a nullable float field.
AvroSchemaBuilder BoolField(string name)
Adds a boolean field.
AvroSchemaBuilder Namespace(string ns)
Sets the namespace for the schema.
Immutable collection of metadata about a Kinetica type.
AvroSchemaBuilder NullableBoolField(string name)
Adds a nullable boolean field.
AvroSchemaBuilder NullableTimestampField(string name)
Adds a nullable timestamp field.
AvroSchemaBuilder LongField(string name)
Adds a long field.
AvroSchemaBuilder DoubleField(string name)
Adds a double field.
AvroSchemaBuilder StringField(string name)
Adds a string field.
FieldDef(string name, AvroType type, bool isNullable, string? doc=null)