public static Version?GetMotorVersion <TData>(this MotorCloudEvent <TData> cloudEvent) where TData : class { return(CloudEventValidation.CheckNotNull(cloudEvent, nameof(cloudEvent))[MotorVersionAttribute] is not string versionString ? null : System.Version.Parse(versionString)); }
public static byte?GetRabbitMQPriority <TData>(this MotorCloudEvent <TData> cloudEvent) where TData : class { return(CloudEventValidation.CheckNotNull(cloudEvent, nameof(cloudEvent))[RabbitMQPriorityAttribute] switch { int and(< 0 or > 255) => null, int priority => (byte)priority, _ => null });
public static MotorCloudEvent <TData> SetMotorVersion <TData>(this MotorCloudEvent <TData> cloudEvent) where TData : class { CloudEventValidation.CheckNotNull(cloudEvent, nameof(cloudEvent)); cloudEvent[MotorVersionAttribute] = CurrentVersion ?? throw new InvalidOperationException("Motor.NET version is undefined."); return(cloudEvent); }
public static MotorCloudEvent <TData> SetRabbitMQBinding <TData>(this MotorCloudEvent <TData> cloudEvent, string?exchange, string?routingKey) where TData : class { CloudEventValidation.CheckNotNull(cloudEvent, nameof(cloudEvent)); cloudEvent[RabbitMQExchangeAttribute] = exchange; cloudEvent[RabbitMQRoutingKeyAttribute] = routingKey; return(cloudEvent); }
/// <summary> /// Consumes the remaining content of the given stream, returning /// it as a byte array. /// </summary> /// <param name="stream">The stream to read from. Must not be null.</param> /// <returns>The content of the stream (from its original position), as a byte array.</returns> public static byte[] ToByteArray(Stream stream) { Validation.CheckNotNull(stream, nameof(stream)); var memory = new MemoryStream(); stream.CopyTo(memory); return(memory.ToArray()); }
/// <summary> /// Asynchronously consumes the remaining content of the given stream, returning /// it as a byte array. /// </summary> /// <param name="stream">The stream to read from. Must not be null.</param> /// <returns>The content of the stream (from its original position), as a byte array.</returns> public async static Task <byte[]> ToByteArrayAsync(Stream stream) { Validation.CheckNotNull(stream, nameof(stream)); // TODO: Optimize if it's already a MemoryStream? var memory = new MemoryStream(); await stream.CopyToAsync(memory).ConfigureAwait(false); return(memory.ToArray()); }
/// <summary> /// Decodes the given memory as a string, using the specified encoding. /// </summary> /// <param name="memory">The memory to decode.</param> /// <param name="encoding">The encoding to use. Must not be null.</param> public static string GetString(ReadOnlyMemory <byte> memory, Encoding encoding) { Validation.CheckNotNull(encoding, nameof(encoding)); // TODO: If we introduce an additional netstandard2.1 target, we can use encoding.GetString(memory.Span) var segment = GetArraySegment(memory); return(encoding.GetString(segment.Array, segment.Offset, segment.Count)); }
/// <summary> /// Consumes the remaining content of the given stream, returning /// it as a read-only memory segment. /// </summary> /// <param name="stream">The stream to read from. Must not be null.</param> /// <returns>The content of the stream (from its original position), as a read-only memory segment.</returns> public static ReadOnlyMemory <byte> ToReadOnlyMemory(Stream stream) { Validation.CheckNotNull(stream, nameof(stream)); // TODO: Optimize if it's already a MemoryStream? Will only work in some cases, // and is most likely to occur in tests, where the efficiency doesn't matter as much. var memory = new MemoryStream(); stream.CopyTo(memory); // It's safe to use memory.GetBuffer() and memory.Position here, as this is a stream // we've created using the parameterless constructor. var buffer = memory.GetBuffer(); return(new ReadOnlyMemory <byte>(buffer, 0, (int)memory.Position)); }
public static MotorCloudEvent <TData> SetRabbitMQPriority <TData>(this MotorCloudEvent <TData> cloudEvent, byte?value) where TData : class { CloudEventValidation.CheckNotNull(cloudEvent, nameof(cloudEvent)); cloudEvent[RabbitMQPriorityAttribute] = (int?)value; return(cloudEvent); }
public static string?GetRabbitMQRoutingKey <TData>(this MotorCloudEvent <TData> cloudEvent) where TData : class { return(CloudEventValidation.CheckNotNull(cloudEvent, nameof(cloudEvent))[RabbitMQRoutingKeyAttribute] as string); }
/// <summary> /// Copies the given memory to a stream, asynchronously. /// </summary> /// <param name="source">The source memory to copy from.</param> /// <param name="destination">The stream to copy to. Must not be null.</param> /// <returns>A task representing the asynchronous operation.</returns> public static async Task CopyToStreamAsync(ReadOnlyMemory <byte> source, Stream destination) { Validation.CheckNotNull(destination, nameof(destination)); var segment = GetArraySegment(source); await destination.WriteAsync(segment.Array, segment.Offset, segment.Count).ConfigureAwait(false); }