private static AvroSchema ParseFixedSchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace) { var keys = new HashSet <string>() { "type", "size", "name" }; var optionalKeys = new HashSet <string>() { "namespace" }; JsonUtil.AssertKeys(jToken, keys, optionalKeys, out var tags); JsonUtil.AssertValue(jToken, "type", "fixed"); var size = JsonUtil.GetValue <int>(jToken, "size"); var name = JsonUtil.GetValue <string>(jToken, "name"); JsonUtil.TryGetValue <string>(jToken, "namespace", out var ns); var fixedSchema = new FixedSchema(name, ns, size); if (string.IsNullOrEmpty(fixedSchema.Namespace)) { fixedSchema.Namespace = enclosingNamespace.Peek(); } fixedSchema.AddTags(tags); namedTypes.Add(fixedSchema.FullName, fixedSchema); return(fixedSchema); }
/// <summary> /// Creates a class declaration for fixed schema /// </summary> /// <param name="schema">fixed schema</param> /// <param name="ns">namespace object</param> protected virtual void processFixed(Schema schema) { FixedSchema fixedSchema = schema as FixedSchema; if (null == fixedSchema) { throw new CodeGenException("Unable to cast schema into a fixed"); } CodeTypeDeclaration ctd = new CodeTypeDeclaration(); ctd.Name = CodeGenUtil.Instance.Mangle(fixedSchema.Name); ctd.IsClass = true; ctd.IsPartial = true; ctd.Attributes = MemberAttributes.Public; ctd.BaseTypes.Add("SpecificFixed"); if (fixedSchema.Documentation != null) { ctd.Comments.Add(createDocComment(fixedSchema.Documentation)); } // create static schema field createSchemaField(schema, ctd, true); // Add Size field string sizefname = "fixedSize"; var ctrfield = new CodeTypeReference(typeof(uint)); var codeField = new CodeMemberField(ctrfield, sizefname); codeField.Attributes = MemberAttributes.Private | MemberAttributes.Static; codeField.InitExpression = new CodePrimitiveExpression(fixedSchema.Size); ctd.Members.Add(codeField); // Add Size property var property = new CodeMemberProperty(); property.Attributes = MemberAttributes.Public | MemberAttributes.Static; property.Name = "FixedSize"; property.Type = ctrfield; property.GetStatements.Add(new CodeMethodReturnStatement(new CodeTypeReferenceExpression(schema.Name + "." + sizefname))); ctd.Members.Add(property); // create constructor to initiate base class SpecificFixed CodeConstructor cc = new CodeConstructor(); cc.Attributes = MemberAttributes.Public; cc.BaseConstructorArgs.Add(new CodeVariableReferenceExpression(sizefname)); ctd.Members.Add(cc); string nspace = fixedSchema.Namespace; if (string.IsNullOrEmpty(nspace)) { throw new CodeGenException("Namespace required for enum schema " + fixedSchema.Name); } CodeNamespace codens = addNamespace(nspace); codens.Types.Add(ctd); }
/// <summary> /// Static function to return a new instance of the named schema /// </summary> /// <param name="jo">JSON object of the named schema</param> /// <param name="names">list of named schemas already read</param> /// <param name="encspace">enclosing namespace of the named schema</param> /// <returns></returns> internal static NamedSchema NewInstance(JObject jo, PropertyMap props, SchemaNames names, string encspace) { string type = JsonHelper.GetRequiredString(jo, "type"); switch (type) { case "fixed": return(FixedSchema.NewInstance(jo, props, names, encspace)); case "enum": return(EnumSchema.NewInstance(jo, props, names, encspace)); case "record": return(RecordSchema.NewInstance(Type.Record, jo, props, names, encspace)); case "error": return(RecordSchema.NewInstance(Type.Error, jo, props, names, encspace)); default: NamedSchema result; if (names.TryGetValue(type, null, encspace, out result)) { return(result); } return(null); } }
/// <summary> /// Compares two fixed schemas /// </summary> /// <param name="obj">fixed schema to compare against this schema</param> /// <returns>true if two schemas are the same, false otherwise</returns> public override bool Equals(object obj) { if (obj == this) { return(true); } if (obj != null && obj is FixedSchema) { FixedSchema that = obj as FixedSchema; return(SchemaName.Equals(that.SchemaName) && Size == that.Size && areEqual(that.Props, this.Props)); } return(false); }
/// <summary> /// Checks if this schema can read data written by the given schema. Used for decoding data. /// </summary> /// <param name="writerSchema">writer schema</param> /// <returns>true if this and writer schema are compatible based on the AVRO specification, false otherwise</returns> public override bool CanRead(Schema writerSchema) { if (writerSchema.Tag != Tag) { return(false); } FixedSchema that = writerSchema as FixedSchema; if (that.Size != Size) { return(false); } if (that.SchemaName.Equals(SchemaName)) { return(true); } else { return(InAliases(that.SchemaName)); } }
private static StringBuilder Build(IDictionary <string, string> env, Schema s, StringBuilder o) { bool firstTime = true; Schema.Type st = s.Tag; switch (st) { case Schema.Type.Union: UnionSchema us = s as UnionSchema; o.Append('['); foreach (Schema b in us.Schemas) { if (!firstTime) { o.Append(','); } else { firstTime = false; } Build(env, b, o); } return(o.Append(']')); case Schema.Type.Array: case Schema.Type.Map: o.Append("{\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append('\"'); if (st == Schema.Type.Array) { ArraySchema arraySchema = s as ArraySchema; Build(env, arraySchema.ItemSchema, o.Append(",\"items\":")); } else { MapSchema mapSchema = s as MapSchema; Build(env, mapSchema.ValueSchema, o.Append(",\"values\":")); } return(o.Append('}')); case Schema.Type.Enumeration: case Schema.Type.Fixed: case Schema.Type.Record: NamedSchema namedSchema = s as NamedSchema; var name = namedSchema.Fullname; if (env.ContainsKey(name)) { return(o.Append(env[name])); } var qname = "\"" + name + "\""; env.Add(name, qname); o.Append("{\"name\":").Append(qname); o.Append(",\"type\":\"").Append(Schema.GetTypeString(s.Tag)).Append('\"'); if (st == Schema.Type.Enumeration) { EnumSchema enumSchema = s as EnumSchema; o.Append(",\"symbols\":["); foreach (var enumSymbol in enumSchema.Symbols) { if (!firstTime) { o.Append(','); } else { firstTime = false; } o.Append('\"').Append(enumSymbol).Append('\"'); } o.Append(']'); } else if (st == Schema.Type.Fixed) { FixedSchema fixedSchema = s as FixedSchema; o.Append(",\"size\":") .Append(fixedSchema.Size.ToString(CultureInfo.InvariantCulture)); } else // st == Schema.Type.Record { RecordSchema recordSchema = s as RecordSchema; o.Append(",\"fields\":["); foreach (var field in recordSchema.Fields) { if (!firstTime) { o.Append(','); } else { firstTime = false; } o.Append("{\"name\":\"").Append(field.Name).Append('\"'); Build(env, field.Schema, o.Append(",\"type\":")).Append('}'); } o.Append(']'); } return(o.Append('}')); default: //boolean, bytes, double, float, int, long, null, string return(o.Append('\"').Append(s.Name).Append('\"')); } }
private static void WriteFixed(TextWriter writer, FixedSchema schema, string parentNamespace, WriterMode mode, bool stripNs) { var ns = parentNamespace; if (!string.IsNullOrEmpty(schema.Namespace)) { ns = schema.Namespace; } if (stripNs && ns == parentNamespace) { ns = null; } switch (mode) { case WriterMode.Canonical: writer.Write(@"{""name"":"""); writer.Write(schema.FullName); writer.Write(@""""); writer.Write(@",""type"":""fixed"""); writer.Write(@",""size"":"); writer.Write(schema.Size); writer.Write(@"}"); break; case WriterMode.Full: writer.Write(@"{ ""type"": ""fixed"""); writer.Write(@", ""name"": """); writer.Write(schema.Name); writer.Write(@""""); writer.Write(@", ""namespace"": """); writer.Write(schema.Namespace); writer.Write(@""""); writer.Write(@", ""size"": "); writer.Write(schema.Size); writer.Write(@", ""aliases"": ["); writer.Write(string.Join(", ", schema.Aliases.Select(r => $"\"{r}\""))); writer.Write(@"]"); writer.Write(@" }"); break; default: writer.Write(@"{ ""type"": ""fixed"""); writer.Write(@", ""name"": """); writer.Write(schema.Name); writer.Write(@""""); if (!string.IsNullOrEmpty(ns)) { writer.Write(@", ""namespace"": """); writer.Write(ns); writer.Write(@""""); } writer.Write(@", ""size"": "); writer.Write(schema.Size); if (schema.Aliases.Count() > 0) { writer.Write(@", ""aliases"": ["); writer.Write(string.Join(", ", schema.Aliases.Select(r => $"\"{r}\""))); writer.Write(@"]"); } writer.Write(@" }"); break; } }