Skip to content

Commit

Permalink
Add StreamName.[Cc]ategory
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Sep 7, 2022
1 parent 06dc490 commit bb53689
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
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 [#84](https://github.com/jet/FsCodec/pull/84)

### 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

0 comments on commit bb53689

Please sign in to comment.