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

deriveCompose: Unmarshal #20

Open
awalterschulze opened this issue Jul 25, 2017 · 0 comments
Open

deriveCompose: Unmarshal #20

awalterschulze opened this issue Jul 25, 2017 · 0 comments

Comments

@awalterschulze
Copy link
Owner

Unmarshal takes two arguments of which one is a return type. So it should still be possible to compose it without the user making a special function.

func getUsername(u *User) (string, error) {
   if u.Username == "" {
       return "", fmt.Errorf("no username")
   }
   return u.Username, nil
}

username, err := deriveCompose(json.Unmarshal, getUsername)(buf)

func deriveCompose(f func([]byte, interface{}) error, g func(*User) (string, error)) func([]byte) (string, error) {
    return func(buf []byte) (string, error) {
         u := new(User)
         if err := f(buf, u); err != nil {
             return "", err
         }
         return g(u)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant