Kinetica   C#   API  Version 7.2.3.0
NamedSchema.cs
Go to the documentation of this file.
1 
18 using System;
19 using System.Collections.Generic;
20 using System.Text;
21 using Newtonsoft.Json.Linq;
22 
23 
24 namespace Avro
25 {
29  public abstract class NamedSchema : Schema
30  {
34  public SchemaName SchemaName { get; private set; }
35 
39  public override string Name
40  {
41  get { return SchemaName.Name; }
42  }
43 
47  public string Namespace
48  {
49  get { return SchemaName.Namespace; }
50  }
51 
55  public string Fullname
56  {
57  get { return SchemaName.Fullname; }
58  }
59 
63  private readonly IList<SchemaName> aliases;
64 
72  internal static NamedSchema NewInstance(JObject jo, PropertyMap props, SchemaNames names, string encspace)
73  {
74  string type = JsonHelper.GetRequiredString(jo, "type");
75  switch (type)
76  {
77  case "fixed":
78  return FixedSchema.NewInstance(jo, props, names, encspace);
79  case "enum":
80  return EnumSchema.NewInstance(jo, props, names, encspace);
81  case "record":
82  return RecordSchema.NewInstance(Type.Record, jo, props, names, encspace);
83  case "error":
84  return RecordSchema.NewInstance(Type.Error, jo, props, names, encspace);
85  default:
86  NamedSchema result;
87  if (names.TryGetValue(type, null, encspace, out result))
88  return result;
89  return null;
90  }
91  }
92 
99  protected NamedSchema(Type type, SchemaName name, IList<SchemaName> aliases, PropertyMap props, SchemaNames names)
100  : base(type, props)
101  {
102  this.SchemaName = name;
103  this.aliases = aliases;
104  if (null != name.Name) // Added this check for anonymous records inside Message
105  if (!names.Add(name, this))
106  throw new AvroException("Duplicate schema name " + name.Fullname);
107  }
108 
116  protected static SchemaName GetName(JToken jtok, string encspace)
117  {
118  String n = JsonHelper.GetOptionalString(jtok, "name"); // Changed this to optional string for anonymous records in messages
119  String ns = JsonHelper.GetOptionalString(jtok, "namespace");
120  return new SchemaName(n, ns, encspace);
121  }
122 
130  protected static IList<SchemaName> GetAliases(JToken jtok, string space, string encspace)
131  {
132  JToken jaliases = jtok["aliases"];
133  if (null == jaliases)
134  return null;
135 
136  if (jaliases.Type != JTokenType.Array)
137  throw new SchemaParseException("Aliases must be of format JSON array of strings");
138 
139  var aliases = new List<SchemaName>();
140  foreach (JToken jalias in jaliases)
141  {
142  if (jalias.Type != JTokenType.String)
143  throw new SchemaParseException("Aliases must be of format JSON array of strings");
144 
145  aliases.Add(new SchemaName((string)jalias, space, encspace));
146  }
147  return aliases;
148  }
149 
150  protected bool InAliases(SchemaName name)
151  {
152  if (null != aliases)
153  {
154  foreach (SchemaName alias in aliases)
155  if (name.Equals(alias)) return true;
156  }
157  return false;
158  }
159 
166  protected internal override void WriteJson(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
167  {
168  if (!names.Add(this))
169  {
170  // schema is already in the list, write name only
171  SchemaName schemaName = this.SchemaName;
172  string name;
173  if (schemaName.Namespace != encspace)
174  name = schemaName.Namespace + "." + schemaName.Name; // we need to add the qualifying namespace of the target schema if it's not the same as current namespace
175  else
176  name = schemaName.Name;
177  writer.WriteValue(name);
178  }
179  else
180  // schema is not in the list, write full schema definition
181  base.WriteJson(writer, names, encspace);
182  }
183 
190  protected internal override void WriteJsonFields(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
191  {
192  this.SchemaName.WriteJson(writer, names, encspace);
193 
194  if (null != aliases)
195  {
196  writer.WritePropertyName("aliases");
197  writer.WriteStartArray();
198  foreach (SchemaName name in aliases)
199  {
200  string fullname = (null != name.Space) ? name.Space + "." + name.Name : name.Name;
201  writer.WriteValue(fullname);
202  }
203  writer.WriteEndArray();
204  }
205  }
206  }
207 }
static SchemaName GetName(JToken jtok, string encspace)
Parses the name and namespace from the given JSON schema object then creates SchemaName object includ...
Definition: NamedSchema.cs:116
Class for record schemas
Definition: RecordSchema.cs:31
static IList< SchemaName > GetAliases(JToken jtok, string space, string encspace)
Parses the 'aliases' property from the given JSON token
Definition: NamedSchema.cs:130
String? Fullname
Namespace.Name of the schema
Definition: SchemaName.cs:47
Class for enum type schemas
Definition: EnumSchema.cs:28
bool TryGetValue(string name, string space, string encspace, out NamedSchema schema)
Tries to get the value for the given name fields
Definition: SchemaName.cs:207
String Name
Name of the schema
Definition: SchemaName.cs:32
bool InAliases(SchemaName name)
Definition: NamedSchema.cs:150
Base class for all schema types
Definition: Schema.cs:29
override string Name
Name of the schema
Definition: NamedSchema.cs:40
Class for fixed schemas
Definition: FixedSchema.cs:28
NamedSchema(Type type, SchemaName name, IList< SchemaName > aliases, PropertyMap props, SchemaNames names)
Constructor for named schema class
Definition: NamedSchema.cs:99
String? Namespace
Namespace of the schema
Definition: SchemaName.cs:52
static string GetRequiredString(JToken jtok, string field)
Retrieves the required string property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:57
Type
Enum for schema types
Definition: Schema.cs:34
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Class to store schema name, namespace and enclosing namespace
Definition: SchemaName.cs:27
internal override void WriteJsonFields(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
Writes named schema in JSON format
Definition: NamedSchema.cs:190
internal override void WriteJson(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
Writes named schema in JSON format
Definition: NamedSchema.cs:166
string Namespace
Namespace of the schema
Definition: NamedSchema.cs:48
A class that contains a list of named schemas.
Definition: SchemaName.cs:146
static string GetOptionalString(JToken jtok, string field)
Retrieves the optional string property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:35
override bool Equals(Object obj)
Compares two schema names
Definition: SchemaName.cs:114
bool Add(SchemaName name, NamedSchema schema)
Adds a schema name to the map if it doesn't exist yet
Definition: SchemaName.cs:179
String Space
Namespace specified within the schema
Definition: SchemaName.cs:37
string Fullname
Namespace.Name of the schema
Definition: NamedSchema.cs:56
Base class for all named schemas: fixed, enum, record
Definition: NamedSchema.cs:29
SchemaName SchemaName
Name of the schema, contains name, namespace and enclosing namespace
Definition: NamedSchema.cs:34