Skip to content

Commit

Permalink
feature: Add the Goa plugin (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikawaha authored Oct 22, 2023
1 parent 4f250a6 commit 5e434b6
Show file tree
Hide file tree
Showing 25 changed files with 1,199 additions and 0 deletions.
21 changes: 21 additions & 0 deletions plugin/goa/_test/calc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package calc

import (
"context"

calcsvc "github.com/ikawaha/httpcheck/plugin/goa/calc/gen/calc"
)

// calc service example implementation.
// The example methods log the requests and return zero values.
type calcSvc struct{}

// NewCalc returns the calc service implementation.
func NewCalc() calcsvc.Service {
return &calcSvc{}
}

// Multiply implements multiply.
func (s *calcSvc) Multiply(_ context.Context, p *calcsvc.MultiplyPayload) (int, error) {
return p.A * p.B, nil
}
35 changes: 35 additions & 0 deletions plugin/goa/_test/calc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package calc

import (
"io"
"net/http"
"strconv"
"strings"
"testing"

"github.com/ikawaha/httpcheck"
"github.com/ikawaha/httpcheck/plugin/goa"
gen "github.com/ikawaha/httpcheck/plugin/goa/calc/gen/calc"
"github.com/ikawaha/httpcheck/plugin/goa/calc/gen/http/calc/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMounter(t *testing.T) {
m := goa.NewMounter()
m.Mount(goa.EndpointModular{
Builder: server.NewMultiplyHandler,
Mounter: server.MountMultiplyHandler,
Endpoint: gen.NewMultiplyEndpoint(NewCalc()),
})
httpcheck.New(m).Test(t, "GET", "/multiply/3/5").
Check().
MustHasStatus(http.StatusOK).
Cb(func(r *http.Response) {
b, err := io.ReadAll(r.Body)
require.NoError(t, err)
i, err := strconv.Atoi(strings.TrimSpace(string(b)))
assert.NoError(t, err, string(b))
assert.Equal(t, 3*5, i)
})
}
39 changes: 39 additions & 0 deletions plugin/goa/_test/design/design.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package design

import (
. "goa.design/goa/v3/dsl"
)

var _ = Service("calc", func() {
Description("The calc service performs operations on numbers")

// Method describes a service method (endpoint)
Method("multiply", func() {
// Payload describes the method payload.
// Here the payload is an object that consists of two fields.
Payload(func() {
// Attribute describes an object field
Attribute("a", Int, "Left operand")
Attribute("b", Int, "Right operand")
Required("a", "b")
})

// Result describes the method result.
// Here the result is a simple integer value.
Result(Int)

// HTTP describes the HTTP transport mapping.
HTTP(func() {
// Requests to the service consist of HTTP GET requests.
// The payload fields are encoded as path parameters.
GET("/multiply/{a}/{b}")
// Responses use a "200 OK" HTTP status.
// The result is encoded in the response body.
Response(StatusOK)
})
})

// Serve the file gen/http/openapi3.json for requests sent to
// /openapi.json.
Files("/openapi.json", "openapi3.json")
})
36 changes: 36 additions & 0 deletions plugin/goa/_test/gen/calc/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions plugin/goa/_test/gen/calc/endpoints.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions plugin/goa/_test/gen/calc/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions plugin/goa/_test/gen/http/calc/client/cli.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions plugin/goa/_test/gen/http/calc/client/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5e434b6

Please sign in to comment.