Kinetica   C#   API  Version 7.2.3.1
AvroSchemaBuilder.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 
5 namespace kinetica.SchemaBuilder;
6 
10  internal readonly struct FieldDef
11  {
12  public string Name { get; }
13  public AvroType Type { get; }
14  public bool IsNullable { get; }
15  public string? Doc { get; }
16 
17  public FieldDef(string name, AvroType type, bool isNullable, string? doc = null)
18  {
19  Name = name;
20  Type = type;
21  IsNullable = isNullable;
22  Doc = doc;
23  }
24  }
25 
42  public sealed class AvroSchemaBuilder
43  {
44  private readonly string _name;
45  private string? _namespace;
46  private string? _doc;
47  private readonly List<FieldDef> _fields;
48 
53  public AvroSchemaBuilder(string name)
54  {
55  _name = name ?? throw new ArgumentNullException(nameof(name));
56  _fields = new List<FieldDef>();
57  }
58 
62  public AvroSchemaBuilder Namespace(string ns)
63  {
64  _namespace = ns;
65  return this;
66  }
67 
71  public AvroSchemaBuilder Doc(string doc)
72  {
73  _doc = doc;
74  return this;
75  }
76 
77  #region Generic Field Methods
78 
82  public AvroSchemaBuilder Field(string name, AvroType type)
83  {
84  _fields.Add(new FieldDef(name, type, false));
85  return this;
86  }
87 
91  public AvroSchemaBuilder FieldWithDoc(string name, AvroType type, string doc)
92  {
93  _fields.Add(new FieldDef(name, type, false, doc));
94  return this;
95  }
96 
100  public AvroSchemaBuilder NullableField(string name, AvroType type)
101  {
102  _fields.Add(new FieldDef(name, type, true));
103  return this;
104  }
105 
106  #endregion
107 
108  #region Convenience Type Methods
109 
111  public AvroSchemaBuilder IntField(string name)
112  {
113  _fields.Add(new FieldDef(name, AvroType.Int, false));
114  return this;
115  }
116 
118  public AvroSchemaBuilder LongField(string name)
119  {
120  _fields.Add(new FieldDef(name, AvroType.Long, false));
121  return this;
122  }
123 
125  public AvroSchemaBuilder FloatField(string name)
126  {
127  _fields.Add(new FieldDef(name, AvroType.Float, false));
128  return this;
129  }
130 
132  public AvroSchemaBuilder DoubleField(string name)
133  {
134  _fields.Add(new FieldDef(name, AvroType.Double, false));
135  return this;
136  }
137 
139  public AvroSchemaBuilder StringField(string name)
140  {
141  _fields.Add(new FieldDef(name, AvroType.String, false));
142  return this;
143  }
144 
146  public AvroSchemaBuilder BoolField(string name)
147  {
148  _fields.Add(new FieldDef(name, AvroType.Boolean, false));
149  return this;
150  }
151 
153  public AvroSchemaBuilder BytesField(string name)
154  {
155  _fields.Add(new FieldDef(name, AvroType.Bytes, false));
156  return this;
157  }
158 
160  public AvroSchemaBuilder TimestampField(string name)
161  {
162  _fields.Add(new FieldDef(name, AvroType.Long, false));
163  return this;
164  }
165 
166  #endregion
167 
168  #region Nullable Convenience Methods
169 
172  {
173  _fields.Add(new FieldDef(name, AvroType.Int, true));
174  return this;
175  }
176 
179  {
180  _fields.Add(new FieldDef(name, AvroType.Long, true));
181  return this;
182  }
183 
186  {
187  _fields.Add(new FieldDef(name, AvroType.Float, true));
188  return this;
189  }
190 
193  {
194  _fields.Add(new FieldDef(name, AvroType.Double, true));
195  return this;
196  }
197 
200  {
201  _fields.Add(new FieldDef(name, AvroType.String, true));
202  return this;
203  }
204 
207  {
208  _fields.Add(new FieldDef(name, AvroType.Boolean, true));
209  return this;
210  }
211 
214  {
215  _fields.Add(new FieldDef(name, AvroType.Bytes, true));
216  return this;
217  }
218 
221  {
222  _fields.Add(new FieldDef(name, AvroType.Long, true));
223  return this;
224  }
225 
226  #endregion
227 
228  #region Build Methods
229 
233  public string Build()
234  {
235  var sb = new StringBuilder();
236  sb.Append('{');
237  sb.Append("\"type\":\"record\"");
238  sb.Append(",\"name\":\"");
239  sb.Append(EscapeJsonString(_name));
240  sb.Append('"');
241 
242  if (!string.IsNullOrEmpty(_namespace))
243  {
244  sb.Append(",\"namespace\":\"");
245  sb.Append(EscapeJsonString(_namespace!));
246  sb.Append('"');
247  }
248 
249  if (!string.IsNullOrEmpty(_doc))
250  {
251  sb.Append(",\"doc\":\"");
252  sb.Append(EscapeJsonString(_doc!));
253  sb.Append('"');
254  }
255 
256  sb.Append(",\"fields\":[");
257 
258  for (int i = 0; i < _fields.Count; i++)
259  {
260  if (i > 0) sb.Append(',');
261 
262  var field = _fields[i];
263  sb.Append("{\"name\":\"");
264  sb.Append(EscapeJsonString(field.Name));
265  sb.Append("\",\"type\":");
266 
267  var typeName = field.Type.GetTypeName();
268  if (field.IsNullable)
269  {
270  sb.Append("[\"null\",\"");
271  sb.Append(typeName);
272  sb.Append("\"]");
273  }
274  else
275  {
276  sb.Append('"');
277  sb.Append(typeName);
278  sb.Append('"');
279  }
280 
281  if (!string.IsNullOrEmpty(field.Doc))
282  {
283  sb.Append(",\"doc\":\"");
284  sb.Append(EscapeJsonString(field.Doc!));
285  sb.Append('"');
286  }
287 
288  sb.Append('}');
289  }
290 
291  sb.Append("]}");
292  return sb.ToString();
293  }
294 
295  private static string EscapeJsonString(string s)
296  {
297  if (s.IndexOfAny(new[] { '"', '\\', '\n', '\r', '\t' }) < 0)
298  return s;
299 
300  var sb = new StringBuilder(s.Length + 10);
301  foreach (var c in s)
302  {
303  switch (c)
304  {
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;
311  }
312  }
313  return sb.ToString();
314  }
315 
316  #endregion
317  }
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.
Definition: AvroType.cs:7
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.
Definition: Type.cs:36
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)