Skip to content

Commit

Permalink
Bumped to 2.9.5
Browse files Browse the repository at this point in the history
Fixed an issue where StatusCode was not included in FromOutcome. Changed this so that StatusCode is now backed by the Keys dictionary. Since StatusCode is a property, it should still be serialized the same way, so this is not a breaking  change for the edge case where an Outcome is serialized to Json and StatusCode is expected.
  • Loading branch information
Brian MacKay committed Oct 9, 2018
1 parent bdbbbb5 commit 4b96527
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
11 changes: 9 additions & 2 deletions Outcomes.Tests/SuccessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ public void Success_WithKeysFrom_Works()
public void Success_FromOutcome_Persists_Values()
{
var outcome1 = Outcomes.Success<int>()
.WithValue(10);
.WithValue(10)
.WithStatusCode(505);


var outcome2 = Outcomes.Success()
.WithValue(20);
.WithValue(20)
.WithStatusCode(null);

//In this case, some casting is going to happen.
var outcome3 = Outcomes.Success<ExampleConcrete>()
.WithValue(new ExampleConcrete() { SomeInt = 0, SomeString = "not important" });


//In this case, there's a null value.
var outcome4 = Outcomes.Success<ExampleConcrete>()
Expand All @@ -97,8 +101,11 @@ public void Success_FromOutcome_Persists_Values()
var from5 = Outcomes.Success<ExampleBase>().FromOutcome(outcome5);

Assert.True(from1.Value.Equals(10));
Assert.True(from1.StatusCode == 505);
Assert.True(from2.Value.Equals(20));
Assert.True(from2.StatusCode == null);
Assert.True(from3.Value.SomeString == "not important");
Assert.True(from3.StatusCode == null); // not set, should be null
Assert.True(from4.Value == null);
Assert.True(from5.Value == null);
}
Expand Down
2 changes: 1 addition & 1 deletion Outcomes/Builder/FailureOutcomeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public IFailureOutcomeBuilder<TValue> FromException(Exception exception)
return this;
}

[Obsolete("This will eventually be replaced by the new Keys dictionary.")]
[Obsolete("Considering removing this in favor of the Keys dictionary, pending community feedback.")]
public new IFailureOutcomeBuilder<TValue> WithStatusCode(int? statusCode)
{
base.WithStatusCode(statusCode);
Expand Down
4 changes: 2 additions & 2 deletions Outcomes/Builder/SuccessOutcomeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public SuccessOutcomeBuilder<TValue> WithMessage(string message)
/// <summary>
/// Append a list of strings to the end of the outcome's message collection.
/// </summary>
/// <param name="messages">Enum of srings to add.</param>
/// <param name="messages">Enum of strings to add.</param>
/// <returns></returns>
public SuccessOutcomeBuilder<TValue> WithMessage(IEnumerable<string> messages)
{
Expand Down Expand Up @@ -91,7 +91,7 @@ public SuccessOutcomeBuilder<TValue> WithValue(TValue value)
/// (optional) Sets the StatusCode, which is an additional piece of metadata you can use for your own purposes.
/// This is handy when there could be, for instance, multiple failure modes.
/// </summary>
[Obsolete("This will eventually be replaced by the new Keys dictionary.")]
[Obsolete("Considering removing this in favor of the Keys dictionary, pending community feedback.")]
public SuccessOutcomeBuilder<TValue> WithStatusCode(int? statusCode)
{
base.StatusCode = statusCode;
Expand Down
32 changes: 30 additions & 2 deletions Outcomes/OutcomeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,36 @@ public class OutcomeResult<TValue> : IOutcome<TValue>
public bool Success { get; protected set; }
public List<string> Messages { get; protected set; }
public TValue Value { get; set; }
public int? StatusCode { get; protected set; }

/// <summary>
/// Syntactic sugar for Keys.ContainsKey["StatusCode"]. Guarantees that StatusCode will be a nullable
/// int. If it is not, because Keys["StatusCode"] was set manually to something strange, it will return null.
/// </summary>
[Obsolete("Considering removing this in favor of the Keys dictionary, pending community feedback.")]
public int? StatusCode
{
get
{
// Since the backing field is in a Dictionary<object>, we have to careful coerce
// it into a nullable int.
if (!Keys.ContainsKey("StatusCode"))
return null;

var statusCode = Keys["StatusCode"];

if (statusCode == null)
return null;

var statusCodeString = statusCode.ToString();

if (int.TryParse(statusCodeString, out var value))
return value;
else
return null;
}
protected set => Keys["StatusCode"] = value;
}

public Dictionary<string, object> Keys { get; }
public bool Failure => !Success;

Expand All @@ -30,7 +59,6 @@ internal OutcomeResult(bool success)
Success = success;
Messages = new List<string>();
Value = default(TValue);
StatusCode = null;
Keys = new Dictionary<string, object>();
}

Expand Down
4 changes: 2 additions & 2 deletions Outcomes/Outcomes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
<AssemblyName>Ether.Outcomes</AssemblyName>
<PackageId>Ether.Outcomes</PackageId>
<PackageTags>c#;.net;notification;fluent;response;failure;success</PackageTags>
<PackageReleaseNotes>Fixed an issue where, on the .Net Standard 1.3 target, FromOutcome() was not copying the value and the test wasn't catching it.</PackageReleaseNotes>
<PackageReleaseNotes>Fixed an issue where StatusCode was not included in FromOutcome. Changed this so that StatusCode is now backed by the Keys dictionary. Since StatusCode is a property, it should still be serialized the same way, so this is not a breaking change for the edge case where an Outcome is serialized to Json and StatusCode is expected.</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/kinetiq/Ether.Outcomes</PackageProjectUrl>
<PackageLicenseUrl>http://choosealicense.com/licenses/mit/</PackageLicenseUrl>
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.3' ">$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.0</NetStandardImplicitPackageVersion>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<Version>2.9.4-beta</Version>
<Version>2.9.5</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Brian MacKay</Authors>
</PropertyGroup>
Expand Down

0 comments on commit 4b96527

Please sign in to comment.