add goa examples

This commit is contained in:
CaptainNEO
2022-03-07 20:49:02 +08:00
commit e2fd9f666e
39 changed files with 3858 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
// Code generated by goa v3.6.0, DO NOT EDIT.
//
// Service1 gRPC client CLI support package
//
// Command:
// $ goa gen goa_example/design
package client
import (
"encoding/json"
"fmt"
service1pb "goa_example/gen/grpc/service1/pb"
service1 "goa_example/gen/service1"
)
// BuildSigninPayload builds the payload for the Service1 signin endpoint from
// CLI flags.
func BuildSigninPayload(service1SigninUsername string, service1SigninPassword string) (*service1.SigninPayload, error) {
var username string
{
username = service1SigninUsername
}
var password string
{
password = service1SigninPassword
}
v := &service1.SigninPayload{}
v.Username = username
v.Password = password
return v, nil
}
// BuildSecurePayload builds the payload for the Service1 secure endpoint from
// CLI flags.
func BuildSecurePayload(service1SecureMessage string, service1SecureToken string) (*service1.SecurePayload, error) {
var err error
var message service1pb.SecureRequest
{
if service1SecureMessage != "" {
err = json.Unmarshal([]byte(service1SecureMessage), &message)
if err != nil {
return nil, fmt.Errorf("invalid JSON for message, \nerror: %s, \nexample of valid JSON:\n%s", err, "'{\n \"fail\": true\n }'")
}
}
}
var token string
{
token = service1SecureToken
}
v := &service1.SecurePayload{
Fail: &message.Fail,
}
v.Token = token
return v, nil
}

View File

@@ -0,0 +1,74 @@
// Code generated by goa v3.6.0, DO NOT EDIT.
//
// Service1 gRPC client
//
// Command:
// $ goa gen goa_example/design
package client
import (
"context"
service1pb "goa_example/gen/grpc/service1/pb"
goagrpc "goa.design/goa/v3/grpc"
goapb "goa.design/goa/v3/grpc/pb"
goa "goa.design/goa/v3/pkg"
"google.golang.org/grpc"
)
// Client lists the service endpoint gRPC clients.
type Client struct {
grpccli service1pb.Service1Client
opts []grpc.CallOption
}
// NewClient instantiates gRPC client for all the Service1 service servers.
func NewClient(cc *grpc.ClientConn, opts ...grpc.CallOption) *Client {
return &Client{
grpccli: service1pb.NewService1Client(cc),
opts: opts,
}
}
// Signin calls the "Signin" function in service1pb.Service1Client interface.
func (c *Client) Signin() goa.Endpoint {
return func(ctx context.Context, v interface{}) (interface{}, error) {
inv := goagrpc.NewInvoker(
BuildSigninFunc(c.grpccli, c.opts...),
EncodeSigninRequest,
DecodeSigninResponse)
res, err := inv.Invoke(ctx, v)
if err != nil {
resp := goagrpc.DecodeError(err)
switch message := resp.(type) {
case *goapb.ErrorResponse:
return nil, goagrpc.NewServiceError(message)
default:
return nil, goa.Fault(err.Error())
}
}
return res, nil
}
}
// Secure calls the "Secure" function in service1pb.Service1Client interface.
func (c *Client) Secure() goa.Endpoint {
return func(ctx context.Context, v interface{}) (interface{}, error) {
inv := goagrpc.NewInvoker(
BuildSecureFunc(c.grpccli, c.opts...),
EncodeSecureRequest,
DecodeSecureResponse)
res, err := inv.Invoke(ctx, v)
if err != nil {
resp := goagrpc.DecodeError(err)
switch message := resp.(type) {
case *goapb.ErrorResponse:
return nil, goagrpc.NewServiceError(message)
default:
return nil, goa.Fault(err.Error())
}
}
return res, nil
}
}

View File

