private static AvroSchema ParseRecordSchema(JToken jToken, IDictionary <string, NamedSchema> namedTypes, Stack <string> enclosingNamespace) { var keys = new HashSet <string>() { "type", "name", "fields" }; var optionalKeys = new HashSet <string>() { "namespace", "aliases", "doc" }; JsonUtil.AssertKeys(jToken, keys, optionalKeys, out var tags); JsonUtil.AssertValues(jToken, "type", "record", "error"); var type = JsonUtil.GetValue <string>(jToken, "type"); var name = JsonUtil.GetValue <string>(jToken, "name"); var fields = JsonUtil.GetValue <JArray>(jToken, "fields"); var recordSchema = new RecordSchema(name); if (type.ToString() == "error") { recordSchema = new ErrorSchema(name); } recordSchema.AddTags(tags); if (JsonUtil.TryGetValue <string>(jToken, "namespace", out var ns)) { recordSchema.Namespace = ns; } if (string.IsNullOrEmpty(recordSchema.Namespace)) { recordSchema.Namespace = enclosingNamespace.Peek(); } enclosingNamespace.Push(recordSchema.Namespace); if (JsonUtil.TryGetValue <JArray>(jToken, "aliases", out var aliases)) { recordSchema.Aliases = aliases.Values <string>().ToArray(); } if (JsonUtil.TryGetValue <string>(jToken, "doc", out var doc)) { recordSchema.Doc = doc; } namedTypes.Add(recordSchema.FullName, recordSchema); foreach (var field in ParseRecordFieldSchema(fields, namedTypes, enclosingNamespace)) { recordSchema.Add(field); } enclosingNamespace.Pop(); return(recordSchema); }
public void AddError(ErrorSchema error) { if (Error.Contains(error)) { throw new AvroException($"Request already contains the parameter: '{error.FullName}'"); } Error.Add(error); }
public void AddError() { var message = new MessageSchema("A"); var error = new ErrorSchema("X"); Assert.DoesNotThrow( () => message.AddError(error) ); Assert.Throws( typeof(AvroException), () => message.AddError(error) ); }
public void ErrorCode(ErrorSchema errorSchema, string expectedName, string expectedNamespace, string expectedDoc, string[] expectedAliases) { var assembly = CodeGen.Compile(Guid.NewGuid().ToString(), errorSchema, out var xmlDocument); var type = assembly.ExportedTypes.FirstOrDefault(r => r.Name == expectedName); Assert.NotNull(type); Assert.IsTrue(type.IsClass); Assert.IsTrue(typeof(IAvroRecord).IsAssignableFrom(type)); Assert.AreEqual(expectedNamespace, type.Namespace); var doc = GetSummaryText(xmlDocument, type.FullName); Assert.AreEqual(expectedDoc, doc); var aliases = GetAliasList(xmlDocument, type.FullName); Assert.AreEqual(expectedAliases, aliases); }
public IEnumerator GetEnumerator() { var record01 = new RecordSchema() { Name = "TestRecord01" }; var record02 = new RecordSchema() { Name = "TestRecord02" }; var errord01 = new ErrorSchema() { Name = "TestError01" }; var errord02 = new ErrorSchema() { Name = "TestError02" }; var protocol01 = new AvroProtocol { Name = "ProtocolName" }; yield return(new object[] { protocol01, @"{""protocol"":""ProtocolName""}", @"{ ""protocol"": ""ProtocolName"" }", @"{ ""namespace"": """", ""protocol"": ""ProtocolName"", ""doc"": """", ""types"": [], ""messages"": {} }" }); var protocol02 = new AvroProtocol { Name = "ProtocolName", Namespace = "Test.Namespace" }; yield return(new object[] { protocol02, @"{""protocol"":""Test.Namespace.ProtocolName""}", @"{ ""namespace"": ""Test.Namespace"", ""protocol"": ""ProtocolName"" }", @"{ ""namespace"": ""Test.Namespace"", ""protocol"": ""ProtocolName"", ""doc"": """", ""types"": [], ""messages"": {} }" }); var protocol03 = new AvroProtocol { Name = "ProtocolName", Namespace = "Test.Namespace", Doc = "Test Documentation" }; yield return(new object[] { protocol03, @"{""protocol"":""Test.Namespace.ProtocolName""}", @"{ ""namespace"": ""Test.Namespace"", ""protocol"": ""ProtocolName"", ""doc"": ""Test Documentation"" }", @"{ ""namespace"": ""Test.Namespace"", ""protocol"": ""ProtocolName"", ""doc"": ""Test Documentation"", ""types"": [], ""messages"": {} }" }); var protocol04 = new AvroProtocol { Name = "ProtocolName", Namespace = "Test.Namespace", Doc = "Test Documentation" }; protocol04.AddType(record01); protocol04.AddType(record02); protocol04.AddType(errord01); protocol04.AddType(errord02); yield return(new object[] { protocol04, @"{""protocol"":""Test.Namespace.ProtocolName"",""types"":" + @"[{""name"":""Test.Namespace.TestRecord01"",""type"":""record"",""fields"":[]}," + @"{""name"":""Test.Namespace.TestRecord02"",""type"":""record"",""fields"":[]}," + @"{""name"":""Test.Namespace.TestError01"",""type"":""error"",""fields"":[]}," + @"{""name"":""Test.Namespace.TestError02"",""type"":""error"",""fields"":[]}" + @"]}", @"{ ""namespace"": ""Test.Namespace"", ""protocol"": ""ProtocolName"", ""doc"": ""Test Documentation"", ""types"": " + @"[{ ""type"": ""record"", ""name"": ""TestRecord01"", ""fields"": [] }, " + @"{ ""type"": ""record"", ""name"": ""TestRecord02"", ""fields"": [] }, " + @"{ ""type"": ""error"", ""name"": ""TestError01"", ""fields"": [] }, " + @"{ ""type"": ""error"", ""name"": ""TestError02"", ""fields"": [] }" + @"] }", @"{ ""namespace"": ""Test.Namespace"", ""protocol"": ""ProtocolName"", ""doc"": ""Test Documentation"", ""types"": " + @"[{ ""type"": ""record"", ""name"": ""TestRecord01"", ""namespace"": ""Test.Namespace"", ""doc"": """", ""aliases"": [], ""fields"": [] }, " + @"{ ""type"": ""record"", ""name"": ""TestRecord02"", ""namespace"": ""Test.Namespace"", ""doc"": """", ""aliases"": [], ""fields"": [] }, " + @"{ ""type"": ""error"", ""name"": ""TestError01"", ""namespace"": ""Test.Namespace"", ""doc"": """", ""aliases"": [], ""fields"": [] }, " + @"{ ""type"": ""error"", ""name"": ""TestError02"", ""namespace"": ""Test.Namespace"", ""doc"": """", ""aliases"": [], ""fields"": [] " + @"}]," + @" ""messages"": {} }" }); var protocol05 = new AvroProtocol { Name = "ProtocolName", Namespace = "Test.Namespace", Doc = "Test Documentation" }; protocol05.AddType(record01); protocol05.AddType(record02); protocol05.AddType(errord01); protocol05.AddType(errord02); var message01 = new MessageSchema("M01") { Doc = "Test Doc 01" }; message01.AddParameter(new ParameterSchema("p01", record01)); message01.AddParameter(new ParameterSchema("p02", record02)); message01.AddError(errord01); message01.AddError(errord02); message01.Response = new DoubleSchema(); var message02 = new MessageSchema("M02"); message02.AddParameter(new ParameterSchema("p01", record01)); message02.AddError(errord01); message02.Oneway = true; protocol05.AddMessage(message01); protocol05.AddMessage(message02); yield return(new object[] { protocol05, @"{""protocol"":""Test.Namespace.ProtocolName"",""types"":" + @"[{""name"":""Test.Namespace.TestRecord01"",""type"":""record"",""fields"":[]}," + @"{""name"":""Test.Namespace.TestRecord02"",""type"":""record"",""fields"":[]}," + @"{""name"":""Test.Namespace.TestError01"",""type"":""error"",""fields"":[]}," + @"{""name"":""Test.Namespace.TestError02"",""type"":""error"",""fields"":[]}" + @"]," + @"""messages"":{" + @"""M01"":{" + @"""request"":[{""name"":""p01"",""type"":""Test.Namespace.TestRecord01""},{""name"":""p02"",""type"":""Test.Namespace.TestRecord02""}]," + @"""response"":""double""," + @"""errors"":[""Test.Namespace.TestError01"",""Test.Namespace.TestError02""]" + @"}," + @"""M02"":{" + @"""request"":[{""name"":""p01"",""type"":""Test.Namespace.TestRecord01""}]," + @"""errors"":[""Test.Namespace.TestError01""]," + @"""one-way"":true" + @"}}}", @"{ ""namespace"": ""Test.Namespace"", ""protocol"": ""ProtocolName"", ""doc"": ""Test Documentation"", ""types"": " + @"[{ ""type"": ""record"", ""name"": ""TestRecord01"", ""fields"": [] }, " + @"{ ""type"": ""record"", ""name"": ""TestRecord02"", ""fields"": [] }, " + @"{ ""type"": ""error"", ""name"": ""TestError01"", ""fields"": [] }, " + @"{ ""type"": ""error"", ""name"": ""TestError02"", ""fields"": [] }" + @"], " + @"""messages"": {" + @"""M01"": {" + @"""doc"": ""Test Doc 01"", " + @"""request"": [{ ""name"": ""p01"", ""type"": ""TestRecord01"" }, { ""name"": ""p02"", ""type"": ""TestRecord02"" }], " + @"""response"": ""double"", " + @"""errors"": [""TestError01"", ""TestError02""]" + @"}, " + @"""M02"": {" + @"""request"": [{ ""name"": ""p01"", ""type"": ""TestRecord01"" }], " + @"""errors"": [""TestError01""], " + @"""one-way"": true" + @"}} }", @"{ ""namespace"": ""Test.Namespace"", ""protocol"": ""ProtocolName"", ""doc"": ""Test Documentation"", ""types"": " + @"[{ ""type"": ""record"", ""name"": ""TestRecord01"", ""namespace"": ""Test.Namespace"", ""doc"": """", ""aliases"": [], ""fields"": [] }, " + @"{ ""type"": ""record"", ""name"": ""TestRecord02"", ""namespace"": ""Test.Namespace"", ""doc"": """", ""aliases"": [], ""fields"": [] }, " + @"{ ""type"": ""error"", ""name"": ""TestError01"", ""namespace"": ""Test.Namespace"", ""doc"": """", ""aliases"": [], ""fields"": [] }, " + @"{ ""type"": ""error"", ""name"": ""TestError02"", ""namespace"": ""Test.Namespace"", ""doc"": """", ""aliases"": [], ""fields"": [] }" + @"], " + @"""messages"": {" + @"""M01"": {" + @"""doc"": ""Test Doc 01"", " + @"""request"": [{ ""name"": ""p01"", ""type"": ""Test.Namespace.TestRecord01"" }, { ""name"": ""p02"", ""type"": ""Test.Namespace.TestRecord02"" }], " + @"""response"": { ""type"": ""double"" }, " + @"""errors"": [""Test.Namespace.TestError01"", ""Test.Namespace.TestError02""], " + @"""one-way"": false" + @"}, " + @"""M02"": {" + @"""doc"": """", " + @"""request"": [{ ""name"": ""p01"", ""type"": ""Test.Namespace.TestRecord01"" }], " + @"""response"": null, " + @"""errors"": [""Test.Namespace.TestError01""], " + @"""one-way"": true" + @"}} }" }); }
public GenericError(AvroException exception, ErrorSchema schema) : base(schema) { Exception = exception; }
public GenericError(AvroException exception, ErrorSchema schema, IReadOnlyDictionary <string, int> index, ValueTuple <int, Func <object> >[] defaultInitializers) : base(schema, index, defaultInitializers) { Exception = exception; }
private static void WriteError(TextWriter writer, ErrorSchema schema, WriterMode mode, string parentNamespace, ISet <string> namedSchemas, 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"":""error"""); writer.Write(@",""fields"":["); WriteFields(writer, schema, mode, parentNamespace, namedSchemas, stripNs); writer.Write(@"]"); writer.Write(@"}"); break; case WriterMode.Full: writer.Write(@"{ ""type"": ""error"""); writer.Write(@", ""name"": """); writer.Write(schema.Name); writer.Write(@""""); writer.Write(@", ""namespace"": """); writer.Write(schema.Namespace); writer.Write(@""""); writer.Write(@", ""doc"": """); writer.Write(schema.Doc); writer.Write(@""""); writer.Write(@", ""aliases"": ["); writer.Write(string.Join(", ", schema.Aliases.Select(r => $"\"{r}\""))); writer.Write(@"]"); writer.Write(@", ""fields"": ["); WriteFields(writer, schema, mode, parentNamespace, namedSchemas, stripNs); writer.Write(@"]"); writer.Write(@" }"); break; default: writer.Write(@"{ ""type"": ""error"""); writer.Write(@", ""name"": """); writer.Write(schema.Name); writer.Write(@""""); if (!string.IsNullOrEmpty(ns)) { writer.Write(@", ""namespace"": """); writer.Write(ns); writer.Write(@""""); } if (!string.IsNullOrEmpty(schema.Doc)) { writer.Write(@", ""doc"": """); writer.Write(schema.Doc); writer.Write(@""""); } if (schema.Aliases.Count > 0) { writer.Write(@", ""aliases"": ["); writer.Write(string.Join(", ", schema.Aliases.Select(r => $"\"{r}\""))); writer.Write(@"]"); } writer.Write(@", ""fields"": ["); WriteFields(writer, schema, mode, parentNamespace, namedSchemas, stripNs); writer.Write(@"]"); writer.Write(@" }"); break; } }