Kinetica   C#   API  Version 7.2.3.0
Message.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  public class Message
27  {
31  public string Name { get; set; }
32 
36  public string Doc { get; set; }
37 
41  public RecordSchema Request { get; set; }
42 
46  public Schema Response { get; set; }
47 
51  public UnionSchema Error { get; set; }
52 
56  public bool? Oneway { get; set; }
57 
61  public UnionSchema SupportedErrors { get; set; }
62 
71  public Message(string name, string doc, RecordSchema request, Schema response, UnionSchema error, bool? oneway)
72  {
73  if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name", "name cannot be null.");
74  this.Request = request;
75  this.Response = response;
76  this.Error = error;
77  this.Name = name;
78  this.Doc = doc;
79  this.Oneway = oneway;
80 
81  if (error != null && error.CanRead(Schema.Parse("string")))
82  {
83  this.SupportedErrors = error;
84  }
85  else
86  {
87  this.SupportedErrors = (UnionSchema) Schema.Parse("[\"string\"]");
88 
89  if (error != null)
90  {
91  for (int i = 0; i < error.Schemas.Count; ++i)
92  {
93  this.SupportedErrors.Schemas.Add(error.Schemas[i]);
94  }
95  }
96  }
97  }
98 
106  internal static Message Parse(JProperty jmessage, SchemaNames names, string encspace)
107  {
108  string name = jmessage.Name;
109  string doc = JsonHelper.GetOptionalString(jmessage.Value, "doc");
110  bool? oneway = JsonHelper.GetOptionalBoolean(jmessage.Value, "one-way");
111 
112  PropertyMap props = Schema.GetProperties(jmessage.Value);
113  RecordSchema schema = RecordSchema.NewInstance(Schema.Type.Record, jmessage.Value as JObject, props, names, encspace);
114 
115  JToken jresponse = jmessage.Value["response"];
116  var response = Schema.ParseJson(jresponse, names, encspace);
117 
118  JToken jerrors = jmessage.Value["errors"];
119  UnionSchema uerrorSchema = null;
120  if (null != jerrors)
121  {
122  Schema errorSchema = Schema.ParseJson(jerrors, names, encspace);
123  if (!(errorSchema is UnionSchema))
124  throw new AvroException("");
125 
126  uerrorSchema = errorSchema as UnionSchema;
127  }
128 
129  return new Message(name, doc, schema, response, uerrorSchema, oneway);
130  }
131 
138  internal void writeJson(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
139  {
140  writer.WriteStartObject();
141  JsonHelper.writeIfNotNullOrEmpty(writer, "doc", this.Doc);
142 
143  if (null != this.Request)
144  this.Request.WriteJsonFields(writer, names, null);
145 
146  if (null != this.Response)
147  {
148  writer.WritePropertyName("response");
149  Response.WriteJson(writer, names, encspace);
150  }
151 
152  if (null != this.Error)
153  {
154  writer.WritePropertyName("errors");
155  this.Error.WriteJson(writer, names, encspace);
156  }
157 
158  if (null != Oneway)
159  {
160  writer.WritePropertyName("one-way");
161  writer.WriteValue(Oneway);
162  }
163 
164  writer.WriteEndObject();
165  }
166 
172  public override bool Equals(Object obj)
173  {
174  if (obj == this) return true;
175  if (!(obj is Message)) return false;
176 
177  Message that = obj as Message;
178  return this.Name.Equals(that.Name) &&
179  this.Request.Equals(that.Request) &&
180  areEqual(this.Response, that.Response) &&
181  areEqual(this.Error, that.Error);
182  }
183 
188  public override int GetHashCode()
189  {
190  return Name.GetHashCode() +
191  Request.GetHashCode() +
192  (Response == null ? 0 : Response.GetHashCode()) +
193  (Error == null ? 0 : Error.GetHashCode());
194  }
195 
202  protected static bool areEqual(object o1, object o2)
203  {
204  return o1 == null ? o2 == null : o1.Equals(o2);
205  }
206  }
207 }
override bool Equals(object obj)
Compares equality of two record schemas
Class for record schemas
Definition: RecordSchema.cs:31
internal override void WriteJsonFields(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
Writes the records schema in JSON format
Schema Response
Schema object for the 'response' attribute
Definition: Message.cs:46
static ? bool GetOptionalBoolean(JToken jtok, string field)
Retrieves the optional boolean property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:86
override bool CanRead(Schema writerSchema)
Checks if this schema can read data written by the given schema.
Definition: UnionSchema.cs:127
override int GetHashCode()
Hash code function
Definition: Schema.cs:272
Base class for all schema types
Definition: Schema.cs:29
static bool areEqual(object o1, object o2)
Tests equality of two objects taking null values into account
Definition: Message.cs:202
override int GetHashCode()
Returns the hash code of this Message object
Definition: Message.cs:188
Class for union schemas
Definition: UnionSchema.cs:29
IList< Schema > Schemas
List of schemas in the union
Definition: UnionSchema.cs:34
internal override void WriteJson(Newtonsoft.Json.JsonTextWriter writer, SchemaNames names, string encspace)
Writes union schema in JSON format
Definition: UnionSchema.cs:100
RecordSchema Request
Anonymous record for the list of parameters for the request fields
Definition: Message.cs:41
Type
Enum for schema types
Definition: Schema.cs:34
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.
string Name
Name of the message
Definition: Message.cs:31
override int GetHashCode()
Hash code function
string Doc
Documentation for the message
Definition: Message.cs:36
A class that contains a list of named schemas.
Definition: SchemaName.cs:146
static string GetOptionalString(JToken jtok, string field)
Retrieves the optional string property value for the given property name from the given JSON object.
Definition: JsonHelper.cs:35
Message(string name, string doc, RecordSchema request, Schema response, UnionSchema error, bool? oneway)
Constructor for Message class
Definition: Message.cs:71
override bool Equals(Object obj)
Tests equality of this Message object with the passed object
Definition: Message.cs:172
override int GetHashCode()
Hash code function
Definition: UnionSchema.cs:156
bool? Oneway
Optional one-way attribute
Definition: Message.cs:56
UnionSchema Error
Union schema object for the 'error' attribute
Definition: Message.cs:51
static Schema Parse(string json)
Parses a given JSON string to create a new schema object
Definition: Schema.cs:141
UnionSchema SupportedErrors
Explicitly defined protocol errors plus system added "string" error
Definition: Message.cs:61