Kinetica   C#   API  Version 7.2.3.0
Field.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 using Newtonsoft.Json;
23 
24 namespace Avro
25 {
26 
30  public class Field
31  {
35  public enum SortOrder
36  {
37  ascending,
38  descending,
39  ignore
40  }
41 
45  public readonly string Name;
46 
50  public readonly IList<string> aliases;
51 
55  public int Pos { get; private set; }
56 
60  public string Documentation { get; private set; }
61 
65  public JToken DefaultValue { get; private set; }
66 
70  public SortOrder? Ordering { get; private set; }
71 
75  public Schema Schema { get; private set; }
76 
83  private readonly PropertyMap Props;
84 
88  internal static JTokenEqualityComparer JtokenEqual = new JTokenEqualityComparer();
89 
94 
105  internal Field(Schema schema, string name, IList<string> aliases, int pos, string doc,
106  JToken defaultValue, SortOrder sortorder, PropertyMap props)
107  {
108  if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name", "name cannot be null.");
109  if (null == schema) throw new ArgumentNullException("type", "type cannot be null.");
110  this.Schema = schema;
111  this.Name = name;
112  this.aliases = aliases;
113  this.Pos = pos;
114  this.Documentation = doc;
115  this.DefaultValue = defaultValue;
116  this.Ordering = sortorder;
117  this.Props = props;
118  }
119 
126  protected internal void writeJson(JsonTextWriter writer, SchemaNames names, string encspace)
127  {
128  writer.WriteStartObject();
129  JsonHelper.writeIfNotNullOrEmpty(writer, "name", this.Name);
130  JsonHelper.writeIfNotNullOrEmpty(writer, "doc", this.Documentation);
131 
132  if (null != this.DefaultValue)
133  {
134  writer.WritePropertyName("default");
135  this.DefaultValue.WriteTo(writer, null);
136  }
137  if (null != this.Schema)
138  {
139  writer.WritePropertyName("type");
140  Schema.WriteJson(writer, names, encspace);
141  }
142 
143  if (null != this.Props)
144  this.Props.WriteJson(writer);
145 
146  if (null != aliases)
147  {
148  writer.WritePropertyName("aliases");
149  writer.WriteStartArray();
150  foreach (string name in aliases)
151  writer.WriteValue(name);
152  writer.WriteEndArray();
153  }
154 
155  writer.WriteEndObject();
156  }
157 
163  internal static IList<string> GetAliases(JToken jtok)
164  {
165  JToken jaliases = jtok["aliases"];
166  if (null == jaliases)
167  return null;
168 
169  if (jaliases.Type != JTokenType.Array)
170  throw new SchemaParseException("Aliases must be of format JSON array of strings");
171 
172  var aliases = new List<string>();
173  foreach (JToken jalias in jaliases)
174  {
175  if (jalias.Type != JTokenType.String)
176  throw new SchemaParseException("Aliases must be of format JSON array of strings");
177 
178  aliases.Add((string)jalias);
179  }
180  return aliases;
181  }
182 
188  public string GetProperty(string key)
189  {
190  if (null == this.Props) return null;
191  string v;
192  return (this.Props.TryGetValue(key, out v)) ? v : null;
193  }
194 
200  public override bool Equals(object obj)
201  {
202  if (obj == this) return true;
203  if (obj != null && obj is Field)
204  {
205  Field that = obj as Field;
206  return areEqual(that.Name, Name) && that.Pos == Pos && areEqual(that.Documentation, Documentation)
207  && areEqual(that.Ordering, Ordering) && JtokenEqual.Equals(that.DefaultValue, DefaultValue)
208  && that.Schema.Equals(Schema) && areEqual(that.Props, this.Props);
209  }
210  return false;
211  }
212 
219  private static bool areEqual(object o1, object o2)
220  {
221  return o1 == null ? o2 == null : o1.Equals(o2);
222  }
223 
228  public override int GetHashCode()
229  {
230  return 17 * Name.GetHashCode() + Pos + 19 * getHashCode(Documentation) +
231  23 * getHashCode(Ordering) + 29 * getHashCode(DefaultValue) + 31 * Schema.GetHashCode() +
232  37 * getHashCode(Props);
233  }
234 
240  private static int getHashCode(object obj)
241  {
242  return obj == null ? 0 : obj.GetHashCode();
243  }
244  }
245 }
Schema Schema
Field type's schema
Definition: Field.cs:75
Class for fields defined in a record
Definition: Field.cs:30
override int GetHashCode()
Hash code function
Definition: Schema.cs:272
SortOrder
Enum for the sorting order of record fields
Definition: Field.cs:35
int Pos
Position of the field within its record.
Definition: Field.cs:55
Base class for all schema types
Definition: Schema.cs:29
string GetProperty(string key)
Returns the field's custom property value given the property name
Definition: Field.cs:188
readonly string Name
Name of the field.
Definition: Field.cs:45
virtual internal void WriteJson(JsonTextWriter writer, SchemaNames names, string encspace)
Writes schema object in JSON format
Definition: Schema.cs:248
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
readonly IList< string > aliases
List of aliases for the field name
Definition: Field.cs:50
override int GetHashCode()
Hash code function
Definition: Field.cs:228
SortOrder? Ordering
Order of the field
Definition: Field.cs:70
A class that contains a list of named schemas.
Definition: SchemaName.cs:146
JToken DefaultValue
The default value for the field stored as JSON object, if defined.
Definition: Field.cs:65
string Documentation
Documentation for the field, if any.
Definition: Field.cs:60
internal void writeJson(JsonTextWriter writer, SchemaNames names, string encspace)
Writes the Field class in JSON format
Definition: Field.cs:126
override bool Equals(object obj)
Compares two field objects
Definition: Field.cs:200