Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/NATS.Client.JetStream/NatsClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@ public static INatsJSContext CreateJetStreamContext(this INatsClient client)
/// <returns>Returns an instance of <see cref="INatsJSContext"/> for interacting with JetStream.</returns>
public static INatsJSContext CreateJetStreamContext(this INatsConnection connection)
=> new NatsJSContext(connection);

/// <summary>
/// Creates a JetStream context using the provided NATS client.
/// </summary>
/// <param name="client">The NATS client used to create the JetStream context.</param>
/// <param name="opts">Context options.</param>
/// <returns>Returns an instance of <see cref="INatsJSContext"/> for interacting with JetStream.</returns>
public static INatsJSContext CreateJetStreamContext(this INatsClient client, NatsJSOpts opts)
=> CreateJetStreamContext(client.Connection, opts);

/// <summary>
/// Creates a JetStream context using the provided NATS connection.
/// </summary>
/// <param name="connection">The NATS connection used to create the JetStream context.</param>
/// <param name="opts">Context options.</param>
/// <returns>Returns an instance of <see cref="INatsJSContext"/> for interacting with JetStream.</returns>
public static INatsJSContext CreateJetStreamContext(this INatsConnection connection, NatsJSOpts opts)
=> new NatsJSContext(connection, opts);
}
5 changes: 5 additions & 0 deletions src/NATS.Client.KeyValueStore/INatsKVContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public interface INatsKVContext
/// </summary>
INatsJSContext JetStreamContext { get; }

/// <summary>
/// Configuration options for the Nats Key-Value Store operations used within the Key-Value Store context.
/// </summary>
NatsKVOpts Opts { get; }

/// <summary>
/// Create a new Key Value Store or get an existing one
/// </summary>
Expand Down
27 changes: 27 additions & 0 deletions src/NATS.Client.KeyValueStore/NatsClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,31 @@ public static INatsKVContext CreateKeyValueStoreContext(this INatsConnection con
/// <returns>An instance of <see cref="INatsKVContext"/> which can be used to interact with the Key-Value Store.</returns>
public static INatsKVContext CreateKeyValueStoreContext(this INatsJSContext context)
=> new NatsKVContext(context);

/// <summary>
/// Creates a NATS Key-Value Store context using the specified NATS client.
/// </summary>
/// <param name="client">The NATS client instance.</param>
/// <param name="opts">Context options.</param>
/// <returns>An instance of <see cref="INatsKVContext"/> which can be used to interact with the Key-Value Store.</returns>
public static INatsKVContext CreateKeyValueStoreContext(this INatsClient client, NatsKVOpts opts)
=> CreateKeyValueStoreContext(client.CreateJetStreamContext(), opts);

/// <summary>
/// Creates a NATS Key-Value Store context using the specified NATS connection.
/// </summary>
/// <param name="connection">The NATS connection instance.</param>
/// <param name="opts">Context options.</param>
/// <returns>An instance of <see cref="INatsKVContext"/> which can be used to interact with the Key-Value Store.</returns>
public static INatsKVContext CreateKeyValueStoreContext(this INatsConnection connection, NatsKVOpts opts)
=> CreateKeyValueStoreContext(connection.CreateJetStreamContext(), opts);

/// <summary>
/// Creates a NATS Key-Value Store context using the specified NATS JetStream context.
/// </summary>
/// <param name="context">The NATS JetStream context instance.</param>
/// <param name="opts">Context options.</param>
/// <returns>An instance of <see cref="INatsKVContext"/> which can be used to interact with the Key-Value Store.</returns>
public static INatsKVContext CreateKeyValueStoreContext(this INatsJSContext context, NatsKVOpts opts)
=> new NatsKVContext(context, opts);
}
14 changes: 8 additions & 6 deletions src/NATS.Client.KeyValueStore/NatsKVContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public class NatsKVContext : INatsKVContext
internal const string KvStreamNamePrefix = "KV_";
private static readonly int KvStreamNamePrefixLen = KvStreamNamePrefix.Length;
private static readonly Regex ValidBucketRegex = new(pattern: @"\A[a-zA-Z0-9_-]+\z", RegexOptions.Compiled);
private readonly NatsKVOpts _opts;

/// <summary>
/// Create a new Key Value Store context
Expand All @@ -32,7 +31,7 @@ public class NatsKVContext : INatsKVContext
public NatsKVContext(INatsJSContext context, NatsKVOpts opts)
{
JetStreamContext = context;
_opts = opts;
Opts = opts;
}

/// <summary>
Expand All @@ -47,6 +46,9 @@ public NatsKVContext(INatsJSContext context)
/// <inheritdoc />
public INatsJSContext JetStreamContext { get; }

/// <inheritdoc />
public NatsKVOpts Opts { get; }

/// <inheritdoc />
public ValueTask<INatsKVStore> CreateStoreAsync(string bucket, CancellationToken cancellationToken = default)
=> CreateStoreAsync(new NatsKVConfig(bucket), cancellationToken);
Expand All @@ -60,7 +62,7 @@ public async ValueTask<INatsKVStore> CreateStoreAsync(NatsKVConfig config, Cance

var stream = await JetStreamContext.CreateStreamAsync(streamConfig, cancellationToken);

return new NatsKVStore(config.Bucket, JetStreamContext, stream, _opts);
return new NatsKVStore(config.Bucket, JetStreamContext, stream, Opts);
}

