Exemplo n.º 1
0
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)
}
Exemplo n.º 2
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)
}