Skip to content

Instantly share code, notes, and snippets.

@illyabusigin
Created July 19, 2016 16:43
Show Gist options
  • Save illyabusigin/52d2634f6eebecef75b2da2acf88b3ad to your computer and use it in GitHub Desktop.
Save illyabusigin/52d2634f6eebecef75b2da2acf88b3ad to your computer and use it in GitHub Desktop.
Goa - Recursive media type with specific view reference
package design
import (
. "github.com/goadesign/goa/design"
. "github.com/goadesign/goa/design/apidsl"
)
var _ = API("test", func() {
Title("test")
Description("test")
Host("localhost:8080")
Scheme("http")
BasePath("/v1/")
Produces("application/json")
})
var _ = Resource("test", func() {
BasePath("/test")
Action("show", func() {
Routing(GET("/"))
Response(OK, Menu)
})
})
var Menu = MediaType("application/vnd.menu+json", func() {
Attributes(func() {
Attribute("name", String, "The name of an application")
Attribute("children", CollectionOf("application/vnd.menu+json"), func() {
View("nameonly")
})
})
View("default", func() {
Attribute("name")
Attribute("children", func() {
View("nameonly")
})
})
View("nameonly", func() {
Attribute("name")
})
})
@illyabusigin
Copy link
Author

Perform the following Goa command to generate the app:

goagen app -d PATH/TO/design
``

The `media_types.go` file should look like below. Notice that the there is a `MenuNameOnly` struct. 

```go
//************************************************************************//
// API "test": Application Media Types
//
// Generated with goagen v0.2.dev, command line:
// $ goagen
// --design=goa_recursive/design
// --out=$(GOPATH)/src/goa_recursive
// --version=v0.2.dev
//
// The content of this file is auto-generated, DO NOT MODIFY
//************************************************************************//

package app

// Menu media type (default view)
//
// Identifier: application/vnd.menu+json
type Menu struct {
    Children MenuNameonlyCollection `json:"children,omitempty" xml:"children,omitempty" form:"children,omitempty"`
    // The name of an application
    Name *string `json:"name,omitempty" xml:"name,omitempty" form:"name,omitempty"`
}

// Menu media type (nameonly view)
//
// Identifier: application/vnd.menu+json
type MenuNameonly struct {
    // The name of an application
    Name *string `json:"name,omitempty" xml:"name,omitempty" form:"name,omitempty"`
}

// MenuCollection is the media type for an array of Menu (default view)
//
// Identifier: application/vnd.menu+json; type=collection
type MenuCollection []*Menu

// MenuCollection is the media type for an array of Menu (nameonly view)
//
// Identifier: application/vnd.menu+json; type=collection
type MenuNameonlyCollection []*MenuNameonly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment