Skip to content

Instantly share code, notes, and snippets.

@ikawaha
Created December 26, 2022 07:45
Show Gist options
  • Save ikawaha/e1df0c84b5e22d6e7f2a8621d6e5631d to your computer and use it in GitHub Desktop.
Save ikawaha/e1df0c84b5e22d6e7f2a8621d6e5631d to your computer and use it in GitHub Desktop.
curl -X POST localhost:8888/check-in -d '{"pet-owner":{"pet":{"Type":"dog","Value":"{\"Name\":\"POCHI\",\"Breed\":\"AKITA\"}"}}}'
{"pet":{"Type":"dog","Value":"{\"Name\":\"POCHI\",\"Breed\":\"AKITA\"}"}}
package design
import (
. "goa.design/goa/v3/dsl"
)
var Dog = Type("Dog", func() {
Description("Dogs are cool")
Field(1, "Name")
Field(2, "Breed")
Required("Name", "Breed")
})
var Cat = Type("Cat", func() {
Description("Cats are cool too")
Field(1, "Name")
Field(2, "Age", Int)
Required("Name", "Age")
})
var PetOwner = Type("PetOwner", func() {
Description("A pet owner")
OneOf("pet", func() {
Description("The owner's pet")
Field(1, "dog", Dog)
Field(2, "cat", Cat)
})
})
var _ = Service("pet-hotel", func() {
Method("check-in", func() {
Payload(func() {
// Attribute describes an object field
Attribute("pet-owner", PetOwner, func() {
Meta("rpc:tag", "1")
})
Required("pet-owner")
})
Result(PetOwner)
HTTP(func() {
POST("/check-in")
Response(StatusOK)
})
GRPC(func() {
Response(CodeOK)
})
})
})
package pethotelapi
import (
"context"
"fmt"
"log"
pethotel "goa.design/examples/union/gen/pet_hotel"
)
// pet-hotel service example implementation.
// The example methods log the requests and return zero values.
type petHotelsrvc struct {
logger *log.Logger
}
// NewPetHotel returns the pet-hotel service implementation.
func NewPetHotel(logger *log.Logger) pethotel.Service {
return &petHotelsrvc{logger}
}
// CheckIn implements check-in.
func (s *petHotelsrvc) CheckIn(ctx context.Context, p *pethotel.CheckInPayload) (*pethotel.PetOwner, error) {
s.logger.Print("petHotel.check-in")
switch p.PetOwner.Pet.(type) {
case *pethotel.Dog:
fmt.Println("bow bow")
case *pethotel.Cat:
fmt.Println("mew mew")
default:
return nil, fmt.Errorf("unknown type: %T", p.PetOwner.Pet)
}
res := &pethotel.PetOwner{
Pet: p.PetOwner.Pet,
}
return res, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment