Skip to content

Instantly share code, notes, and snippets.

@aslakknutsen
Created October 27, 2016 14:18
Show Gist options
  • Save aslakknutsen/34fa7a87111bb64c4584474dc0621688 to your computer and use it in GitHub Desktop.
Save aslakknutsen/34fa7a87111bb64c4584474dc0621688 to your computer and use it in GitHub Desktop.
json api ish Goa struct
// ALMRelationship describes the basic relationship
var ALMRelationship = Type("ALMRelationship", func() {
Attribute("data", ArrayOf(Any))
})
// ALMLink describes a single linked resource
var ALMLink = Type("ALMLink", func() {
Attribute("href", Integer, "Unique numeric representation of the error")
Attribute("meta", HashOf(String, Any), "Related meta information")
})
// ALMError describes a single error caused by the current request
var ALMError = Type("ALMError", func() {
Attribute("code", Integer, "Unique numeric representation of the error")
Attribute("title", String, "Descriptive title for the given error")
Attribute("detail", String, "Complete description of the error")
Attribute("source", func() {
Attribute("pointer", String, "Reference to where in the payload the problem error is")
Attribute("parameter", String, "Reference to what request parameter caused the error")
})
Required("code", "title")
})
// ALMItem a
var ALMItem = Type("ALMItem", func() {
Attribute("type", String, "The object type")
Attribute("id", String, "The UUID for the object")
Attribute("attributes", func() {
Attribute("name", String)
})
Attribute("relationships", HashOf(String, ALMRelationship), "The items relations")
Required("type", "id")
})
// ALMProject a
var ALMProject = Type("ALMProject", func() {
Attribute("type", String, "The object type")
Attribute("id", String, "The UUID for the object")
Attribute("attributes", func() {
Attribute("name", String)
})
Attribute("relationships", HashOf(String, ALMRelationship), "The items relations")
Required("type", "id")
})
// ALMItemMediaType defines the Project MediaType
var ALMItemMediaType = DescribeMediaType("application/vnd.api.item+json", ALMItem)
// ALMProjectMediaType defines the Project MediaType
var ALMProjectMediaType = DescribeMediaType("application/vnd.api.project+json", ALMProject)
// ALMProjectsMediaType defines the Project MediaType
var ALMProjectsMediaType = DescribeMediaType("application/vnd.api.projects+json", ArrayOf(ALMProject))
// DescribeMediaType defines the basic structure of the json-api
func DescribeMediaType(mt string, data DataType) *MediaTypeDefinition {
return MediaType(mt, func() {
Attribute("meta", HashOf(String, Any), "Related meta information")
Attribute("errors", ArrayOf(ALMError), "Errors found in request")
Attribute("data", data, "Data")
Attribute("included", ArrayOf(Any))
Attribute("links", func() {
Attribute("self", String, "Self href")
Attribute("related", String, "Self href")
})
View("default", func() {
Attribute("meta")
Attribute("data")
Attribute("included")
Attribute("links")
})
View("error", func() {
Attribute("meta")
Attribute("errors")
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment