Skip to content

Commit

Permalink
Subject Transforms in Mirror/Info and Source/Info (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Sep 26, 2023
1 parent 4f63aaa commit 45f0dab
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 373 deletions.
1 change: 1 addition & 0 deletions src/NATS.Client/JetStream/ApiConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ internal static class ApiConstants
internal const string Streams = "streams";
internal const string Subject = "subject";
internal const string SubjectTransform = "subject_transform";
internal const string SubjectTransforms = "subject_transforms";
internal const string Subjects = "subjects";
internal const string SubjectsFilter = "subjects_filter";
internal const string Success = "success";
Expand Down
186 changes: 16 additions & 170 deletions src/NATS.Client/JetStream/Mirror.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,16 @@
// limitations under the License.

using System;
using NATS.Client.Internals;
using System.Collections.Generic;
using NATS.Client.Internals.SimpleJSON;

namespace NATS.Client.JetStream
{
/// <summary>
/// Information about a mirror.
/// </summary>
public sealed class Mirror : JsonSerializable
public sealed class Mirror : SourceBase
{
/// <summary>
/// Mirror stream name.
/// </summary>
public string Name { get; }

/// <summary>
/// The sequence to start replicating from.
/// </summary>
public ulong StartSeq { get; }

/// <summary>
/// The time stamp to start replicating from.
/// </summary>
public DateTime StartTime { get; }

/// <summary>
/// The subject filter to replicate
/// </summary>
public string FilterSubject { get; }

/// <summary>
/// External stream reference
/// </summary>
public External External { get; }

internal Mirror(JSONNode mirrorBaseNode)
{
Name = mirrorBaseNode[ApiConstants.Name].Value;
StartSeq = mirrorBaseNode[ApiConstants.OptStartSeq].AsUlong;
StartTime = JsonUtils.AsDate(mirrorBaseNode[ApiConstants.OptStartTime]);
FilterSubject = mirrorBaseNode[ApiConstants.FilterSubject].Value;
External = External.OptionalInstance(mirrorBaseNode[ApiConstants.External]);
}

public override JSONNode ToJsonNode()
{
JSONObject o = new JSONObject();
JsonUtils.AddField(o, ApiConstants.Name, Name);
JsonUtils.AddField(o, ApiConstants.OptStartSeq, StartSeq);
JsonUtils.AddField(o, ApiConstants.OptStartTime, JsonUtils.ToString(StartTime));
JsonUtils.AddField(o, ApiConstants.FilterSubject, FilterSubject);
if (External != null)
{
o[ApiConstants.External] = External.ToJsonNode();
}
return o;
}

internal static Mirror OptionalInstance(JSONNode mirrorNode)
{
return mirrorNode == null || mirrorNode.Count == 0 ? null : new Mirror(mirrorNode);
Expand All @@ -83,14 +35,14 @@ internal static Mirror OptionalInstance(JSONNode mirrorNode)
/// <param name="startTime">the start time</param>
/// <param name="filterSubject">the filter subject</param>
/// <param name="external">the external reference</param>
public Mirror(string name, ulong startSeq, DateTime startTime, string filterSubject, External external)
{
Name = name;
StartSeq = startSeq;
StartTime = startTime;
FilterSubject = filterSubject;
External = external;
}
/// <param name="subjectTransforms">the subject transforms, defaults to none</param>
public Mirror(string name, ulong startSeq, DateTime startTime, string filterSubject, External external,
IList<SubjectTransform> subjectTransforms = null)
: base(name, startSeq, startTime, filterSubject, external, subjectTransforms) {}

internal Mirror(JSONNode mirrorNode) : base(mirrorNode) {}

internal Mirror(MirrorBuilder mb) : base(mb) {}

/// <summary>
/// Creates a builder for a mirror object.
Expand All @@ -111,126 +63,20 @@ public static MirrorBuilder Builder(Mirror mirror) {
/// <summary>
/// Mirror can be created using a MirrorBuilder.
/// </summary>
public sealed class MirrorBuilder
public sealed class MirrorBuilder : SourceBaseBuilder<MirrorBuilder, Mirror>
{
private string _name;
private ulong _startSeq;
private DateTime _startTime;
private string _filterSubject;
private External _external;

public MirrorBuilder() { }

public MirrorBuilder(Mirror mirror)
{
_name = mirror.Name;
_startSeq = mirror.StartSeq;
_startTime = mirror.StartTime;
_filterSubject = mirror.FilterSubject;
_external = mirror.External;
}
public MirrorBuilder() {}
public MirrorBuilder(Mirror mirror) : base(mirror) {}

/// <summary>
/// Set the mirror name.
/// </summary>
/// <param name="name">the name</param>
/// <returns>The Builder</returns>
public MirrorBuilder WithName(string name)
protected override MirrorBuilder GetThis()
{
_name = name;
return this;
}

/// <summary>
/// Set the start sequence.
/// </summary>
/// <param name="startSeq">the start sequence</param>
/// <returns>The Builder</returns>
public MirrorBuilder WithStartSeq(ulong startSeq)
public override Mirror Build()
{
_startSeq = startSeq;
return this;
}

/// <summary>
/// Set the start time.
/// </summary>
/// <param name="startTime">the start time</param>
/// <returns>The Builder</returns>
public MirrorBuilder WithStartTime(DateTime startTime)
{
_startTime = startTime;
return this;
}

/// <summary>
/// Set the filter subject.
/// </summary>
/// <param name="filterSubject">the filterSubject</param>
/// <returns>The Builder</returns>
public MirrorBuilder WithFilterSubject(string filterSubject)
{
_filterSubject = filterSubject;
return this;
}

/// <summary>
/// Set the external reference.
/// </summary>
/// <param name="external">the external</param>
/// <returns>The Builder</returns>
public MirrorBuilder WithExternal(External external)
{
_external = external;
return this;
}

/// <summary>
/// Set the external reference by using a domain based prefix.
/// </summary>
/// <param name="domain">the domain</param>
/// <returns>The Builder</returns>
public MirrorBuilder WithDomain(string domain)
{
string prefix = JetStreamOptions.ConvertDomainToPrefix(domain);
_external = prefix == null ? null : External.Builder().WithApi(prefix).Build();
return this;
}

/// <summary>
/// Build a Mirror object
/// </summary>
/// <returns>The Mirror</returns>
public Mirror Build()
{
return new Mirror(_name, _startSeq, _startTime, _filterSubject, _external);
return new Mirror(this);
}
}

public bool Equals(Mirror other)
{
return Name == other.Name && StartSeq == other.StartSeq && StartTime.Equals(other.StartTime) && FilterSubject == other.FilterSubject && Equals(External, other.External);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Mirror) obj);
}

public override int GetHashCode()
{
unchecked
{
var hashCode = (Name != null ? Name.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ StartSeq.GetHashCode();
hashCode = (hashCode * 397) ^ StartTime.GetHashCode();
hashCode = (hashCode * 397) ^ (FilterSubject != null ? FilterSubject.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (External != null ? External.GetHashCode() : 0);
return hashCode;
}
}
}
}
Loading

0 comments on commit 45f0dab

Please sign in to comment.