예제 #1
0
        /// <summary>
        /// Gets the data (payload) of the <see cref="CloudEvent"/> as type <typeparamref name=
        /// "T"/>.
        /// <para>
        /// This method, along with the <see cref="GetData"/> 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="data">
        /// When this method returns, the data of the <paramref name="cloudEvent"/> if it exists
        /// as (or can be serialized to) type <typeparamref name="T"/>; otherwise, <see langword=
        /// "null"/>. This parameter is passed uninitialized.
        /// </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>
        /// <see langword="true"/> if the data of the <paramref name="cloudEvent"/> exists as (or
        /// can be serialized to) type <typeparamref name="T"/>; otherwise <see langword="false"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="cloudEvent"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// If <paramref name="serialization"/> is not defined.
        /// </exception>
        public static bool TryGetData <T>(this CloudEvent cloudEvent, out T data,
                                          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));
            }

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

                if (dataObject is T)
                {
                    data = (T)dataObject;
                    return(true);
                }
            }
            catch { }

            data = default;
            return(false);
        }
예제 #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}'.");
        }