/// <summary> /// Helper method for retrieving an enumeration property. /// </summary> /// <typeparam name="TEnum">The enumeration type.</typeparam> /// <param name="key">The property key.</param> /// <param name="def">The default value to be returned if the named property doesn't exist.</param> /// <returns>The long value.</returns> internal TEnum GetEnumProperty <TEnum>(PropertyNameUtf8 key, TEnum def = default(TEnum)) where TEnum : struct, Enum { if (Properties.TryGetValue(key, out var value)) { return(NeonHelper.ParseEnum <TEnum>(value, def)); } else { return(def); } }
/// <summary> /// Helper method for retrieving a timespan property. /// </summary> /// <param name="key">The property key.</param> /// <param name="def">The default value to be returned if the named property doesn't exist.</param> /// <returns>The double value.</returns> internal TimeSpan GetTimeSpanProperty(PropertyNameUtf8 key, TimeSpan def = default) { if (Properties.TryGetValue(key, out var value)) { var ticks = long.Parse(value, CultureInfo.InvariantCulture); return(TimeSpan.FromTicks(ticks)); } else { return(def); } }
/// <summary> /// Helper method for retrieving a byte array property. /// </summary> /// <param name="key">The property key.</param>] /// <returns>The byte array or <c>null</c>.</returns> internal byte[] GetBytesProperty(PropertyNameUtf8 key) { if (Properties.TryGetValue(key, out var value)) { if (value == null) { return(null); } return(Convert.FromBase64String(value)); } else { return(null); } }
/// <summary> /// Helper method for retrieving a complex property serialized as a JSON string. /// </summary> /// <typeparam name="T">The property type.</typeparam> /// <param name="key">The property key.</param> /// <returns>The parsed value if the property exists or <c>null</c>.</returns> /// <remarks> /// <note> /// <para> /// <b>IMPORTANT:</b> Be very careful when referencing properties that use this /// method because the behavior will probably be unexepected. You should: /// </para> /// <list type="bullet"> /// <item> /// When you need to access multiple subfields of the property value, /// dereference the property once, save the value to a variable and /// then use the variable to access the subproperty. Not doing this /// will result in the JSON being parsed again for each property /// reference. /// </item> /// <item> /// Dereferencing the property and changing a subproperty value won't /// actually persist the change back to the underlying property. You'll /// need to dereference the property to a variable, change the subproperty, /// and then use <see cref="SetJsonProperty{T}(PropertyNameUtf8, T)"/> to persist the /// change. /// </item> /// </list> /// <para> /// These restrictions are a bit odd but we're not actually expecting to /// be doing any of these things within the <b>cadence-client</b> code. /// </para> /// </note> /// </remarks> internal T GetJsonProperty <T>(PropertyNameUtf8 key) where T : class, new() { if (Properties.TryGetValue(key, out var value)) { if (value == null) { return(null); } if (typeof(T).Implements <IRoundtripData>()) { return(RoundtripDataFactory.CreateFrom <T>(JObject.Parse(value))); } else { return(NeonHelper.JsonDeserialize <T>(value, strict: false)); } } else { return(null); } }
/// <summary> /// Helper method for setting a timespan property. /// </summary> /// <param name="key">The property key.</param> /// <param name="value">The property value.</param> internal void SetTimeSpanProperty(PropertyNameUtf8 key, TimeSpan value) { Properties[key] = value.Ticks.ToString(CultureInfo.InvariantCulture); }
/// <summary> /// Helper method for setting a date/time property. /// </summary> /// <param name="key">The property key.</param> /// <param name="value">The property value.</param> internal void SetDateTimeProperty(PropertyNameUtf8 key, DateTime value) { Properties[key] = value.ToString(NeonHelper.DateFormatTZ); }
/// <summary> /// Helper method for setting a double property. /// </summary> /// <param name="key">The property key.</param> /// <param name="value">The property value.</param> internal void SetDoubleProperty(PropertyNameUtf8 key, double value) { Properties[key] = value.ToString("G"); }
/// <summary> /// Helper method for setting an enumeration property. /// </summary> /// <typeparam name="TEnum">The enumeration type.</typeparam> /// <param name="key">The property key.</param> /// <param name="value">The property value.</param> internal void SetEnumProperty <TEnum>(PropertyNameUtf8 key, TEnum value) where TEnum : struct { Properties[key] = NeonHelper.EnumToString(value); }
/// <summary> /// Helper method for setting a boolean property. /// </summary> /// <param name="key">The property key.</param> /// <param name="value">The property value.</param> internal void SetBoolProperty(PropertyNameUtf8 key, bool value) { Properties[key] = NeonHelper.ToBoolString(value); }
/// <summary> /// Helper method for setting a 64-bit integer property. /// </summary> /// <param name="key">The property key.</param> /// <param name="value">The property value.</param> internal void SetLongProperty(PropertyNameUtf8 key, long value) { Properties[key] = value.ToString(); }
/// <summary> /// Helper method for setting a 32-bit integer property. /// </summary> /// <param name="key">The property key.</param> /// <param name="value">The property value.</param> internal void SetIntProperty(PropertyNameUtf8 key, int value) { Properties[key] = value.ToString(CultureInfo.InvariantCulture); }
//--------------------------------------------------------------------- // Helper methods derived classes can use for setting typed message properties. /// <summary> /// Helper method for setting a string property. /// </summary> /// <param name="key">The property key.</param> /// <param name="value">The property value.</param> internal void SetStringProperty(PropertyNameUtf8 key, string value) { Properties[key] = value; }
/// <summary> /// Serialize a string from a <see cref="PropertyNameUtf8"/>. /// </summary> /// <param name="writer">The output writer.</param> /// <param name="value">The string being serialized.</param> private static void WriteString(BinaryWriter writer, PropertyNameUtf8 value) { writer.Write(value.NameUtf8.Length); writer.Write(value.NameUtf8); }
/// <summary> /// Adds a property name to the internal hash table. /// </summary> /// <param name="propertyName">The property name.</param> private static void Add(PropertyNameUtf8 propertyName) { var hashCode = propertyName.GetHashCode(); buckets[hashCode % buckets.Length].Add(propertyName); }