本文整理匯總了Golang中github.com/raphael/goa/design.APIDefinition.Example方法的典型用法代碼示例。如果您正苦於以下問題:Golang APIDefinition.Example方法的具體用法?Golang APIDefinition.Example怎麽用?Golang APIDefinition.Example使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/raphael/goa/design.APIDefinition
的用法示例。
在下文中一共展示了APIDefinition.Example方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: generateIndexHTML
func (g *Generator) generateIndexHTML(
htmlFile string,
api *design.APIDefinition,
exampleAction *design.ActionDefinition,
funcs template.FuncMap,
) error {
file, err := os.Create(htmlFile)
if err != nil {
return err
}
defer file.Close()
g.genfiles = append(g.genfiles, htmlFile)
htmlTmpl := template.Must(template.New("exampleHTML").Funcs(funcs).Parse(exampleT))
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
argValues[i] = fmt.Sprintf("%v", api.Example(q.Type))
}
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 {
pathValues[i] = api.Example(pathVars[n].Type)
}
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 htmlTmpl.Execute(file, data)
}