awalterschulze / goderive

Derives and generates mundane golang functions that you do not want to maintain yourself
Apache License 2.0
1.23k stars 44 forks source link

deriveCompose: make it variadic #19

Closed awalterschulze closed 7 years ago

awalterschulze commented 7 years ago

Also rename deriveBind to deriveCompose

awalterschulze commented 7 years ago

Then we can write

getEndpoint := fmt.Sprintf("%s/oldusers/%s", config.endpoint, config.username)
postEndpoint := fmt.Sprintf("%s/newusers/%s", config.endpoint, config.username)

result, err := deriveCompose(
    http.Get(getEndpoint),
    func(req *http.Request) ([]byte, error) { return ioutil.ReadAll(req.Body) },
    user.NewFromJson,
    user.NewUserFromUser,
    json.Marshal,
    func(buf []byte) (*http.Response, error) {
        return http.Post(postEndpoint, "application/json", bytes.NewBuffer(buf))
    },
)

Example found on slide 55 of https://speakerdeck.com/rebeccaskinner/monadic-error-handling-in-go

mh-cbon commented 7 years ago

crazy shit. That is awesome.

awalterschulze commented 7 years ago

Glad you like it :)

awalterschulze commented 7 years ago

done and also included an example https://github.com/awalterschulze/goderive/tree/master/example/plugin/compose