/// <summary> /// Fetches a business data snapshot from blob storage. /// </summary> /// <param name="cancellationToken">the CancellationToken.</param> /// <returns>the most recent snapshot.</returns> public async Task <FSharpResult <BusinessData <TBusinessData>, BusinessDataUpdateError> > FetchBusinessDataSnapshot(CancellationToken cancellationToken) { try { FSharpOption <(Watermark, string)> someWatermarkAndName = await this.GetLatestSnapshotID(cancellationToken); if (FSharpOption <(Watermark, string)> .get_IsNone(someWatermarkAndName)) { var emptyBusinessData = new BusinessData <TBusinessData>( data: this.createEmptyBusinessData(), watermark: Watermark.NewWatermark(-1)); return(FSharpResult <BusinessData <TBusinessData>, BusinessDataUpdateError> .NewOk(emptyBusinessData)); } var(watermark, blobName) = someWatermarkAndName.Value; await Console.Out.WriteLineAsync($"Loading snapshot watermark {watermark.Item} from {blobName}"); var blobClient = this.snapshotContainerClient.GetBlobClient(blobName: blobName); var result = await blobClient.DownloadAsync(cancellationToken : cancellationToken); var val = await result.Value.Content.ReadJSON <BusinessData <TBusinessData> >(); return(FSharpResult <BusinessData <TBusinessData>, BusinessDataUpdateError> .NewOk(val)); } catch (Exception ex) { return(FSharpResult <BusinessData <TBusinessData>, BusinessDataUpdateError> .NewError( BusinessDataUpdateError.NewSnapshotDownloadError(ex))); } }
public void Select2() { var a = FSharpResult.NewError <int, string>("hello"); var b = a.Select(i => i + 2); b.Match(_ => Assert.Fail("is int"), s => Assert.AreEqual("hello", s)); }
/// <summary> /// Converts a compile-time unknown <see cref="FSharpResult{T, TError}"/> into an <c>FSharpResult<TResult, string></c>. /// </summary> /// <typeparam name="TResult">The type of result you expect.</typeparam> /// <param name="result">An unknown instance of a <see cref="FSharpResult{T, TError}"/></param> /// <returns>An <c>FSharpResult<TResult, string></c> where <typeparamref name="TResult"/> is your expected result type and the error is a <see cref="string"/></returns> /// <exception cref="ArgumentNullException">The result is null.</exception> /// <exception cref="NotSupportedException">The result is not one of the supported conversions.</exception> public static FSharpResult <TResult, string> ResultFromFSharp <TResult>(this object result) { if (result is null) { throw new ArgumentNullException(nameof(result)); } switch (result) { case FSharpResult <TResult, string> typedResult: return(typedResult); case FSharpResult <TResult, object> typedResult: if (typedResult.IsOk) { return(FSharpResult <TResult, string> .NewOk(typedResult.ResultValue)); } else { throw new NotSupportedException($"Error value is not a 'string' it is a '{typedResult.ErrorValue.GetType().Name}'."); } case FSharpResult <object, string> typedResult: if (typedResult.IsError) { return(FSharpResult <TResult, string> .NewError(typedResult.ErrorValue)); } else { throw new NotSupportedException($"Result value is not a '{typeof(TResult).Name}' it is a '{typedResult.ErrorValue.GetType().Name}'."); } default: throw new NotSupportedException($"Unknown result type '{result.GetType().Name}'."); } }
public void CanConvertToAndFromAnFSharpResult() { Error <int, string>("didn't work").ToFs().ShouldBe(FSharpResult <int, string> .NewError("didn't work")); Ok <int, string>(10).ToFs().ShouldBe(FSharpResult <int, string> .NewOk(10)); Result.FromFs(Error <int, string>("didn't work").ToFs()).ShouldBe(Error <int, string>("didn't work")); Result.FromFs(Ok <int, string>(10).ToFs()).ShouldBe(Ok <int, string>(10)); }
// public string Name; // public Visa.Status StatusC; // public Person.Person Person; // public PersonC2(Person.Person person){ // Name = person.Name; // StatusC = person.Status; // } // public Visa.Status status2 = Visa.Status.Student; public static FSharpResult <Person.Person, string> ValidateStudentC(Person.Person person) { if (person.Status == Visa.Status.Student) { return(FSharpResult <Person.Person, string> .NewOk(person)); } else { return(FSharpResult <Person.Person, string> .NewError("Tourist")); } }
public static FSharpResult <TOk1, TError> Map <TOk, TError, TOk1>(this FSharpResult <TOk, TError> result, Func <TOk, TOk1> continuation) { if (result.IsError) { return(FSharpResult <TOk1, TError> .NewError(result.ErrorValue)); } else { return(FSharpResult <TOk1, TError> .NewOk(continuation(result.ResultValue))); } }
public FSharpResult <decimal, string> WorkflowStep2(int input) { if (input % 3 == 0) { return(FSharpResult <decimal, string> .NewError("Sholud not be dividable by 3")); } else { var result = input * 100.0m / 356; return(FSharpResult <decimal, string> .NewOk(result)); } }
public FSharpResult <int, string> WorkflowStep1(int input) { if (input % 2 == 0) { return(FSharpResult <int, string> .NewError("Sholud be odd")); } else { var result = input * 356; return(FSharpResult <int, string> .NewOk(result)); } }
public void New() { var a = FSharpResult.NewOk <int, string>(1); var b = FSharpResult <int, string> .NewOk(1); Assert.AreEqual(a, b); var c = FSharpResult.NewError <int, string>("a"); var d = FSharpResult <int, string> .NewError("a"); Assert.AreEqual(c, d); }
public void Controller_Error_Is_Thrown() { sut.ModelUpdateRequired += delegate(Action <IPreferencesStateController> action) { var prefsController = Mock.Of <IPreferencesStateController>(); Mock.Get(prefsController) .Setup(x => x.SetEnginesPathConfig(It.IsAny <string>())) .Returns(FSharpResult <Unit, string> .NewError("Something went very very wrong!")); action(prefsController); }; sut.GeneralConfig.EnginesPathConfig.SetValue(new DirectoryInfo("path/to/exile")); Assert.That(thrownErrors.Count, Is.EqualTo(1)); Assert.That(thrownErrors[0].Message, Is.EqualTo("Something went very very wrong!")); }