Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StreamName.[Cc]ategory #85

Merged
merged 1 commit into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ The `Unreleased` section name is replaced by the expected version of next releas
## [Unreleased]

### Added

- `StreamName.Category` + `category`: Extracts the category portion of a streamName [#85](https://github.com/jet/FsCodec/pull/85)

### Changed
### Removed
### Fixed
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ module StreamName =
(* Splitting: functions/Active patterns for (i.e. generated via `parse`, `create` or `compose`) well-formed Stream Names
Will throw if presented with malformed strings [generated via alternate means] *)

/// Extracts the category portion of the StreamName
let category (x : StreamName) : string = ...
let (|Category|) = category

// Splits a well-formed Stream Name of the form {category}-{streamId} into its two elements.
// Throws <code>InvalidArgumentException</code> if it does not adhere to the well known format (i.e. if it was not produced by `parse`).
// <remarks>Inverse of <code>create</code>
Expand Down
7 changes: 7 additions & 0 deletions src/FsCodec/StreamName.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ module StreamName =
(* Splitting: functions/Active patterns for (i.e. generated via `parse`, `create` or `compose`) well-formed Stream Names
Will throw if presented with malformed strings [generated via alternate means] *)

/// Extracts the category portion of the StreamName
let category (x : StreamName) =
let raw = toString x
raw.Substring(0, raw.IndexOf '-')
/// Extracts the category portion of a StreamName
let (|Category|) = category

/// <summary>Splits a well-formed Stream Name of the form <c>{category}-{streamId}</c> into its two elements.<br/>
/// Throws <c>InvalidArgumentException</c> if it does not adhere to the well known format (i.e. if it was not produced by `parse`).</summary>
/// <remarks>Inverse of <c>create</c></remarks>
Expand Down
6 changes: 3 additions & 3 deletions tests/FsCodec.NewtonsoftJson.Tests/Examples.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ module Events =

/// Pattern to determine whether a given {category}-{streamId} StreamName represents the stream associated with this Aggregate
/// Yields a strongly typed id from the streamId if the Category does match
let (|StreamName|_|) = function
| FsCodec.StreamName.CategoryAndId (Category, ClientId.Parse clientId) -> Some clientId
| _ -> None
let [<return: Struct>] (|StreamName|_|) = function
| FsCodec.StreamName.CategoryAndId (Category, ClientId.Parse clientId) -> ValueSome clientId
| _ -> ValueNone

type Added = { item : string }
type Removed = { name : string }
Expand Down
6 changes: 3 additions & 3 deletions tests/FsCodec.SystemTextJson.Tests/Examples.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ module Events =

/// Pattern to determine whether a given {category}-{streamId} StreamName represents the stream associated with this Aggregate
/// Yields a strongly typed id from the streamId if the Category does match
let (|StreamName|_|) = function
| FsCodec.StreamName.CategoryAndId (Category, ClientId.Parse clientId) -> Some clientId
| _ -> None
let [<return: Struct>] (|StreamName|_|) = function
| FsCodec.StreamName.CategoryAndId (Category, ClientId.Parse clientId) -> ValueSome clientId
| _ -> ValueNone

type Added = { item : string }
type Removed = { name : string }
Expand Down