Created
March 13, 2021 16:58
-
-
Save marcusirgens/48fd364fa785e9c27fe88c116d2ff98f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by openapi-embed. DO NOT EDIT. | |
package {{.PackageName}} | |
import ( | |
"embed" | |
"io/fs" | |
) | |
//go:embed {{.Filename}} | |
var {{.Embed}} embed.FS | |
var {{.EmbedFilename}} = `{{.Filename}}` | |
func {{.EmbedFile}}() (fs.File, error) { | |
return {{.Embed}}.Open({{.EmbedFilename}}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// openapi-embed is used in conjunction with protoc-gen-openapiv2 (see | |
// https://github.com/grpc-ecosystem/grpc-gateway/) to embed the openapi files | |
// in the source. This allows services serving grpc-gateway to embed the OpenAPI | |
// definitions without having to generate those manually. | |
package main | |
import ( | |
"bytes" | |
_ "embed" | |
"flag" | |
"fmt" | |
"io" | |
"io/fs" | |
"log" | |
"os" | |
"path/filepath" | |
"strings" | |
"text/template" | |
) | |
//go:embed embed.go.tmpl | |
var tf string | |
var ( | |
dir *string | |
sfx = ".swagger.json" | |
tmpl = template.Must(template.New("embedfile").Parse(tf)) | |
) | |
func main() { | |
if err := filepath.Walk(*dir, func(path string, info fs.FileInfo, err error) error { | |
if info.IsDir() || !strings.HasSuffix(info.Name(), sfx) { | |
return nil | |
} | |
sf := SwaggerFile{Path: path} | |
return generate(sf) | |
}); err != nil { | |
log.Fatalln(err) | |
} | |
} | |
func generate(sf SwaggerFile) error { | |
var buf bytes.Buffer | |
if err := tmpl.Execute(&buf, sf); err != nil { | |
return err | |
} | |
f, err := os.Create(sf.GenPathname()) | |
if err != nil { | |
return err | |
} | |
if _, err := io.Copy(f, &buf); err != nil { | |
return err | |
} | |
return nil | |
} | |
// SwaggerFile is a struct containing the data about the swaggerfile. | |
type SwaggerFile struct { | |
Path string | |
} | |
// ProtoFilename returns the proto filename (inferred from the name of the | |
// swaggerfile). | |
func (f SwaggerFile) ProtoFilename() string { | |
bn := filepath.Base(f.Path) | |
return strings.TrimSuffix(bn, sfx) | |
} | |
// CamelProtoFilename returns the proto filename (inferred from the name of the | |
// swaggerfile) in CamelCase. | |
func (f SwaggerFile) CamelProtoFilename() string { | |
fn := f.ProtoFilename() | |
fn = strings.ReplaceAll(fn, "_", " ") | |
fn = strings.Title(fn) | |
return strings.ReplaceAll(fn, " ", "") | |
} | |
// Embed returns the name of the embed.FS for the swaggerfile, in the form | |
// MyServiceSwaggerEmbed. | |
func (f SwaggerFile) Embed() string { | |
fn := f.CamelProtoFilename() | |
return fmt.Sprintf("%sSwaggerEmbed", fn) | |
} | |
// EmbedFilename generates CamelProtoFilename suffixed by SwaggerFilename, for | |
// use as an exported variable containing the filename of the swaggerfile (see | |
// Filename) | |
func (f SwaggerFile) EmbedFilename() string { | |
fn := f.CamelProtoFilename() | |
return fmt.Sprintf("%sSwaggerFilename", fn) | |
} | |
// EmbedFile generates CamelProtoFilename suffixed by SwaggerFile, for use as | |
// a function name, eg MyServiceSwaggerFile(). | |
func (f SwaggerFile) EmbedFile() string { | |
fn := f.CamelProtoFilename() | |
return fmt.Sprintf("%sSwaggerFile", fn) | |
} | |
// Filename returns the basename of the swaggerfile. | |
func (f SwaggerFile) Filename() string { | |
return filepath.Base(f.Path) | |
} | |
// Dir returns the directory of the swaggerfile. | |
func (f SwaggerFile) Dir() string { | |
return filepath.Dir(f.Path) | |
} | |
// PackageName returns the package name. | |
func (f SwaggerFile) PackageName() string { | |
d := filepath.Dir(f.Path) | |
return filepath.Base(d) | |
} | |
// GenFilename returns the name of the generated file. | |
func (f SwaggerFile) GenFilename() string { | |
return fmt.Sprintf("%s.swaggerembed.go", f.ProtoFilename()) | |
} | |
// GenPathname returns the pathname of the generated file | |
func (f SwaggerFile) GenPathname() string { | |
return filepath.Join(f.Dir(), f.GenFilename()) | |
} | |
func init() { | |
dir = flag.String("dir", ".", "Directory to search for openapi files") | |
flag.Parse() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment