本文整理匯總了Golang中github.com/go-openapi/spec.Swagger.Swagger方法的典型用法代碼示例。如果您正苦於以下問題:Golang Swagger.Swagger方法的具體用法?Golang Swagger.Swagger怎麽用?Golang Swagger.Swagger使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/go-openapi/spec.Swagger
的用法示例。
在下文中一共展示了Swagger.Swagger方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Execute
// Execute this command
func (s *Spec) Execute(args []string) error {
targetPath := "."
if len(args) > 0 {
targetPath = args[0]
}
realPath, err := filepath.Abs(targetPath)
if err != nil {
return err
}
var file *os.File
switch s.Format {
case "json":
file, err = os.Create(filepath.Join(realPath, "swagger.json"))
if err != nil {
return err
}
case "yaml", "yml":
file, err = os.Create(filepath.Join(realPath, "swagger.yml"))
if err != nil {
return err
}
default:
return fmt.Errorf("invalid format: %s", s.Format)
}
defer file.Close()
log.Println("creating specification document in", filepath.Join(targetPath, file.Name()))
var doc spec.Swagger
info := new(spec.Info)
doc.Info = info
doc.Swagger = "2.0"
doc.Paths = new(spec.Paths)
doc.Definitions = make(spec.Definitions)
info.Title = s.Title
if info.Title == "" {
info.Title = swag.ToHumanNameTitle(filepath.Base(realPath))
}
info.Description = s.Description
info.Version = s.Version
info.TermsOfService = s.Terms
if s.Contact.Name != "" || s.Contact.Email != "" || s.Contact.URL != "" {
var contact spec.ContactInfo
contact.Name = s.Contact.Name
contact.Email = s.Contact.Email
contact.URL = s.Contact.URL
info.Contact = &contact
}
if s.License.Name != "" || s.License.URL != "" {
var license spec.License
license.Name = s.License.Name
license.URL = s.License.URL
info.License = &license
}
for _, cons := range s.Consumes {
doc.Consumes = append(doc.Consumes, cons)
}
for _, prods := range s.Produces {
doc.Produces = append(doc.Produces, prods)
}
for _, scheme := range s.Schemes {
doc.Schemes = append(doc.Schemes, scheme)
}
if s.Format == "json" {
enc := json.NewEncoder(file)
if err := enc.Encode(doc); err != nil {
return err
}
return nil
}
b, err := yaml.Marshal(swag.ToDynamicJSON(doc))
if err != nil {
return err
}
if _, err := file.Write(b); err != nil {
return err
}
return nil
}