Skip to content

Commit

Permalink
Change WithRoute() to accept unmarshaling options.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Apr 25, 2023
1 parent 8d68699 commit 4badc2b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 17 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ The format is based on [Keep a Changelog], and this project adheres to
[keep a changelog]: https://keepachangelog.com/en/1.0.0/
[semantic versioning]: https://semver.org/spec/v2.0.0.html

## [0.8.2] - 2023-04-26

### Changed

- Change `WithRoute()` to accept unmarshaling options

## [0.8.1] - 2023-04-25

### Added
Expand Down Expand Up @@ -150,6 +156,10 @@ if err != nil {
[0.6.0]: https://github.com/dogmatiq/harpy/releases/tag/v0.6.0
[0.6.1]: https://github.com/dogmatiq/harpy/releases/tag/v0.6.1
[0.6.2]: https://github.com/dogmatiq/harpy/releases/tag/v0.6.2
[0.7.0]: https://github.com/dogmatiq/harpy/releases/tag/v0.7.0
[0.8.0]: https://github.com/dogmatiq/harpy/releases/tag/v0.8.0
[0.8.1]: https://github.com/dogmatiq/harpy/releases/tag/v0.8.1
[0.8.2]: https://github.com/dogmatiq/harpy/releases/tag/v0.8.2

<!-- version template
## [0.0.1] - YYYY-MM-DD
Expand Down
18 changes: 5 additions & 13 deletions internal/jsonx/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import (
)

// Decode unmarshals JSON content from r into v.
func Decode[O ~UnmarshalOption](
r io.Reader,
v any,
options ...O,
) error {
func Decode(r io.Reader, v any, options ...UnmarshalOption) error {
var opts UnmarshalOptions
for _, fn := range options {
fn(&opts)
Expand All @@ -26,22 +22,18 @@ func Decode[O ~UnmarshalOption](
}

// Unmarshal unmarshals JSON content from data into v.
func Unmarshal[O ~UnmarshalOption](
data []byte,
v any,
options ...O,
) error {
func Unmarshal(data []byte, v any, options ...UnmarshalOption) error {
return Decode(
bytes.NewReader(data),
v,
options...,
)
}

// UnmarshalOption is an option that changes the behavior of JSON unmarshaling.
type UnmarshalOption func(*UnmarshalOptions)

// UnmarshalOptions is a set of options that control how JSON is unmarshaled.
type UnmarshalOptions struct {
AllowUnknownFields bool
}

// UnmarshalOption is a function that changes the behavior of JSON unmarshaling.
type UnmarshalOption = func(*UnmarshalOptions)
2 changes: 1 addition & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// UnmarshalOption is an option that changes the behavior of JSON unmarshaling.
type UnmarshalOption func(*jsonx.UnmarshalOptions)
type UnmarshalOption = jsonx.UnmarshalOption

// AllowUnknownFields is an UnmarshalOption that controls whether parameters,
// results and error data may contain unknown fields.
Expand Down
2 changes: 1 addition & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func unmarshalBatchRequest(r *bufio.Reader) (RequestSet, error) {
// unmarshalJSONForRequest unmarshals JSON content from r into v. If the JSON
// cannot be parsed it returns a JSON-RPC error with the "parse error" code.
func unmarshalJSONForRequest(r io.Reader, v any) error {
err := jsonx.Decode[jsonx.UnmarshalOption](r, v)
err := jsonx.Decode(r, v)

if jsonx.IsParseError(err) {
return NewErrorWithReservedCode(
Expand Down
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func unmarshalBatchResponse(r *bufio.Reader) (ResponseSet, error) {

// unmarshalJSONForResponse unmarshals JSON content from r into v.
func unmarshalJSONForResponse(r io.Reader, v any) error {
err := jsonx.Decode[jsonx.UnmarshalOption](r, v)
err := jsonx.Decode(r, v)

if jsonx.IsParseError(err) {
return fmt.Errorf("unable to parse response: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ type RouterOption func(*Router)
func WithRoute[P, R any](
m string,
h func(context.Context, P) (R, error),
options ...UnmarshalOption,
) RouterOption {
return WithUntypedRoute(
m,
func(ctx context.Context, req Request) (any, error) {
var params P
if err := req.UnmarshalParameters(&params); err != nil {
if err := req.UnmarshalParameters(&params, options...); err != nil {
return nil, err
}

Expand Down
24 changes: 24 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ var _ = Describe("type Router", func() {
Expect(called).To(BeTrue())
})

It("supports unmarshal options (via WithRoute())", func() {
called := false
request.Parameters = json.RawMessage(`{"Value": 123, "Unknown": 456}`)

type Params struct {
Value int
}

router = NewRouter(
WithRoute(
"<method>",
func(ctx context.Context, params Params) (any, error) {
called = true
Expect(params).To(Equal(Params{Value: 123}))
return nil, nil
},
AllowUnknownFields(true),
),
)

router.Call(context.Background(), request)
Expect(called).To(BeTrue())
})

It("allows calls to handlers that don't return a result (via NoResult())", func() {
called := false

Expand Down

0 comments on commit 4badc2b

Please sign in to comment.