예제 #1
0
func paramsFromHeaders(action *design.ActionDefinition) []*Parameter {
	params := []*Parameter{}
	action.IterateHeaders(func(name string, required bool, header *design.AttributeDefinition) error {
		p := paramFor(header, name, "header", required)
		params = append(params, p)
		return nil
	})
	return params
}
예제 #2
0
// defaultRouteParams returns the parameters needed to build the first route of the given action.
func defaultRouteParams(a *design.ActionDefinition) *design.AttributeDefinition {
	r := a.Routes[0]
	params := r.Params()
	o := make(design.Object, len(params))
	pparams := a.PathParams()
	for _, p := range params {
		o[p] = pparams.Type.ToObject()[p]
	}
	return &design.AttributeDefinition{Type: o}
}
예제 #3
0
파일: generator.go 프로젝트: DavyC/goa
func (g *Generator) generateIndexHTML(htmlFile string, api *design.APIDefinition, exampleAction *design.ActionDefinition) error {
	file, err := codegen.SourceFileFor(htmlFile)
	if err != nil {
		return err
	}
	g.genfiles = append(g.genfiles, htmlFile)

	argNames := params(exampleAction)
	var args string
	if len(argNames) > 0 {
		query := exampleAction.QueryParams.Type.ToObject()
		argValues := make([]string, len(argNames))
		for i, n := range argNames {
			q := query[n].Type.ToArray().ElemType
			// below works because we deal with simple types in query strings
			if q.Example != nil {
				argValues[i] = fmt.Sprintf("%v", q.Example)
			} else {
				argValues[i] = fmt.Sprintf("%v", q.GenerateExample(api.RandomGenerator()))
			}
		}
		args = strings.Join(argValues, ", ")
	}
	examplePath := exampleAction.Routes[0].FullPath(design.Design.APIVersionDefinition)
	pathParams := exampleAction.Routes[0].Params(design.Design.APIVersionDefinition)
	if len(pathParams) > 0 {
		pathVars := exampleAction.AllParams().Type.ToObject()
		pathValues := make([]interface{}, len(pathParams))
		for i, n := range pathParams {
			if pathVars[n].Example != nil {
				pathValues[i] = fmt.Sprintf("%v", pathVars[n].Example)
			} else {
				pathValues[i] = pathVars[n].GenerateExample(api.RandomGenerator())
			}
		}
		format := design.WildcardRegex.ReplaceAllLiteralString(examplePath, "/%v")
		examplePath = fmt.Sprintf(format, pathValues...)
	}
	if len(argNames) > 0 {
		args = ", " + args
	}
	exampleFunc := fmt.Sprintf(
		`%s%s ("%s"%s)`,
		exampleAction.Name,
		strings.Title(exampleAction.Parent.Name),
		examplePath,
		args,
	)
	data := map[string]interface{}{
		"API":         api,
		"ExampleFunc": exampleFunc,
	}

	return file.ExecuteTemplate("exampleHTML", exampleT, nil, data)
}
예제 #4
0
// defaultRouteParams returns the parameters needed to build the first route of the given action.
func defaultRouteParams(a *design.ActionDefinition) *design.AttributeDefinition {
	r := a.Routes[0]
	params := r.Params()
	o := make(design.Object, len(params))
	nz := make(map[string]bool, len(params))
	pparams := a.PathParams()
	for _, p := range params {
		o[p] = pparams.Type.ToObject()[p]
		nz[p] = true
	}
	return &design.AttributeDefinition{Type: o, NonZeroAttributes: nz}
}
예제 #5
0
func (g *Generator) generateIndexHTML(htmlFile string, exampleAction *design.ActionDefinition) error {
	file, err := codegen.SourceFileFor(htmlFile)
	if err != nil {
		return err
	}
	g.genfiles = append(g.genfiles, htmlFile)

	argNames := params(exampleAction)
	var args string
	if len(argNames) > 0 {
		query := exampleAction.QueryParams.Type.ToObject()
		argValues := make([]string, len(argNames))
		for i, n := range argNames {
			ex := query[n].GenerateExample(g.API.RandomGenerator(), nil)
			argValues[i] = fmt.Sprintf("%v", ex)
		}
		args = strings.Join(argValues, ", ")
	}
	examplePath := exampleAction.Routes[0].FullPath()
	pathParams := exampleAction.Routes[0].Params()
	if len(pathParams) > 0 {
		pathVars := exampleAction.AllParams().Type.ToObject()
		pathValues := make([]interface{}, len(pathParams))
		for i, n := range pathParams {
			ex := pathVars[n].GenerateExample(g.API.RandomGenerator(), nil)
			pathValues[i] = ex
		}
		format := design.WildcardRegex.ReplaceAllLiteralString(examplePath, "/%v")
		examplePath = fmt.Sprintf(format, pathValues...)
	}
	if len(argNames) > 0 {
		args = ", " + args
	}
	exampleFunc := fmt.Sprintf(
		`%s%s ("%s"%s)`,
		exampleAction.Name,
		strings.Title(exampleAction.Parent.Name),
		examplePath,
		args,
	)
	data := map[string]interface{}{
		"API":         g.API,
		"ExampleFunc": exampleFunc,
	}

	return file.ExecuteTemplate("exampleHTML", exampleT, nil, data)
}
예제 #6
0
			}
			action = &design.ActionDefinition{Parent: resource}
		})

		It("does not panic and merges the resource responses", func() {
			Ω(action.Finalize).ShouldNot(Panic())
			Ω(action.Responses).Should(HaveKey("NotFound"))
		})
	})
})

