diff --git a/gen/genv3.go b/gen/genv3.go deleted file mode 100644 index 8010660f2..000000000 --- a/gen/genv3.go +++ /dev/null @@ -1,202 +0,0 @@ -package gen - -import ( - "bytes" - "fmt" - "io" - "os" - "path" - "path/filepath" - "strings" - "text/template" - "time" - - "github.com/sv-tools/openapi/spec" - "github.com/swaggo/swag" -) - -type openAPITypeWriter func(*Config, *spec.OpenAPI) error - -func (g *Gen) writeDocOpenAPI(config *Config, openAPI *spec.OpenAPI) error { - var filename = "docs.go" - - if config.InstanceName != swag.Name { - filename = config.InstanceName + "_" + filename - } - - docFileName := path.Join(config.OutputDir, filename) - - absOutputDir, err := filepath.Abs(config.OutputDir) - if err != nil { - return err - } - - packageName := filepath.Base(absOutputDir) - - docs, err := os.Create(docFileName) - if err != nil { - return err - } - defer docs.Close() - - // Write doc - err = g.writeGoDocV3(packageName, docs, openAPI, config) - if err != nil { - return err - } - - g.debug.Printf("create docs.go at %+v", docFileName) - - return nil -} - -func (g *Gen) writeJSONOpenAPI(config *Config, swagger *spec.OpenAPI) error { - var filename = "swagger.json" - - if config.InstanceName != swag.Name { - filename = config.InstanceName + "_" + filename - } - - jsonFileName := path.Join(config.OutputDir, filename) - - b, err := g.jsonIndent(swagger) - if err != nil { - return err - } - - err = g.writeFile(b, jsonFileName) - if err != nil { - return err - } - - g.debug.Printf("create swagger.json at %+v", jsonFileName) - - return nil -} - -func (g *Gen) writeYAMLOpenAPI(config *Config, swagger *spec.OpenAPI) error { - var filename = "swagger.yaml" - - if config.InstanceName != swag.Name { - filename = config.InstanceName + "_" + filename - } - - yamlFileName := path.Join(config.OutputDir, filename) - - b, err := g.json(swagger) - if err != nil { - return err - } - - y, err := g.jsonToYAML(b) - if err != nil { - return fmt.Errorf("cannot covert json to yaml error: %s", err) - } - - err = g.writeFile(y, yamlFileName) - if err != nil { - return err - } - - g.debug.Printf("create swagger.yaml at %+v", yamlFileName) - - return nil -} - -func (g *Gen) writeGoDocV3(packageName string, output io.Writer, openAPI *spec.OpenAPI, config *Config) error { - generator, err := template.New("swagger_info").Funcs(template.FuncMap{ - "printDoc": func(v string) string { - // Add schemes - v = "{\n \"schemes\": {{ marshal .Schemes }}," + v[1:] - // Sanitize backticks - return strings.Replace(v, "`", "`+\"`\"+`", -1) - }, - }).Parse(packageTemplateV3) - if err != nil { - return err - } - - openAPISpec := spec.OpenAPI{ - Components: openAPI.Components, - OpenAPI: openAPI.OpenAPI, - Info: &spec.Extendable[spec.Info]{ - Spec: &spec.Info{ - Description: "{{escape .Description}}", - Title: "{{.Title}}", - Version: "{{.Version}}", - TermsOfService: openAPI.Info.Spec.TermsOfService, - Contact: openAPI.Info.Spec.Contact, - License: openAPI.Info.Spec.License, - Summary: openAPI.Info.Spec.Summary, - }, - Extensions: openAPI.Info.Extensions, - }, - ExternalDocs: openAPI.ExternalDocs, - Paths: openAPI.Paths, - WebHooks: openAPI.WebHooks, - JsonSchemaDialect: openAPI.JsonSchemaDialect, - Security: openAPI.Security, - Tags: openAPI.Tags, - Servers: openAPI.Servers, - } - - // crafted docs.json - buf, err := g.jsonIndent(openAPISpec) - if err != nil { - return err - } - - buffer := &bytes.Buffer{} - - err = generator.Execute(buffer, struct { - Timestamp time.Time - Doc string - PackageName string - Title string - Description string - Version string - InstanceName string - GeneratedTime bool - }{ - Timestamp: time.Now(), - GeneratedTime: config.GeneratedTime, - Doc: string(buf), - PackageName: packageName, - Title: openAPI.Info.Spec.Title, - Description: openAPI.Info.Spec.Description, - Version: openAPI.Info.Spec.Version, - InstanceName: config.InstanceName, - }) - if err != nil { - return err - } - - code := g.formatSource(buffer.Bytes()) - - // write - _, err = output.Write(code) - - return err -} - -var packageTemplateV3 = `// Code generated by swaggo/swag{{ if .GeneratedTime }} at {{ .Timestamp }}{{ end }}. DO NOT EDIT - -package docs - -import "github.com/swaggo/swag" - -const docTemplate{{ if ne .InstanceName "swagger" }}{{ .InstanceName }} {{- end }} = ` + "`{{ printDoc .Doc}}`" + ` - -// SwaggerInfo{{ if ne .InstanceName "swagger" }}{{ .InstanceName }} {{- end }} holds exported Swagger Info so clients can modify it -var SwaggerInfo{{ if ne .InstanceName "swagger" }}{{ .InstanceName }} {{- end }} = &swag.Spec{ - Version: {{ printf "%q" .Version}}, - Title: {{ printf "%q" .Title}}, - Description: {{ printf "%q" .Description}}, - InfoInstanceName: {{ printf "%q" .InstanceName }}, - SwaggerTemplate: docTemplate{{ if ne .InstanceName "swagger" }}{{ .InstanceName }} {{- end }}, -} - -func init() { - swag.Register(SwaggerInfo{{ if ne .InstanceName "swagger" }}{{ .InstanceName }} {{- end }}.InstanceName(), SwaggerInfo{{ if ne .InstanceName "swagger" }}{{ .InstanceName }} {{- end }}) -} -`