Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an argument to CollectionOf() #1232

Merged
merged 2 commits into from May 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 35 additions & 3 deletions design/apidsl/media_type.go
Expand Up @@ -376,9 +376,17 @@ func Link(name string, view ...string) {
// CollectionOf creates a collection media type from its element media type. A collection media

This comment was marked as off-topic.

// type represents the content of responses that return a collection of resources such as "list"
// actions. This function can be called from any place where a media type can be used.
//
// The resulting media type identifier is built from the element media type by appending the media

This comment was marked as off-topic.

// type parameter "type" with value "collection".
func CollectionOf(v interface{}, apidsl ...func()) *design.MediaTypeDefinition {
//
// CollectionOf(BottleMedia) // If the identifier of BottleMedia is "vnd.goa.bottle',
// // Content-Type will be "vnd.goa.bottle; type=collection".

This comment was marked as off-topic.

//
// The collection identifier can be specified as second argument.
//
// CollectionOf(BottleMedia, "vnd.goa.bottles") // Content-Type will be "vnd.goa.bottles".

This comment was marked as off-topic.

func CollectionOf(v interface{}, paramAndDSL ...interface{}) *design.MediaTypeDefinition {
var m *design.MediaTypeDefinition
var ok bool
m, ok = v.(*design.MediaTypeDefinition)
Expand Down Expand Up @@ -410,6 +418,10 @@ func CollectionOf(v interface{}, apidsl ...func()) *design.MediaTypeDefinition {
params["type"] = "collection"
}
id = mime.FormatMediaType(mediatype, params)
p, apidsl := parseCollectionOfDSL(paramAndDSL...)
if p != "" {
id = p
}
canonical := design.CanonicalIdentifier(id)
if mt, ok := design.GeneratedMediaTypes[canonical]; ok {
// Already have a type for this collection, reuse it.
Expand All @@ -421,8 +433,8 @@ func CollectionOf(v interface{}, apidsl ...func()) *design.MediaTypeDefinition {
// since the DSL may modify element type name via the TypeName function.
mt.TypeName = m.TypeName + "Collection"
mt.AttributeDefinition = &design.AttributeDefinition{Type: ArrayOf(m)}
if len(apidsl) > 0 {
dslengine.Execute(apidsl[0], mt)
if apidsl != nil {
dslengine.Execute(apidsl, mt)
}
if mt.Views == nil {
// If the apidsl didn't create any views (or there is no apidsl at all)
Expand All @@ -439,3 +451,23 @@ func CollectionOf(v interface{}, apidsl ...func()) *design.MediaTypeDefinition {
design.GeneratedMediaTypes[canonical] = mt
return mt
}

func parseCollectionOfDSL(paramAndDSL ...interface{}) (string, func()) {
var param string
var dsl func()
var ok bool
if len(paramAndDSL) > 0 {
d := paramAndDSL[len(paramAndDSL)-1]
if dsl, ok = d.(func()); ok {
paramAndDSL = paramAndDSL[:len(paramAndDSL)-1]
}
for _, p := range paramAndDSL {
param, ok = p.(string)
if !ok {
dslengine.ReportError("invalid CollectionOf argument, must be a string", p)

This comment was marked as off-topic.

return "", nil
}
}
}
return param, dsl
}
29 changes: 28 additions & 1 deletion design/apidsl/media_type_test.go
Expand Up @@ -281,7 +281,34 @@ var _ = Describe("CollectionOf", func() {

It("produces a media type", func() {
Ω(col).ShouldNot(BeNil())
Ω(col.Identifier).ShouldNot(BeEmpty())
Ω(col.Identifier).Should(Equal("application/vnd.example; type=collection"))
Ω(col.TypeName).ShouldNot(BeEmpty())
Ω(Design.MediaTypes).Should(HaveKey(col.Identifier))
})
})

Context("defined with the collectio identifier", func() {

This comment was marked as off-topic.

var col *MediaTypeDefinition
BeforeEach(func() {
dslengine.Reset()
mt := MediaType("application/vnd.example", func() {
Attribute("id")
View("default", func() {
Attribute("id")
})
})
col = CollectionOf(mt, "application/vnd.examples")
Ω(dslengine.Errors).ShouldNot(HaveOccurred())
})

JustBeforeEach(func() {
dslengine.Run()
Ω(dslengine.Errors).ShouldNot(HaveOccurred())
})

It("produces a media type", func() {
Ω(col).ShouldNot(BeNil())
Ω(col.Identifier).Should(Equal("application/vnd.examples"))
Ω(col.TypeName).ShouldNot(BeEmpty())
Ω(Design.MediaTypes).Should(HaveKey(col.Identifier))
})
Expand Down