public static RowParser <IMaybe <T>, IDataRecord> Optional <T>(RowParser <T, IDataRecord> other) { return(new RowParser <IMaybe <T>, IDataRecord>(row => { return other.Parse(row).Map(Maybe.Just).Recover(ex => { if (ex == UnexpectedNullFieldException.UnexpectedNull) { return RowParserResult.Successful(Maybe.Nothing <T>()); } return RowParserResult.Failed <IMaybe <T> >(ex); }); })); }
internal static async Task <T> ParseSingle <T>(DbDataReader result, RowParser <T, IDataRecord> parser, CancellationToken ct) { var success = await result.ReadAsync(ct); if (!success) { throw new DataException("Expected a single value, but got nothing!"); } var parseResult = parser.Parse(result).Value; success = await result.ReadAsync(ct); if (success) { throw new DataException("Expected a single value, but got more than that!"); } return(parseResult); }