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

[WIP] Add MakeMultiEncoder #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,39 @@ func MakeTypedEncoder(f interface{}) func(*Request) func(io.Writer) Encoder {
})
}

// MakeMultiEncoder takes pairs of arguments, where the first of the pair is a value that denotes a type and the second denotes an encoder.
// The resulting encoder then uses the encoder of the pair with matching first element.
// Example:
// e := MakeMultiEncoder(
// "string", MakeTypedEncoder(func(req *Request, w io.Writer, str string) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we be able to use reflection to get the type from the function signature, instead of manually writing the type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do, we could also pass an empty string but this is more descriptive.

// // ...
// return nil
// },
// cmdkit.Error{}, MakeTypedEncoder(func(req *Request, w io.Writer, err cmdkit.Error) error {
// // ...
// return nil
// }))
func MakeMultiEncoder(args ...interface{}) func(*Request) func(io.Writer) Encoder {
if len(args)%2 != 0 {
panic("MakeMultiEncoder must receive an even number of parameters")
}

types := make(map[reflect.Type]func(*Request) func(io.Writer) Encoder)

for i := 0; i < len(args); i += 2 {
types[reflect.TypeOf(args[i])] = args[i+1].(func(*Request) func(io.Writer) Encoder)
}

return MakeEncoder(func(req *Request, w io.Writer, i interface{}) error {
f, ok := types[reflect.TypeOf(i)]
if !ok {
return fmt.Errorf("unexpected type: %T", i)
}

return f(req)(w).Encode(i)
})
}

type genericEncoder struct {
f func(*Request, io.Writer, interface{}) error
w io.Writer
Expand Down
46 changes: 46 additions & 0 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type fooTestObj struct {
Good bool
}

type barTestObj struct {
Bad bool
}

func TestMakeTypedEncoder(t *testing.T) {
expErr := fmt.Errorf("command fooTestObj failed")
f := MakeTypedEncoder(func(req *Request, w io.Writer, v *fooTestObj) error {
Expand Down Expand Up @@ -55,3 +59,45 @@ func TestMakeTypedEncoderArrays(t *testing.T) {
t.Fatal(err)
}
}

func TestMakeMultiEncoder(t *testing.T) {
expErrFoo := fmt.Errorf("command fooTestObj failed")
expErrBar := fmt.Errorf("command barTestObj failed")

f := MakeMultiEncoder(
&fooTestObj{}, MakeTypedEncoder(func(req *Request, w io.Writer, v *fooTestObj) error {
if v.Good {
return nil
}
return expErrFoo
}),
&barTestObj{}, MakeTypedEncoder(func(req *Request, w io.Writer, v *barTestObj) error {
if !v.Bad {
return nil
}
return expErrBar
}))

req := &Request{}

encoderFunc := f(req)

buf := new(bytes.Buffer)
encoder := encoderFunc(buf)

if err := encoder.Encode(&fooTestObj{true}); err != nil {
t.Fatal(err)
}

if err := encoder.Encode(&fooTestObj{false}); err != expErrFoo {
t.Fatal("expected: ", expErrFoo)
}

if err := encoder.Encode(&barTestObj{true}); err != expErrBar {
t.Fatal("expected: ", expErrBar)
}

if err := encoder.Encode(&barTestObj{false}); err != nil {
t.Fatal(err)
}
}