19 using System.Collections.Generic;
21 using Newtonsoft.Json.Linq;
22 using Newtonsoft.Json;
36 public List<Field>
Fields {
get;
private set; }
46 private readonly IDictionary<string, Field> fieldLookup;
48 private readonly IDictionary<string, Field> fieldAliasLookup;
62 JToken jfields = jtok[
"fields"];
65 jfields = jtok[
"request"];
66 if (
null != jfields) request =
true;
69 throw new SchemaParseException(
"'fields' cannot be null for record");
70 if (jfields.Type != JTokenType.Array)
71 throw new SchemaParseException(
"'fields' not an array for record");
73 var name =
GetName(jtok, encspace);
74 var aliases =
NamedSchema.GetAliases(jtok, name.Space, name.EncSpace);
75 var fields =
new List<Field>();
76 var fieldMap =
new Dictionary<string, Field>();
77 var fieldAliasMap =
new Dictionary<string, Field>();
78 var result =
new RecordSchema(type, name, aliases, props, fields, request, fieldMap, fieldAliasMap, names);
81 foreach (JObject jfield
in jfields)
83 string fieldName = JsonHelper.GetRequiredString(jfield,
"name");
84 Field field = createField(jfield, fieldPos++, names, name.Namespace);
86 addToFieldMap(fieldMap, fieldName, field);
87 addToFieldMap(fieldAliasMap, fieldName, field);
89 if (
null != field.aliases)
90 foreach (
string alias
in field.aliases)
91 addToFieldMap(fieldAliasMap, alias, field);
106 private RecordSchema(
Type type,
SchemaName name, IList<SchemaName> aliases, PropertyMap props,
107 List<Field> fields,
bool request, IDictionary<string, Field> fieldMap,
108 IDictionary<string, Field> fieldAliasMap, SchemaNames names)
109 : base(type, name, aliases, props, names)
111 if (!request &&
null == name.Name)
throw new SchemaParseException(
"name cannot be null for record schema.");
113 this.request = request;
114 this.fieldLookup = fieldMap;
115 this.fieldAliasLookup = fieldAliasMap;
126 private static Field createField(JToken jfield,
int pos, SchemaNames names,
string encspace)
128 var name = JsonHelper.GetRequiredString(jfield,
"name");
129 var doc = JsonHelper.GetOptionalString(jfield,
"doc");
131 var jorder = JsonHelper.GetOptionalString(jfield,
"order");
132 Field.SortOrder sortorder = Field.SortOrder.ignore;
134 sortorder = (Field.SortOrder) Enum.Parse(typeof(Field.SortOrder), jorder);
136 var aliases = Field.GetAliases(jfield);
137 var props =
Schema.GetProperties(jfield);
138 var defaultValue = jfield[
"default"];
140 JToken jtype = jfield[
"type"];
142 throw new SchemaParseException(
"'type' was not found for field: " + name);
143 var schema =
Schema.ParseJson(jtype, names, encspace);
145 return new Field(schema, name, aliases, pos, doc, defaultValue, sortorder, props);
148 private static void addToFieldMap(Dictionary<string, Field> map,
string name, Field field)
150 if (map.ContainsKey(name))
151 throw new SchemaParseException(
"field or alias " + name +
" is a duplicate name");
152 map.Add(name, field);
160 public Field
this[
string name]
164 if (
string.IsNullOrEmpty(name))
throw new ArgumentNullException(
"name");
166 return (fieldLookup.TryGetValue(name, out field)) ? field :
null;
177 return fieldLookup.ContainsKey(fieldName);
182 return fieldLookup.TryGetValue(fieldName, out field);
186 return fieldAliasLookup.TryGetValue(fieldName, out field);
195 return Fields.GetEnumerator();
206 base.WriteJsonFields(writer, names, encspace);
210 writer.WritePropertyName(
"request");
212 writer.WritePropertyName(
"fields");
213 writer.WriteStartArray();
217 foreach (
Field field
in this)
220 writer.WriteEndArray();
230 if (obj ==
this)
return true;
234 return protect(() =>
true, () =>
239 return areEqual(that.Props,
this.Props);
253 return protect(() => 0, () =>
269 if ((writerSchema.
Tag !=
Type.Record) && (writerSchema.
Tag !=
Type.Error))
return false;
272 return protect(() =>
true, () =>
278 foreach (
Field f
in this)
283 foreach (
string alias
in f.
aliases)
286 if (
null != f2)
break;
299 private class RecordSchemaPair
307 this.second = second;
312 private static List<RecordSchemaPair> seen;
328 seen =
new List<RecordSchemaPair>();
330 else if (seen.Find((RecordSchemaPair rs) => rs.first ==
this && rs.second == that) !=
null)
333 RecordSchemaPair p =
new RecordSchemaPair(
this, that);
335 try {
return main(); }
336 finally { seen.Remove(p); }
static SchemaName GetName(JToken jtok, string encspace)
Parses the name and namespace from the given JSON schema object then creates SchemaName object includ...
override bool Equals(object obj)
Compares equality of two record schemas
internal override void WriteJsonFields(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
Writes the records schema in JSON format
static int getHashCode(object obj)
Hash code helper function
Schema Schema
Field type's schema
Class for fields defined in a record
bool InAliases(SchemaName name)
Base class for all schema types
IEnumerator< Field > GetEnumerator()
Returns an enumerator which enumerates over the fields of this record schema
readonly string Name
Name of the field.
virtual bool CanRead(Schema writerSchema)
Returns true if and only if data written using writerSchema can be read using the current schema acco...
NamedSchema(Type type, SchemaName name, IList< SchemaName > aliases, PropertyMap props, SchemaNames names)
Constructor for named schema class
Type
Enum for schema types
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
internal delegate T Function< T >()
Class to store schema name, namespace and enclosing namespace
override int GetHashCode()
Hash code function
override int GetHashCode()
Hash code function
bool TryGetField(string fieldName, out Field field)
string Namespace
Namespace of the schema
A class that contains a list of named schemas.
override int GetHashCode()
int Count
Number of fields in the record
override bool CanRead(Schema writerSchema)
Checks if this schema can read data written by the given schema.
JToken DefaultValue
The default value for the field stored as JSON object, if defined.
override bool Equals(Object obj)
Compares two schema names
Schema(Type type, PropertyMap props)
Constructor for schema class
static bool areEqual(object o1, object o2)
Compares two objects, null is equal to null
internal void writeJson(JsonTextWriter writer, SchemaNames names, string encspace)
Writes the Field class in JSON format
Type Tag
Schema type property
List< Field > Fields
List of fields in the record
Base class for all named schemas: fixed, enum, record
bool TryGetFieldAlias(string fieldName, out Field field)
bool Contains(string fieldName)
Returns true if and only if the record contains a field by the given name.
SchemaName SchemaName
Name of the schema, contains name, namespace and enclosing namespace