Skip to content

Commit

Permalink
feat(configio): remove public references to representation
Browse files Browse the repository at this point in the history
- remove Decode methods from interface Decoder and its implementations
- rename DecodeRunner methods to Decode
- remove UnmarshalXXXX functions
- rename UnmarshalXXXXRunner functions to UnmarshalXXXX
  • Loading branch information
GregoryAlbouy committed Mar 28, 2023
1 parent 313f8c4 commit 39fb4a6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 54 deletions.
2 changes: 1 addition & 1 deletion configio/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (b *Builder) WriteYAML(in []byte) error {

func (b *Builder) decodeAndWrite(in []byte, format Format) error {
repr := representation{}
if err := DecoderOf(format, in).Decode(&repr); err != nil {
if err := decoderOf(format, in).decodeRepr(&repr); err != nil {
return err
}
// early check for invalid configuration
Expand Down
18 changes: 13 additions & 5 deletions configio/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@ import (
"github.com/benchttp/sdk/benchttp"
)

type Decoder interface {
Decode(dst *representation) error
DecodeRunner(dst *benchttp.Runner) error
}

type Format string

const (
FormatJSON Format = "json"
FormatYAML Format = "yaml"
)

type Decoder interface {
Decode(dst *benchttp.Runner) error
}

// DecoderOf returns the appropriate Decoder for the given Format.
// It panics if the format is not a Format declared in configio.
func DecoderOf(format Format, in []byte) Decoder {
return decoderOf(format, in)
}

type decoder interface {
Decoder
decodeRepr(dst *representation) error
}

func decoderOf(format Format, in []byte) decoder {
r := bytes.NewReader(in)
switch format {
case FormatYAML:
Expand Down
2 changes: 1 addition & 1 deletion configio/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (f *file) decode() (err error) {
return err
}

if err := DecoderOf(ext, b).Decode(&f.repr); err != nil {
if err := decoderOf(ext, b).decodeRepr(&f.repr); err != nil {
return errorutil.WithDetails(ErrFileParse, f.path, err)
}

Expand Down
39 changes: 17 additions & 22 deletions configio/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,40 @@ import (
"github.com/benchttp/sdk/benchttp"
)

// UnmarshalJSON parses the JSON-encoded data and stores the result
// in the Representation pointed to by dst.
func UnmarshalJSON(in []byte, dst *representation) error {
dec := NewJSONDecoder(bytes.NewReader(in))
return dec.Decode(dst)
}
// JSONDecoder implements Decoder
type JSONDecoder struct{ r io.Reader }

var _ decoder = (*JSONDecoder)(nil)

// UnmarshalJSONRunner parses the JSON-encoded data and stores the result
// UnmarshalJSON parses the JSON-encoded data and stores the result
// in the benchttp.Runner pointed to by dst.
func UnmarshalJSONRunner(in []byte, dst *benchttp.Runner) error {
func UnmarshalJSON(in []byte, dst *benchttp.Runner) error {
dec := NewJSONDecoder(bytes.NewReader(in))
return dec.DecodeRunner(dst)
return dec.Decode(dst)
}

// JSONDecoder implements Decoder
type JSONDecoder struct{ r io.Reader }

func NewJSONDecoder(r io.Reader) JSONDecoder {
return JSONDecoder{r: r}
}

// Decode reads the next JSON-encoded value from its input
// and stores it in the Representation pointed to by dst.
func (d JSONDecoder) Decode(dst *representation) error {
decoder := json.NewDecoder(d.r)
decoder.DisallowUnknownFields()
return d.handleError(decoder.Decode(dst))
}

// Decode reads the next JSON-encoded value from its input
// and stores it in the benchttp.Runner pointed to by dst.
func (d JSONDecoder) DecodeRunner(dst *benchttp.Runner) error {
func (d JSONDecoder) Decode(dst *benchttp.Runner) error {
repr := representation{}
if err := d.Decode(&repr); err != nil {
if err := d.decodeRepr(&repr); err != nil {
return err
}
return repr.parseAndMutate(dst)
}

// decodeRepr reads the next JSON-encoded value from its input
// and stores it in the Representation pointed to by dst.
func (d JSONDecoder) decodeRepr(dst *representation) error {
decoder := json.NewDecoder(d.r)
decoder.DisallowUnknownFields()
return d.handleError(decoder.Decode(dst))
}

// handleError handles an error from package json,
// transforms it into a user-friendly standardized format
// and returns the resulting error.
Expand Down
4 changes: 2 additions & 2 deletions configio/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestMarshalJSON(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
gotRunner := benchttp.DefaultRunner()
gotError := configio.UnmarshalJSONRunner(tc.input, &gotRunner)
gotError := configio.UnmarshalJSON(tc.input, &gotRunner)

if !tc.isValidRunner(benchttp.DefaultRunner(), gotRunner) {
t.Errorf("unexpected runner:\n%+v", gotRunner)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestJSONDecoder(t *testing.T) {
runner := benchttp.Runner{}
decoder := configio.NewJSONDecoder(bytes.NewReader(tc.in))

gotErr := decoder.DecodeRunner(&runner)
gotErr := decoder.Decode(&runner)

if tc.exp == "" {
if gotErr != nil {
Expand Down
39 changes: 17 additions & 22 deletions configio/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,40 @@ import (
"github.com/benchttp/sdk/benchttp"
)

// UnmarshalYAML parses the YAML-encoded data and stores the result
// in the Representation pointed to by dst.
func UnmarshalYAML(in []byte, dst *representation) error {
dec := NewYAMLDecoder(bytes.NewReader(in))
return dec.Decode(dst)
}
// YAMLDecoder implements Decoder
type YAMLDecoder struct{ r io.Reader }

var _ decoder = (*YAMLDecoder)(nil)

// UnmarshalYAMLRunner parses the YAML-encoded data and stores the result
// UnmarshalYAML parses the YAML-encoded data and stores the result
// in the benchttp.Runner pointed to by dst.
func UnmarshalYAMLRunner(in []byte, dst *benchttp.Runner) error {
func UnmarshalYAML(in []byte, dst *benchttp.Runner) error {
dec := NewYAMLDecoder(bytes.NewReader(in))
return dec.DecodeRunner(dst)
return dec.Decode(dst)
}

// YAMLDecoder implements Decoder
type YAMLDecoder struct{ r io.Reader }

func NewYAMLDecoder(r io.Reader) YAMLDecoder {
return YAMLDecoder{r: r}
}

// Decode reads the next YAML-encoded value from its input
// and stores it in the Representation pointed to by dst.
func (d YAMLDecoder) Decode(dst *representation) error {
decoder := yaml.NewDecoder(d.r)
decoder.KnownFields(true)
return d.handleError(decoder.Decode(dst))
}

// Decode reads the next YAML-encoded value from its input
// and stores it in the benchttp.Runner pointed to by dst.
func (d YAMLDecoder) DecodeRunner(dst *benchttp.Runner) error {
func (d YAMLDecoder) Decode(dst *benchttp.Runner) error {
repr := representation{}
if err := d.Decode(&repr); err != nil {
if err := d.decodeRepr(&repr); err != nil {
return err
}
return repr.parseAndMutate(dst)
}

// decodeRepr reads the next YAML-encoded value from its input
// and stores it in the Representation pointed to by dst.
func (d YAMLDecoder) decodeRepr(dst *representation) error {
decoder := yaml.NewDecoder(d.r)
decoder.KnownFields(true)
return d.handleError(decoder.Decode(dst))
}

// handleError handles a raw yaml decoder.Decode error, filters it,
// and return the resulting error.
func (d YAMLDecoder) handleError(err error) error {
Expand Down
2 changes: 1 addition & 1 deletion configio/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestYAMLDecoder(t *testing.T) {
runner := benchttp.Runner{}
decoder := configio.NewYAMLDecoder(bytes.NewReader(tc.in))

gotErr := decoder.DecodeRunner(&runner)
gotErr := decoder.Decode(&runner)

if tc.expErr == nil {
if gotErr != nil {
Expand Down

0 comments on commit 39fb4a6

Please sign in to comment.