-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
js-adapters: Better result type (#4274)
Adds convenience functions for creating result types: `err` and `ok`, similar to Rust ones. Adds `map` and `flatMap` methods on `Result` type for easier way to work with a `Result` on a happy path. Split from #4213
- Loading branch information
Serhii Tatarintsev
authored
Sep 22, 2023
1 parent
818b9fc
commit c78977b
Showing
7 changed files
with
88 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
query-engine/driver-adapters/js/driver-adapter-utils/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { bindAdapter } from './binder' | ||
export { ColumnTypeEnum } from './const' | ||
export { Debug } from './debug' | ||
export { ok, err, type Result } from './result' | ||
export type * from './types' |
41 changes: 41 additions & 0 deletions
41
query-engine/driver-adapters/js/driver-adapter-utils/src/result.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Error } from './types' | ||
export type Result<T> = { | ||
// common methods | ||
map<U>(fn: (value: T) => U): Result<U> | ||
flatMap<U>(fn: (value: T) => Result<U>): Result<U> | ||
} & ( | ||
| { | ||
readonly ok: true | ||
readonly value: T | ||
} | ||
| { | ||
readonly ok: false | ||
readonly error: Error | ||
} | ||
) | ||
|
||
export function ok<T>(value: T): Result<T> { | ||
return { | ||
ok: true, | ||
value, | ||
map(fn) { | ||
return ok(fn(value)) | ||
}, | ||
flatMap(fn) { | ||
return fn(value) | ||
}, | ||
} | ||
} | ||
|
||
export function err<T>(error: Error): Result<T> { | ||
return { | ||
ok: false, | ||
error, | ||
map() { | ||
return err(error) | ||
}, | ||
flatMap() { | ||
return err(error) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters