/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Text; using Newtonsoft.Json.Linq; namespace Avro { /// /// Class for map schemas /// public class MapSchema : UnnamedSchema { /// /// Schema for map values type /// public Schema ValueSchema { get; set; } /// /// Creates a new from the given schema. /// /// Schema to create the map schema from. /// A new . public static MapSchema CreateMap(Schema type) { return new MapSchema(type,null); } /// /// Static function to return new instance of map schema /// /// JSON object for the map schema /// dictionary that provides access to custom properties /// list of named schemas already read /// enclosing namespace of the map schema /// internal static MapSchema NewInstance(JToken jtok, PropertyMap props, SchemaNames names, string encspace) { JToken jvalue = jtok["values"]; if (null == jvalue) throw new AvroTypeException($"Map does not have 'values' at '{jtok.Path}'"); try { return new MapSchema(ParseJson(jvalue, names, encspace), props); } catch (Exception e) { throw new SchemaParseException($"Error creating MapSchema at '{jtok.Path}'", e); } } /// /// Constructor for map schema class /// /// schema for map values type /// dictionary that provides access to custom properties private MapSchema(Schema valueSchema, PropertyMap props) : base(Type.Map, props) { if (null == valueSchema) throw new ArgumentNullException(nameof(valueSchema), "valueSchema cannot be null."); this.ValueSchema = valueSchema; } /// /// Writes map schema in JSON format /// /// JSON writer /// list of named schemas already written /// enclosing namespace of the map schema protected internal override void WriteJsonFields(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace) { writer.WritePropertyName("values"); ValueSchema.WriteJson(writer, names, encspace); } /// /// Checks if this schema can read data written by the given schema. Used for decoding data. /// /// writer schema /// true if this and writer schema are compatible based on the AVRO specification, false otherwise public override bool CanRead(Schema writerSchema) { if (writerSchema.Tag != Tag) return false; MapSchema that = writerSchema as MapSchema; return ValueSchema.CanRead(that.ValueSchema); } /// /// Compares equality of two map schemas /// /// map schema to compare against this schema /// true if two schemas are equal, false otherwise public override bool Equals(object obj) { if (this == obj) return true; if (obj != null && obj is MapSchema) { MapSchema that = obj as MapSchema; if (ValueSchema.Equals(that.ValueSchema)) return areEqual(that.Props, this.Props); } return false; } /// /// Hashcode function /// /// public override int GetHashCode() { return 29 * ValueSchema.GetHashCode() + getHashCode(Props); } } }