/// <inheritdoc />
Expand All @@ -76,7 +78,7 @@ public async ValueTask<INatsKVStore> GetStoreAsync(string bucket, CancellationTo
}

// TODO: KV mirror
return new NatsKVStore(bucket, JetStreamContext, stream, _opts);
return new NatsKVStore(bucket, JetStreamContext, stream, Opts);
}

/// <inheritdoc />
Expand All @@ -88,7 +90,7 @@ public async ValueTask<INatsKVStore> UpdateStoreAsync(NatsKVConfig config, Cance

var stream = await JetStreamContext.UpdateStreamAsync(streamConfig, cancellationToken);

return new NatsKVStore(config.Bucket, JetStreamContext, stream, _opts);
return new NatsKVStore(config.Bucket, JetStreamContext, stream, Opts);
}

/// <inheritdoc />
Expand All @@ -100,7 +102,7 @@ public async ValueTask<INatsKVStore> CreateOrUpdateStoreAsync(NatsKVConfig confi

var stream = await JetStreamContext.CreateOrUpdateStreamAsync(streamConfig, cancellationToken);

return new NatsKVStore(config.Bucket, JetStreamContext, stream, _opts);
return new NatsKVStore(config.Bucket, JetStreamContext, stream, Opts);
}

/// <inheritdoc />
Expand Down
28 changes: 28 additions & 0 deletions tests/NATS.Client.JetStream.Tests/ClientExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NATS.Net;

namespace NATS.Client.JetStream.Tests;

public class ClientExtensionsTest
{
[Fact]
public void Test()
{
var opts = new NatsJSOpts(new NatsOpts(), apiPrefix: "$TEST");

var connection = new NatsConnection();
Assert.IsType<NatsJSContext>(connection.CreateJetStreamContext(), exactMatch: true);
Assert.IsType<INatsJSContext>(connection.CreateJetStreamContext(), exactMatch: false);
Assert.IsType<NatsJSContext>(connection.CreateJetStreamContext(opts), exactMatch: true);
Assert.IsType<INatsJSContext>(connection.CreateJetStreamContext(opts), exactMatch: false);
Assert.Equal("$JS.API", connection.CreateJetStreamContext().Opts.ApiPrefix);
Assert.Equal("$TEST", connection.CreateJetStreamContext(opts).Opts.ApiPrefix);

var client = new NatsClient();
Assert.IsType<NatsJSContext>(client.CreateJetStreamContext(), exactMatch: true);
Assert.IsType<INatsJSContext>(client.CreateJetStreamContext(), exactMatch: false);
Assert.IsType<NatsJSContext>(client.CreateJetStreamContext(opts), exactMatch: true);
Assert.IsType<INatsJSContext>(client.CreateJetStreamContext(opts), exactMatch: false);
Assert.Equal("$JS.API", client.CreateJetStreamContext().Opts.ApiPrefix);
Assert.Equal("$TEST", client.CreateJetStreamContext(opts).Opts.ApiPrefix);
}
}
36 changes: 36 additions & 0 deletions tests/NATS.Client.KeyValueStore.Tests/ClientExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using NATS.Net;

namespace NATS.Client.KeyValueStore.Tests;

public class ClientExtensionsTest
{
[Fact]
public void Test()
{
var opts = new NatsKVOpts { UseDirectGetApiWithKeysInSubject = true };

var connection = new NatsConnection();
Assert.IsType<NatsKVContext>(connection.CreateKeyValueStoreContext(), exactMatch: true);
Assert.IsType<INatsKVContext>(connection.CreateKeyValueStoreContext(), exactMatch: false);
Assert.IsType<NatsKVContext>(connection.CreateKeyValueStoreContext(opts), exactMatch: true);
Assert.IsType<INatsKVContext>(connection.CreateKeyValueStoreContext(opts), exactMatch: false);
Assert.False(connection.CreateKeyValueStoreContext().Opts.UseDirectGetApiWithKeysInSubject);
Assert.True(connection.CreateKeyValueStoreContext(opts).Opts.UseDirectGetApiWithKeysInSubject);

var client = new NatsClient();
Assert.IsType<NatsKVContext>(client.CreateKeyValueStoreContext(), exactMatch: true);
Assert.IsType<INatsKVContext>(client.CreateKeyValueStoreContext(), exactMatch: false);
Assert.IsType<NatsKVContext>(client.CreateKeyValueStoreContext(opts), exactMatch: true);
Assert.IsType<INatsKVContext>(client.CreateKeyValueStoreContext(opts), exactMatch: false);
Assert.False(client.CreateKeyValueStoreContext().Opts.UseDirectGetApiWithKeysInSubject);
Assert.True(client.CreateKeyValueStoreContext(opts).Opts.UseDirectGetApiWithKeysInSubject);

var js = new NatsJSContext(connection);
Assert.IsType<NatsKVContext>(js.CreateKeyValueStoreContext(), exactMatch: true);
Assert.IsType<INatsKVContext>(js.CreateKeyValueStoreContext(), exactMatch: false);
Assert.IsType<NatsKVContext>(js.CreateKeyValueStoreContext(opts), exactMatch: true);
Assert.IsType<INatsKVContext>(js.CreateKeyValueStoreContext(opts), exactMatch: false);
Assert.False(js.CreateKeyValueStoreContext().Opts.UseDirectGetApiWithKeysInSubject);
Assert.True(js.CreateKeyValueStoreContext(opts).Opts.UseDirectGetApiWithKeysInSubject);
}
}