-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
151 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package jsonx contains utilities for dealing with JSON and the encoding/json | ||
// package. | ||
package jsonx |
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,25 @@ | ||
package jsonx | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
// IsParseError returns true if err indicates a JSON parse failure of some kind. | ||
func IsParseError(err error) bool { | ||
switch err.(type) { | ||
case nil: | ||
return false | ||
case *json.SyntaxError: | ||
return true | ||
case *json.UnmarshalTypeError: | ||
return true | ||
default: | ||
// Unfortunately, some JSON errors do not have distinct types. For | ||
// example, when parsing using a decoder with DisallowUnknownFields() | ||
// enabled an unexpected field is reported using the equivalent of: | ||
// | ||
// errors.New(`json: unknown field "<field name>"`) | ||
return strings.HasPrefix(err.Error(), "json:") | ||
} | ||
} |
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,47 @@ | ||
package jsonx | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
) | ||
|
||
// Decode unmarshals JSON content from r into v. | ||
func Decode[O ~UnmarshalOption]( | ||
r io.Reader, | ||
v any, | ||
options ...O, | ||
) error { | ||
var opts UnmarshalOptions | ||
for _, fn := range options { | ||
fn(&opts) | ||
} | ||
|
||
dec := json.NewDecoder(r) | ||
if !opts.AllowUnknownFields { | ||
dec.DisallowUnknownFields() | ||
} | ||
|
||
return dec.Decode(&v) | ||
} | ||
|
||
// Unmarshal unmarshals JSON content from data into v. | ||
func Unmarshal[O ~UnmarshalOption]( | ||
data []byte, | ||
v any, | ||
options ...O, | ||
) error { | ||
return Decode( | ||
bytes.NewReader(data), | ||
v, | ||
options..., | ||
) | ||
} | ||
|
||
// 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) |
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,35 +1,18 @@ | ||
package harpy | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"strings" | ||
"github.com/dogmatiq/harpy/internal/jsonx" | ||
) | ||
|
||
// isJSONError returns true if err indicates a JSON parse failure of some kind. | ||
func isJSONError(err error) bool { | ||
switch err.(type) { | ||
case nil: | ||
return false | ||
case *json.SyntaxError: | ||
return true | ||
case *json.UnmarshalTypeError: | ||
return true | ||
default: | ||
// Unfortunately, some JSON errors do not have distinct types. For | ||
// example, when parsing using a decoder with DisallowUnknownFields() | ||
// enabled an unexpected field is reported using the equivalent of: | ||
// | ||
// errors.New(`json: unknown field "<field name>"`) | ||
return strings.HasPrefix(err.Error(), "json:") | ||
} | ||
} | ||
// UnmarshalOption is an option that changes the behavior of JSON unmarshaling. | ||
type UnmarshalOption func(*jsonx.UnmarshalOptions) | ||
|
||
// unmarshalJSON unmarshals JSON content from r into v. The main reason for this | ||
// function is to disallow unknown fields. | ||
func unmarshalJSON(r io.Reader, v any) error { | ||
dec := json.NewDecoder(r) | ||
dec.DisallowUnknownFields() | ||
|
||
return dec.Decode(&v) | ||
// AllowUnknownFields is an UnmarshalOption that controls whether parameters, | ||
// results and error data may contain unknown fields. | ||
// | ||
// Unknown fields are disallowed by default. | ||
func AllowUnknownFields(allow bool) UnmarshalOption { | ||
return func(opts *jsonx.UnmarshalOptions) { | ||
opts.AllowUnknownFields = allow | ||
} | ||
} |
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