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

bind interface{ Arrow() Arrow } #54

Merged
merged 2 commits into from
Aug 18, 2023
Merged
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
16 changes: 16 additions & 0 deletions http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ func Join(arrows ...Arrow) Arrow {
}
}

// Bind composes HTTP arrows to high-order function
// In contrast with Join, input is arrow builders
// (a ⟼ b, b ⟼ c, c ⟼ d) ⤇ a ⟼ d
func Bind(arrows ...interface{ Arrow() Arrow }) Arrow {
return func(cat *Context) error {
for _, arrow := range arrows {
f := arrow.Arrow()
if err := f(cat); err != nil {
return err
}
}

return nil
}
}

// GET composes HTTP arrows to high-order function for HTTP GET request
// (a ⟼ b, b ⟼ c, c ⟼ d) ⤇ a ⟼ d
func GET(arrows ...Arrow) Arrow { return method(http.MethodGet, arrows) }
Expand Down
61 changes: 60 additions & 1 deletion http/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package http_test

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -67,8 +68,13 @@ func TestJoin(t *testing.T) {
defer ts.Close()

req := µ.GET(
ø.URI("%s/ok", ø.Authority(ts.URL)),
ø.URI("%s/opts", ø.Authority(ts.URL)),
µ.Join(
ø.Param("a", "1"),
ø.Param("b", "2"),
),
ƒ.Code(µ.StatusOK),
ƒ.Match(`{"opts": "a=1&b=2"}`),
)
cat := µ.New()
err := cat.IO(context.Background(), req)
Expand Down Expand Up @@ -100,6 +106,56 @@ func TestJoinCats(t *testing.T) {
)
}

type opt struct{ key, val string }

func (opt opt) Arrow() µ.Arrow { return ø.Param(opt.key, opt.val) }

type err string

func (err err) Arrow() µ.Arrow { return func(ctx *µ.Context) error { return fmt.Errorf("%s", err) } }

func TestBind(t *testing.T) {
ts := mock()
defer ts.Close()

req := µ.GET(
ø.URI("%s/opts", ø.Authority(ts.URL)),
µ.Bind(
opt{"a", "1"},
opt{"b", "2"},
),
ƒ.Code(µ.StatusOK),
ƒ.Match(`{"opts": "a=1&b=2"}`),
)
cat := µ.New()
err := cat.IO(context.Background(), req)

it.Then(t).Should(
it.Nil(err),
)
}

func TestBindFailed(t *testing.T) {
ts := mock()
defer ts.Close()

req := µ.GET(
ø.URI("%s/opts", ø.Authority(ts.URL)),
µ.Bind(
opt{"a", "1"},
err("failed"),
),
ƒ.Code(µ.StatusOK),
ƒ.Match(`{"opts": "a=1&b=2"}`),
)
cat := µ.New()
err := cat.IO(context.Background(), req)

it.Then(t).ShouldNot(
it.Nil(err),
)
}

func TestIOWithContext(t *testing.T) {
ts := mock()
defer ts.Close()
Expand Down Expand Up @@ -189,6 +245,9 @@ func mock() *httptest.Server {
w.Write([]byte("site=example.com"))
case r.URL.Path == "/ok":
w.WriteHeader(http.StatusOK)
case r.URL.Path == "/opts":
w.Header().Add("Content-Type", "application/json")
w.Write([]byte(`{"opts": "` + r.URL.RawQuery + `"}`))
default:
w.WriteHeader(http.StatusBadRequest)
}
Expand Down
Loading