var _ = Describe("FullPath", func() {

	Context("Given a base resource and a resource with an action with a route", func() {
		var resource, parentResource *design.ResourceDefinition
		var action *design.ActionDefinition
		var route *design.RouteDefinition

		var actionPath string
		var resourcePath string
		var parentResourcePath string

		JustBeforeEach(func() {
			showAct := &design.ActionDefinition{}
			showRoute := &design.RouteDefinition{
				Path:   parentResourcePath,
				Parent: showAct,
			}
			showAct.Routes = []*design.RouteDefinition{showRoute}
			parentResource = &design.ResourceDefinition{}
			parentResource.Actions = map[string]*design.ActionDefinition{"show": showAct}
예제 #7
0
func (g *Generator) generateActionClient(action *design.ActionDefinition, file *codegen.SourceFile, funcs template.FuncMap) error {
	var (
		params        []string
		names         []string
		queryParams   []*paramData
		headers       []*paramData
		signer        string
		clientsTmpl   = template.Must(template.New("clients").Funcs(funcs).Parse(clientsTmpl))
		requestsTmpl  = template.Must(template.New("requests").Funcs(funcs).Parse(requestsTmpl))
		clientsWSTmpl = template.Must(template.New("clientsws").Funcs(funcs).Parse(clientsWSTmpl))
	)
	if action.Payload != nil {
		params = append(params, "payload "+codegen.GoTypeRef(action.Payload, action.Payload.AllRequired(), 1, false))
		names = append(names, "payload")
	}
	initParams := func(att *design.AttributeDefinition) []*paramData {
		if att == nil {
			return nil
		}
		obj := att.Type.ToObject()
		var pdata []*paramData
		var optData []*paramData
		for n, q := range obj {
			varName := codegen.Goify(n, false)
			param := &paramData{
				Name:      n,
				VarName:   varName,
				Attribute: q,
			}
			if q.Type.IsPrimitive() {
				param.MustToString = q.Type.Kind() != design.StringKind
				if att.IsRequired(n) {
					param.ValueName = varName
					pdata = append(pdata, param)
				} else {
					param.ValueName = "*" + varName
					param.CheckNil = true
					optData = append(optData, param)
				}
			} else {
				if q.Type.IsArray() {
					param.IsArray = true
					param.ElemAttribute = q.Type.ToArray().ElemType
				}
				param.MustToString = true
				param.ValueName = varName
				param.CheckNil = true
				if att.IsRequired(n) {
					pdata = append(pdata, param)
				} else {
					optData = append(optData, param)
				}
			}
		}

		sort.Sort(byParamName(pdata))
		sort.Sort(byParamName(optData))

		// Update closure
		for _, p := range pdata {
			names = append(names, p.VarName)
			params = append(params, p.VarName+" "+cmdFieldType(p.Attribute.Type, false))
		}
		for _, p := range optData {
			names = append(names, p.VarName)
			params = append(params, p.VarName+" "+cmdFieldType(p.Attribute.Type, p.Attribute.Type.IsPrimitive()))
		}

		return append(pdata, optData...)
	}
	queryParams = initParams(action.QueryParams)
	headers = initParams(action.Headers)
	if action.Security != nil {
		signer = codegen.Goify(action.Security.Scheme.SchemeName, true)
	}
	data := struct {
		Name            string
		ResourceName    string
		Description     string
		Routes          []*design.RouteDefinition
		HasPayload      bool
		Params          string
		ParamNames      string
		CanonicalScheme string
		Signer          string
		QueryParams     []*paramData
		Headers         []*paramData
	}{
		Name:            action.Name,
		ResourceName:    action.Parent.Name,
		Description:     action.Description,
		Routes:          action.Routes,
		HasPayload:      action.Payload != nil,
		Params:          strings.Join(params, ", "),
		ParamNames:      strings.Join(names, ", "),
		CanonicalScheme: action.CanonicalScheme(),
		Signer:          signer,
		QueryParams:     queryParams,
		Headers:         headers,
	}
	if action.WebSocket() {
		return clientsWSTmpl.Execute(file, data)
	}
	if err := clientsTmpl.Execute(file, data); err != nil {
		return err
	}
	return requestsTmpl.Execute(file, data)
}