Skip to content

Commit

Permalink
Update User Guide
Browse files Browse the repository at this point in the history
  • Loading branch information
fogfish committed Mar 19, 2023
1 parent 1081a3f commit 9e5fcde
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 298 deletions.
543 changes: 288 additions & 255 deletions doc/user-guide.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/http-recursion/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func request(page int) (*seq, http.Arrow) {
ø.Param("page", page),
ø.Accept.JSON,
ƒ.Status.OK,
ƒ.Recv(&seq),
ƒ.Body(&seq),
)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/http-request/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func request() http.Arrow {
// specify requirements to the response
ƒ.Status.OK,
ƒ.ContentType.JSON,
ƒ.Recv(&data),
ƒ.Body(&data),
)
}

Expand Down
4 changes: 2 additions & 2 deletions examples/http-response-chain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (hof *Heap) uuid() http.Arrow {

ƒ.Status.OK,
ƒ.ContentType.JSON,
ƒ.Recv(&hof.ID),
ƒ.Body(&hof.ID),
)
}

Expand All @@ -62,7 +62,7 @@ func (hof *Heap) post() http.Arrow {
ø.Send(&hof.ID.UUID),

ƒ.Status.OK,
ƒ.Recv(&hof.HTTPBin),
ƒ.Body(&hof.HTTPBin),
)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/http-response/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func request() (*HTTPBin, http.Arrow) {
// HTTP Response
ƒ.Status.OK,
ƒ.ContentType.JSON,
ƒ.Recv(&data),
ƒ.Body(&data),

// asserts
data.validate,
Expand Down
48 changes: 27 additions & 21 deletions http/recv/arrows.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,10 @@ const (
Via = HeaderOf[string]("Via")
)

// Recv applies auto decoders for response and returns either binary or
// Body applies auto decoders for response and returns either binary or
// native Go data structure. The Content-Type header give a hint to decoder.
// Supply the pointer to data target data structure.
func Recv[T any](out *T) http.Arrow {
func Body[T any](out *T) http.Arrow {
return func(cat *http.Context) error {
err := decode(
cat.Response.Header.Get("Content-Type"),
Expand All @@ -645,6 +645,12 @@ func Recv[T any](out *T) http.Arrow {
}
}

// Recv is alias for Body, maintained only for compatibility
func Recv[T any](out *T) http.Arrow {
return Body(out)
}

// Match received payload to defined pattern
func Expect[T any](expect T) http.Arrow {
return func(cat *http.Context) error {
var actual T
Expand Down Expand Up @@ -728,26 +734,11 @@ func Match(val string) http.Arrow {
}
}

func equiv(pat, val map[string]any) bool {
for k, p := range pat {
v, has := val[k]
if !has {
return false
}

if p == "_" {
continue
}

if !equivVal(p, v) {
return false
}
func equivVal(pat, val any) bool {
if pp, ok := pat.(string); ok && pp == "_" {
return true
}

return true
}

func equivVal(pat, val any) bool {
switch vv := val.(type) {
case string:
pp, ok := pat.(string)
Expand Down Expand Up @@ -786,8 +777,23 @@ func equivVal(pat, val any) bool {
if !ok {
return false
}
return equiv(pp, vv)
return equivMap(pp, vv)
}

return false
}

func equivMap(pat, val map[string]any) bool {
for k, p := range pat {
v, has := val[k]
if !has {
return false
}

if !equivVal(p, v) {
return false
}
}

return true
}
43 changes: 26 additions & 17 deletions http/recv/arrows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,32 +255,38 @@ func TestHeaderUndefinedWithVal(t *testing.T) {
)
}

func TestRecvJSON(t *testing.T) {
func TestBodyJSON(t *testing.T) {
type Site struct {
Site string `json:"site"`
}

ts := mock()
defer ts.Close()

var site Site
req := µ.GET(
ø.URI("%s/json", ø.Authority(ts.URL)),
ƒ.Status.OK,
ƒ.ContentType.ApplicationJSON,
ƒ.ContentType.JSON,
ƒ.Recv(&site),
)
cat := µ.New()
err := cat.IO(context.Background(), req)
for _, arrow := range []func(out *Site) µ.Arrow{
ƒ.Body[Site],
ƒ.Recv[Site],
} {

it.Then(t).Should(
it.Nil(err),
it.Equal(site.Site, "example.com"),
)
var site Site
req := µ.GET(
ø.URI("%s/json", ø.Authority(ts.URL)),
ƒ.Status.OK,
ƒ.ContentType.ApplicationJSON,
ƒ.ContentType.JSON,
arrow(&site),
)
cat := µ.New()
err := cat.IO(context.Background(), req)

it.Then(t).Should(
it.Nil(err),
it.Equal(site.Site, "example.com"),
)
}
}

func TestRecvForm(t *testing.T) {
func TestBodyForm(t *testing.T) {
type Site struct {
Site string `json:"site"`
}
Expand All @@ -293,7 +299,7 @@ func TestRecvForm(t *testing.T) {
ø.URI("%s/form", ø.Authority(ts.URL)),
ƒ.Status.OK,
ƒ.ContentType.Form,
ƒ.Recv(&site),
ƒ.Body(&site),
)
cat := µ.New()
err := cat.IO(context.Background(), req)
Expand Down Expand Up @@ -395,6 +401,9 @@ func TestMatch(t *testing.T) {
`{"c":1.1}`,
`{"f":true}`,
`{"a":"a", "b":101, "c":1.1}`,
`{"d":["_", "_", "_"]}`,
`{"d":["a", "_", "_"]}`,
`{"d":["_", "b", "_"]}`,
`{"d":["a", "b", "c"]}`,
`{"e":{"a":"_"}}`,
`{"e":{"a":"a"}}`,
Expand Down

0 comments on commit 9e5fcde

Please sign in to comment.