-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reorder Source/FlowWithContext type parameters #5648
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="FlowWithContextSpec.cs" company="Akka.NET Project"> | ||
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com> | ||
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net> | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Linq; | ||
using Akka.Streams.Dsl; | ||
using Akka.Streams.TestKit; | ||
using Akka.TestKit; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Akka.Streams.Tests.Dsl | ||
{ | ||
public class FlowWithContextSpec : AkkaSpec | ||
{ | ||
private ActorMaterializer Materializer { get; } | ||
|
||
public FlowWithContextSpec(ITestOutputHelper helper) : base(helper) | ||
{ | ||
var settings = ActorMaterializerSettings.Create(Sys); | ||
Materializer = ActorMaterializer.Create(Sys, settings); | ||
} | ||
|
||
[Fact] | ||
public void A_FlowWithContext_must_get_created_from_FlowAsFlowWithContext() | ||
{ | ||
var flow = Flow.Create<Message>().Select(m => m.Copy(data: m.Data + "z")); | ||
var flowWithContext = flow.AsFlowWithContext<Message, long, Message, long, NotUsed, Message>((m, o) => new Message(m.Data, o), m => m.Offset); | ||
|
||
Source.From(new[] { new Message("a", 1L) }) | ||
.AsSourceWithContext(m => m.Offset) | ||
.Via(flowWithContext) | ||
.AsSource() | ||
.RunWith(this.SinkProbe<(Message, long)>(), Materializer) | ||
.Request(1) | ||
.ExpectNext((new Message("az", 1L), 1L)) | ||
.ExpectComplete(); | ||
} | ||
} | ||
|
||
sealed class Message : IEquatable<Message> | ||
{ | ||
public string Data { get; } | ||
public long Offset { get; } | ||
|
||
public Message(string data, long offset) | ||
{ | ||
Data = data; | ||
Offset = offset; | ||
} | ||
|
||
public Message Copy(string data = null, long? offset = null) => new Message(data ?? Data, offset ?? Offset); | ||
|
||
public bool Equals(Message other) | ||
{ | ||
if (other is null) return false; | ||
if (ReferenceEquals(this, other)) return true; | ||
return string.Equals(Data, other.Data) && Offset == other.Offset; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is null) return false; | ||
if (ReferenceEquals(this, obj)) return true; | ||
return obj is Message other && Equals(other); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
unchecked | ||
{ | ||
return ((Data != null ? Data.GetHashCode() : 0) * 397) ^ Offset.GetHashCode(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using Akka.Annotations; | ||
|
||
namespace Akka.Streams.Dsl | ||
{ | ||
|
@@ -17,28 +18,27 @@ namespace Akka.Streams.Dsl | |
/// operations. | ||
/// | ||
/// An "empty" flow can be created by calling <see cref="FlowWithContext.Create{TCtx,TIn}"/>. | ||
/// | ||
/// API MAY CHANGE | ||
///</summary> | ||
public sealed class FlowWithContext<TCtxIn, TIn, TCtxOut, TOut, TMat> | ||
///</summary> | ||
[ApiMayChange] | ||
public sealed class FlowWithContext<TIn, TCtxIn, TOut, TCtxOut, TMat> | ||
: GraphDelegate<FlowShape<(TIn, TCtxIn), (TOut, TCtxOut)>, TMat> | ||
{ | ||
internal FlowWithContext(Flow<(TIn, TCtxIn), (TOut, TCtxOut), TMat> flow) | ||
internal FlowWithContext(Flow<(TIn, TCtxIn), (TOut, TCtxOut), TMat> flow) | ||
: base(flow) | ||
{ | ||
} | ||
|
||
///<summary> | ||
/// Transform this flow by the regular flow. The given flow must support manual context propagation by | ||
/// taking and producing tuples of (data, context). | ||
/// | ||
/// This can be used as an escape hatch for operations that are not (yet) provided with automatic | ||
/// context propagation here. | ||
///</summary> | ||
public FlowWithContext<TCtxIn, TIn, TCtx2, TOut2, TMat> Via<TCtx2, TOut2, TMat2>( | ||
public FlowWithContext<TIn, TCtxIn, TOut2, TCtx2, TMat> Via<TOut2, TCtx2, TMat2>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of changing the existing APIs, can we make it call the new API? We can adapt the existing API @ismaelhamed ? This applies to all other API changes. We can deprecate the old ones and have it call the new ones, that is, old over new. Is that possible? |
||
IGraph<FlowShape<(TOut, TCtxOut), (TOut2, TCtx2)>, TMat2> viaFlow) => | ||
FlowWithContext.From(Flow.FromGraph(Inner).Via(viaFlow)); | ||
|
||
///<summary> | ||
/// Transform this flow by the regular flow. The given flow must support manual context propagation by | ||
/// taking and producing tuples of (data, context). | ||
|
@@ -49,7 +49,7 @@ public FlowWithContext<TCtxIn, TIn, TCtx2, TOut2, TMat> Via<TCtx2, TOut2, TMat2> | |
/// The <paramref name="combine"/> function is used to compose the materialized values of this flow and that | ||
/// flow into the materialized value of the resulting Flow. | ||
///</summary> | ||
public FlowWithContext<TCtxIn, TIn, TCtx2, TOut2, TMat3> ViaMaterialized<TCtx2, TOut2, TMat2, TMat3>( | ||
public FlowWithContext<TIn, TCtxIn, TOut2, TCtx2, TMat3> ViaMaterialized<TOut2, TCtx2, TMat2, TMat3>( | ||
IGraph<FlowShape<(TOut, TCtxOut), (TOut2, TCtx2)>, TMat2> viaFlow, Func<TMat, TMat2, TMat3> combine) => | ||
FlowWithContext.From(Flow.FromGraph(Inner).ViaMaterialized(viaFlow, combine)); | ||
|
||
|
@@ -60,17 +60,17 @@ public FlowWithContext<TCtxIn, TIn, TCtx2, TOut2, TMat3> ViaMaterialized<TCtx2, | |
public static class FlowWithContext | ||
{ | ||
/// <summary> | ||
/// Creates an "empty" <see cref="FlowWithContext{TCtxIn,TIn,TCtxOut,TOut,TMat}"/> that passes elements through with their context unchanged. | ||
/// Creates an "empty" <see cref="FlowWithContext{TIn,TCtxIn,TOut,TCtxOut,TMat}"/> that passes elements through with their context unchanged. | ||
/// </summary> | ||
/// <typeparam name="TCtx"></typeparam> | ||
/// <typeparam name="TIn"></typeparam> | ||
/// <typeparam name="TCtx"></typeparam> | ||
/// <returns></returns> | ||
public static FlowWithContext<TCtx, TIn, TCtx, TIn, NotUsed> Create<TCtx, TIn>() | ||
public static FlowWithContext<TIn, TCtx, TIn, TCtx, NotUsed> Create<TIn, TCtx>() | ||
{ | ||
var under = Flow.Create<(TIn, TCtx), NotUsed>(); | ||
return new FlowWithContext<TCtx, TIn, TCtx, TIn, NotUsed>(under); | ||
return new FlowWithContext<TIn, TCtx, TIn, TCtx, NotUsed>(under); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a FlowWithContext from a regular flow that operates on a pair of `(data, context)` elements. | ||
/// </summary> | ||
|
@@ -81,8 +81,8 @@ public static FlowWithContext<TCtx, TIn, TCtx, TIn, NotUsed> Create<TCtx, TIn>() | |
/// <typeparam name="TOut"></typeparam> | ||
/// <typeparam name="TMat"></typeparam> | ||
/// <returns></returns> | ||
public static FlowWithContext<TCtxIn, TIn, TCtxOut, TOut, TMat> From<TCtxIn, TIn, TCtxOut, TOut, TMat>( | ||
Flow<(TIn, TCtxIn), (TOut, TCtxOut), TMat> flow) => | ||
new FlowWithContext<TCtxIn, TIn, TCtxOut, TOut, TMat>(flow); | ||
public static FlowWithContext<TIn, TCtxIn, TOut, TCtxOut, TMat> From<TIn, TCtxIn, TOut, TCtxOut, TMat>( | ||
Flow<(TIn, TCtxIn), (TOut, TCtxOut), TMat> flow) => | ||
new FlowWithContext<TIn, TCtxIn, TOut, TCtxOut, TMat>(flow); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why can't the compiler infer the type arguments? This should be just:
flow.AsFlowWithContext((m, o) => new Message(m.Data, o), m => m.Offset);