public override ApiTaskResult <TResult, TFailureResult> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { using var jsonDocument = JsonDocument.ParseValue(ref reader); var taskId = jsonDocument.RootElement.GetProperty(propNames.IdPropName.AsUtf8()).GetGuid(); var taskStateValue = jsonDocument.RootElement.GetProperty(propNames.TaskStatePropName.AsUtf8()).GetString(); var taskState = (ApiTaskState)Enum.Parse(typeof(ApiTaskState), taskStateValue !, true); var taskTypeValue = jsonDocument.RootElement.GetProperty(propNames.TaskTypePropName.AsUtf8()).GetString(); var taskType = taskTypeValue == null ? null : Urn.Parse(taskTypeValue); switch (taskState) { case ApiTaskState.Running: return(ApiTaskResult <TResult, TFailureResult> .Running(taskId, taskType)); case ApiTaskState.Succeed: return(DeserializeResult(jsonDocument, taskStateValue, taskId, taskType, options)); case ApiTaskState.Failed: var errorJson = jsonDocument.RootElement.GetProperty(propNames.ApiErrorPropName.AsUtf8()).GetRawText(); var error = DeserializeAs <ApiError>(errorJson, propNames.ApiErrorPropName, taskStateValue, options); return(ApiTaskResult <TResult, TFailureResult> .TaskFailure(error, taskId, taskType)); default: throw Errors.UnexpectedEnumMember(nameof(taskState), taskState); } }
public void Running_should_create_result_in_the_running_state() { var id = Guid.NewGuid(); var taskType = new Urn("nid", "nss"); var expectedDto = new ExpectedResult { Id = id, TaskType = taskType, TaskState = ApiTaskState.Running }; var result = ApiTaskResult <SuccessResult, FailureResult> .Running(id, taskType); result.Should().BeEquivalentTo(expectedDto); }
public void ConvertToSingleApiResult_should_convert_running_result_to_running_result() { var id = Guid.NewGuid(); var taskType = new Urn("nid", "nss"); var expectedDto = new ExpectedResult { Id = id, TaskType = taskType, TaskState = ApiTaskState.Running }; var taskResult = ApiTaskResult <SuccessResult, FailureResult> .Running(id, taskType); var oneResultApiResult = taskResult.ConvertToSingleApiResult( x => x.Value, x => new ApiError(HttpStatusCode.Forbidden, x.Value.ToString()) ); oneResultApiResult.Should().BeEquivalentTo(expectedDto); oneResultApiResult.TryGetSuccessResult(out _).Should().BeFalse(); }
public override ApiTaskResult <TResult> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { using var jsonDocument = JsonDocument.ParseValue(ref reader); var taskId = jsonDocument.RootElement.GetProperty(propNames.IdPropName.AsUtf8()).GetGuid(); var taskStateValue = jsonDocument.RootElement.GetProperty(propNames.TaskStatePropName.AsUtf8()).GetString(); var taskState = (ApiTaskState)Enum.Parse(typeof(ApiTaskState), taskStateValue !, true); var taskTypeValue = jsonDocument.RootElement.GetProperty(propNames.TaskTypePropName.AsUtf8()).GetString(); var taskType = taskTypeValue == null ? null : Urn.Parse(taskTypeValue); switch (taskState) { case ApiTaskState.Running: return(ApiTaskResult <TResult> .Running(taskId, taskType)); case ApiTaskState.Succeed: var resultJson = jsonDocument.RootElement.GetProperty(propNames.TaskResultPropName.AsUtf8()).GetRawText(); var successResult = DeserializeAs <TResult>(resultJson, propNames.TaskResultPropName, taskStateValue); return(ApiTaskResult <TResult> .Success(successResult, taskId, taskType)); case ApiTaskState.Failed: var errorJson = jsonDocument.RootElement.GetProperty(propNames.ApiErrorPropName.AsUtf8()).GetRawText(); var error = DeserializeAs <ApiError>(errorJson, propNames.ApiErrorPropName, taskStateValue); return(ApiTaskResult <TResult> .TaskFailure(error, taskId, taskType)); default: throw Errors.UnexpectedEnumMember(nameof(taskState), taskState); } T DeserializeAs <T>(string json, in Utf8String propName, string?taskStatePropValue) { return(JsonSerializer.Deserialize <T>(json, options) ?? throw Errors.JsonPropertyCannotBeNullIfAnotherPropertyHasValue( propName.ToString(), propNames.TaskStatePropName.ToString(), taskStatePropValue)); } }