Beispiel #1
0
// Resource implements the resource definition DSL. There is one resource definition per resource
// exposed by the API. The resource DSL allows setting the resource default media type. This media
// type is used to render the response body of actions that return the OK response (unless the
// action overrides the default). The default media type also sets the properties of the request
// payload attributes with the same name. See DefaultMedia.
//
// The resource DSL also allows listing the supported resource collection and resource collection
// item actions. Each action corresponds to a specific API endpoint. See Action.
//
// The resource DSL can also specify a parent resource. Parent resources have two effects.
// First, they set the prefix of all resource action paths to the parent resource href. Note that
// actions can override the path using an absolute path (that is a path starting with "//").
// Second, goa uses the parent resource href coupled with the resource BasePath if any to build
// hrefs to the resource collection or resource collection items. By default goa uses the show
// action if present to compute a resource href (basically concatenating the parent resource href
// with the base path and show action path). The resource definition may specify a canonical action
// via CanonicalActionName to override that default. Here is an example of a resource definition:
//
//	Resource("bottle", func() {
//		Description("A wine bottle")	// Resource description
//		DefaultMedia(BottleMedia)	// Resource default media type
//		BasePath("/bottles")		// Common resource action path prefix if not ""
//		Parent("account")		// Name of parent resource if any
//		CanonicalActionName("get")	// Name of action that returns canonical representation if not "show"
//		UseTrait("Authenticated")	// Included trait if any, can appear more than once
//		APIVersion("v1")		// API version exposing this resource, can appear more than once.
//
//		Action("show", func() {		// Action definition, can appear more than once
//			// ... Action DSL
//		})
//	})
func Resource(name string, dsl func()) *design.ResourceDefinition {
	if design.Design.Resources == nil {
		design.Design.Resources = make(map[string]*design.ResourceDefinition)
	}
	var resource *design.ResourceDefinition
	if topLevelDefinition(true) {
		if _, ok := design.Design.Resources[name]; ok {
			ReportError("resource %#v is defined twice", name)
			return nil
		}
		resource = design.NewResourceDefinition(name, dsl)
		design.Design.Resources[name] = resource
	}
	return resource
}
Beispiel #2
0
// Resource implements the resource definition dsl. There is one resource definition per resource
// exposed by the API. The resource dsl allows setting the resource default media type. This media
// type is used to render the response body of actions that return the OK response (unless the
// action overrides the default). The default media type also sets the properties of the request
// payload attributes with the same name. See DefaultMedia.
//
// The resource dsl also allows listing the supported resource collection and resource collection
// item actions. Each action corresponds to a specific API endpoint. See Action.
//
// The resource dsl can also specify a parent resource. Parent resources have two effects.
// First, they set the prefix of all resource action paths to the parent resource href. Note that
// actions can override the path using an absolute path (that is a path starting with "//").
// Second, goa uses the parent resource href coupled with the resource BasePath if any to build
// hrefs to the resource collection or resource collection items. By default goa uses the show
// action if present to compute a resource href (basically concatenating the parent resource href
// with the base path and show action path). The resource definition may specify a canonical action
// via CanonicalActionName to override that default. Here is an example of a resource definition:
//
//	Resource("bottle", func() {
//		Description("A wine bottle")	// Resource description
//		DefaultMedia(BottleMedia)	// Resource default media type
//		BasePath("/bottles")		// Common resource action path prefix if not ""
//		Parent("account")		// Name of parent resource if any
//		CanonicalActionName("get")	// Name of action that returns canonical representation if not "show"
//		UseTrait("Authenticated")	// Included trait if any, can appear more than once
//
//		Origin("http://swagger.goa.design", func() { // Define CORS policy, may be prefixed with "*" wildcard
//			Headers("X-Shared-Secret")           // One or more authorized headers, use "*" to authorize all
//			Methods("GET", "POST")               // One or more authorized HTTP methods
//			Expose("X-Time")                     // One or more headers exposed to clients
//			MaxAge(600)                          // How long to cache a preflight request response
//			Credentials()                        // Sets Access-Control-Allow-Credentials header
//		})
//
//		Response(Unauthorized, ErrorMedia) // Common responses to all actions
//		Response(BadRequest, ErrorMedia)
//
//		Action("show", func() {		// Action definition, can appear more than once
//			// ... Action dsl
//		})
//	})
func Resource(name string, dsl func()) *design.ResourceDefinition {
	if design.Design.Resources == nil {
		design.Design.Resources = make(map[string]*design.ResourceDefinition)
	}
	if !dslengine.IsTopLevelDefinition() {
		dslengine.IncompatibleDSL()
		return nil
	}

	if _, ok := design.Design.Resources[name]; ok {
		dslengine.ReportError("resource %#v is defined twice", name)
		return nil
	}
	resource := design.NewResourceDefinition(name, dsl)
	design.Design.Resources[name] = resource
	return resource
}