示例#1
0
        private static object?GetDataObject <T>(this CloudEvent cloudEvent, DataSerialization serialization)
            where T : class
        {
            return(_dataObjects.GetValue(cloudEvent, evt =>
            {
                var stringData = evt.StringData;

                if (string.IsNullOrWhiteSpace(stringData))
                {
                    if (evt.BinaryData is not null)
                    {
                        stringData = Encoding.UTF8.GetString(evt.BinaryData);
                    }

                    if (string.IsNullOrWhiteSpace(stringData))
                    {
                        return null !;
                    }
                }

                return serialization == DataSerialization.Json
                    ? JsonDeserialize <T>(stringData !) !
                    : XmlDeserialize <T>(stringData !) !;
            }));
示例#2
0
        /// <summary>
        /// Gets the data (payload) of the <see cref="CloudEvent"/> as type <typeparamref name=
        /// "T"/>.
        /// <para>
        /// This method, along with the <see cref="TryGetData"/> method, is <em>idempotent</em>. In
        /// other words, every call to either of these methods with <em>same instance</em> of <see
        /// cref="CloudEvent"/> and the <em>same type</em> <typeparamref name="T"/> will return the
        /// <em>same instance</em> of type <typeparamref name="T"/>.
        /// </para>
        /// <para>
        /// If the data object of this cloud event was set using the <see cref=
        /// "SetData{TCloudEvent, T}(TCloudEvent, T, DataSerialization)"/> method, then the same
        /// instance of <typeparamref name="T"/> that was passed to that method will be returned by
        /// this method. Otherwise, the value of <see cref="CloudEvent.StringData"/> is used to
        /// deserialize the instance of <typeparamref name="T"/>.
        /// </para>
        /// </summary>
        /// <typeparam name="T">The type of the <see cref="CloudEvent"/> data.</typeparam>
        /// <param name="cloudEvent">The <see cref="CloudEvent"/> to get data from.</param>
        /// <param name="serialization">
        /// If <paramref name="cloudEvent"/> does not already has a data object associated with it,
        /// the kind of serialization that will be used to convert its <see cref=
        /// "CloudEvent.StringData"/> to type <typeparamref name="T"/>.
        /// </param>
        /// <returns>
        /// The data of the <see cref="CloudEvent"/> as type <typeparamref name="T"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="cloudEvent"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <paramref name="serialization"/> is not defined.
        /// </exception>
        /// <exception cref="InvalidCastException">
        /// If <paramref name="cloudEvent"/> already has a data object associated with it, but that
        /// data object cannot be converted to type <typeparamref name="T"/>.
        /// </exception>
        /// <exception cref="JsonException">
        /// If an error occurs during JSON deserialization.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// If an error occurs during XML deserialization.
        /// </exception>
        public static T GetData <T>(this CloudEvent cloudEvent,
                                    DataSerialization serialization = DataSerialization.Json)
            where T : class
        {
            if (cloudEvent is null)
            {
                throw new ArgumentNullException(nameof(cloudEvent));
            }

            if (!Enum.IsDefined(typeof(DataSerialization), serialization))
            {
                throw new ArgumentOutOfRangeException(nameof(serialization));
            }

            var dataObject = cloudEvent.GetDataObject <T>(serialization);

            if (dataObject is T data)
            {
                return(data);
            }

            throw new InvalidCastException(
                      $"Unable to cast the CloudEvent's data of type '{dataObject?.GetType().FullName}' to type '{typeof(T).FullName}'.");
        }
示例#3
0
 public void Bind(IReceiverMessage fromReceiverMessage, CloudEvent toCloudEvent)
 {
 }
示例#4
0
 public void Bind(CloudEvent fromCloudEvent, SenderMessage toSenderMessage)
 {
 }
 public CloudEvent Invoke(CloudEvent cloudEvent) =>
 _invokeConstructor(cloudEvent);
示例#6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CloudEvent"/> class based on the source
 /// cloud event. All cloud event attributes except <see cref="CloudEvent.Id"/> and <see
 /// cref="CloudEvent.Time"/> are copied to the new instance. Note that the source event's
 /// data is <em>not</em> copied to the new instance.
 /// </summary>
 /// <param name="source">
 /// The source for cloud event attribute values.
 /// </param>
 public PartitionedEvent(CloudEvent source)
     : base(source)
 {
 }
示例#7
0
 internal static bool TryGetDataObject(this CloudEvent cloudEvent, out object data) =>
 _dataObjects.TryGetValue(cloudEvent, out data);
示例#8
0
 private static void SetDataObject <T>(this CloudEvent cloudEvent, T data)
     where T : class
 {
     cloudEvent.ClearDataObject();
     _dataObjects.Add(cloudEvent, data);
 }