@@ -0,0 +1,87 @@
// Code generated by goa v3.6.0, DO NOT EDIT.
//
// Service1 gRPC client encoders and decoders
//
// Command:
// $ goa gen goa_example/design
package client
import (
"context"
service1pb "goa_example/gen/grpc/service1/pb"
service1 "goa_example/gen/service1"
goagrpc "goa.design/goa/v3/grpc"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// BuildSigninFunc builds the remote method to invoke for "Service1" service
// "signin" endpoint.
func BuildSigninFunc(grpccli service1pb.Service1Client, cliopts ...grpc.CallOption) goagrpc.RemoteFunc {
return func(ctx context.Context, reqpb interface{}, opts ...grpc.CallOption) (interface{}, error) {
for _, opt := range cliopts {
opts = append(opts, opt)
}
if reqpb != nil {
return grpccli.Signin(ctx, reqpb.(*service1pb.SigninRequest), opts...)
}
return grpccli.Signin(ctx, &service1pb.SigninRequest{}, opts...)
}
}
// EncodeSigninRequest encodes requests sent to Service1 signin endpoint.
func EncodeSigninRequest(ctx context.Context, v interface{}, md *metadata.MD) (interface{}, error) {
payload, ok := v.(*service1.SigninPayload)
if !ok {
return nil, goagrpc.ErrInvalidType("Service1", "signin", "*service1.SigninPayload", v)
}
(*md).Append("username", payload.Username)
(*md).Append("password", payload.Password)
return NewSigninRequest(), nil
}
// DecodeSigninResponse decodes responses from the Service1 signin endpoint.
func DecodeSigninResponse(ctx context.Context, v interface{}, hdr, trlr metadata.MD) (interface{}, error) {
message, ok := v.(*service1pb.SigninResponse)
if !ok {
return nil, goagrpc.ErrInvalidType("Service1", "signin", "*service1pb.SigninResponse", v)
}
res := NewSigninResult(message)
return res, nil
}
// BuildSecureFunc builds the remote method to invoke for "Service1" service
// "secure" endpoint.
func BuildSecureFunc(grpccli service1pb.Service1Client, cliopts ...grpc.CallOption) goagrpc.RemoteFunc {
return func(ctx context.Context, reqpb interface{}, opts ...grpc.CallOption) (interface{}, error) {
for _, opt := range cliopts {
opts = append(opts, opt)
}
if reqpb != nil {
return grpccli.Secure(ctx, reqpb.(*service1pb.SecureRequest), opts...)
}
return grpccli.Secure(ctx, &service1pb.SecureRequest{}, opts...)
}
}
// EncodeSecureRequest encodes requests sent to Service1 secure endpoint.
func EncodeSecureRequest(ctx context.Context, v interface{}, md *metadata.MD) (interface{}, error) {
payload, ok := v.(*service1.SecurePayload)
if !ok {
return nil, goagrpc.ErrInvalidType("Service1", "secure", "*service1.SecurePayload", v)
}
(*md).Append("authorization", payload.Token)
return NewSecureRequest(payload), nil
}
// DecodeSecureResponse decodes responses from the Service1 secure endpoint.
func DecodeSecureResponse(ctx context.Context, v interface{}, hdr, trlr metadata.MD) (interface{}, error) {
message, ok := v.(*service1pb.SecureResponse)
if !ok {
return nil, goagrpc.ErrInvalidType("Service1", "secure", "*service1pb.SecureResponse", v)
}
res := NewSecureResult(message)
return res, nil
}

View File

@@ -0,0 +1,48 @@
// Code generated by goa v3.6.0, DO NOT EDIT.
//
// Service1 gRPC client types
//
// Command:
// $ goa gen goa_example/design
package client
import (
service1pb "goa_example/gen/grpc/service1/pb"
service1 "goa_example/gen/service1"
)
// NewSigninRequest builds the gRPC request type from the payload of the
// "signin" endpoint of the "Service1" service.
func NewSigninRequest() *service1pb.SigninRequest {
message := &service1pb.SigninRequest{}
return message
}
// NewSigninResult builds the result type of the "signin" endpoint of the
// "Service1" service from the gRPC response type.
func NewSigninResult(message *service1pb.SigninResponse) *service1.Creds {
result := &service1.Creds{
JWT: message.Jwt,
APIKey: message.ApiKey,
OauthToken: message.OauthToken,
}
return result
}
// NewSecureRequest builds the gRPC request type from the payload of the
// "secure" endpoint of the "Service1" service.
func NewSecureRequest(payload *service1.SecurePayload) *service1pb.SecureRequest {
message := &service1pb.SecureRequest{}
if payload.Fail != nil {
message.Fail = *payload.Fail
}
return message
}
// NewSecureResult builds the result type of the "secure" endpoint of the
// "Service1" service from the gRPC response type.
func NewSecureResult(message *service1pb.SecureResponse) string {
result := message.Field
return result
}