Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove GOPATH (breaking change for using wag outside of GOPATH) #423

Merged
merged 2 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions clients/go/gengo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
)

// Generate generates a client
func Generate(packageName, packagePath string, s spec.Swagger) error {
if err := generateClient(packageName, packagePath, s); err != nil {
func Generate(packageName, basePath string, s spec.Swagger) error {
if err := generateClient(packageName, basePath, s); err != nil {
return err
}
return generateInterface(packageName, packagePath, &s, s.Info.InfoProps.Title, s.Paths)
return generateInterface(packageName, basePath, &s, s.Info.InfoProps.Title, s.Paths)
}

type clientCodeTemplate struct {
Expand Down Expand Up @@ -197,8 +197,7 @@ func shortHash(s string) string {
}
`

func generateClient(packageName, packagePath string, s spec.Swagger) error {

func generateClient(packageName, basePath string, s spec.Swagger) error {
codeTemplate := clientCodeTemplate{
PackageName: packageName,
ServiceName: s.Info.InfoProps.Title,
Expand Down Expand Up @@ -227,7 +226,7 @@ func generateClient(packageName, packagePath string, s spec.Swagger) error {
return err
}

g := swagger.Generator{PackagePath: packagePath}
g := swagger.Generator{BasePath: basePath}
g.Printf(clientCode)
return g.WriteFile("client/client.go")
}
Expand All @@ -248,8 +247,8 @@ func IsBinaryParam(param spec.Parameter, definitions map[string]spec.Schema) boo
return definitions[definitionName].Format == "binary"
}

func generateInterface(packageName, packagePath string, s *spec.Swagger, serviceName string, paths *spec.Paths) error {
g := swagger.Generator{PackagePath: packagePath}
func generateInterface(packageName, basePath string, s *spec.Swagger, serviceName string, paths *spec.Paths) error {
g := swagger.Generator{BasePath: basePath}
g.Printf("package client\n\n")
g.Printf(swagger.ImportStatements([]string{"context", packageName + "/models"}))
g.Printf("//go:generate mockgen -source=$GOFILE -destination=mock_client.go -package=client\n\n")
Expand Down
78 changes: 28 additions & 50 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ type config struct {

dynamoPath string
goAbsolutePackagePath string
goClientPath string
goPackagePath string
jsClientPath string
modelsPath string
serverPath string
tracingPath string

generateDynamo bool
generateGoClient bool
Expand Down Expand Up @@ -95,31 +90,31 @@ func main() {
}

if conf.generateGoModels {
if err := generateGoModels(conf.modelsPath, conf.goPackagePath, swaggerSpec); err != nil {
if err := generateGoModels(conf.goAbsolutePackagePath, swaggerSpec); err != nil {
log.Fatal(err.Error())
}
}

if conf.generateServer {
if err := generateServer(conf.serverPath, *conf.goPackageName, conf.goPackagePath, swaggerSpec); err != nil {
if err := generateServer(*conf.goPackageName, conf.goAbsolutePackagePath, swaggerSpec); err != nil {
log.Fatal(err.Error())
}
}

if conf.generateTracing {
if err := generateTracing(conf.tracingPath, conf.goPackagePath); err != nil {
if err := generateTracing(conf.goAbsolutePackagePath); err != nil {
log.Fatal(err.Error())
}
}

if conf.generateDynamo {
if err := generateDynamo(conf.dynamoPath, *conf.goPackageName, conf.goPackagePath, *conf.relativeDynamoPath, swaggerSpec); err != nil {
if err := generateDynamo(conf.dynamoPath, *conf.goPackageName, conf.goAbsolutePackagePath, *conf.relativeDynamoPath, swaggerSpec); err != nil {
log.Fatal(err.Error())
}
}

if conf.generateGoClient {
if err := generateGoClient(conf.goClientPath, *conf.goPackageName, conf.goPackagePath, swaggerSpec); err != nil {
if err := generateGoClient(*conf.goPackageName, conf.goAbsolutePackagePath, swaggerSpec); err != nil {
log.Fatal(err.Error())
}
}
Expand All @@ -131,24 +126,24 @@ func main() {
}
}

func generateGoModels(modelsPath, goPackagePath string, swaggerSpec spec.Swagger) error {
if err := prepareDir(modelsPath); err != nil {
func generateGoModels(basePath string, swaggerSpec spec.Swagger) error {
if err := prepareDir(filepath.Join(basePath, "models")); err != nil {
return err
}
if err := models.Generate(goPackagePath, swaggerSpec); err != nil {
if err := models.Generate(basePath, swaggerSpec); err != nil {
return fmt.Errorf("Error generating models: %s", err)
}
return nil
}

func generateServer(serverPath, goPackageName, goPackagePath string, swaggerSpec spec.Swagger) error {
if err := prepareDir(serverPath); err != nil {
func generateServer(goPackageName, basePath string, swaggerSpec spec.Swagger) error {
if err := prepareDir(filepath.Join(basePath, "server")); err != nil {
return err
}
if err := server.Generate(goPackageName, goPackagePath, swaggerSpec); err != nil {
if err := server.Generate(goPackageName, basePath, swaggerSpec); err != nil {
return fmt.Errorf("Failed to generate server: %s", err)
}
middlewareGenerator := swagger.Generator{PackagePath: goPackagePath}
middlewareGenerator := swagger.Generator{BasePath: basePath}
middlewareGenerator.Write(hardcoded.MustAsset("../_hardcoded/middleware.go"))
if err := middlewareGenerator.WriteFile("server/middleware.go"); err != nil {
return fmt.Errorf("Failed to copy middleware.go: %s", err)
Expand All @@ -157,12 +152,12 @@ func generateServer(serverPath, goPackageName, goPackagePath string, swaggerSpec
return nil
}

func generateTracing(tracingPath, goPackagePath string) error {
if err := prepareDir(tracingPath); err != nil {
func generateTracing(basePath string) error {
if err := prepareDir(filepath.Join(basePath, "tracing")); err != nil {
return err
}

tracingGenerator := swagger.Generator{PackagePath: goPackagePath}
tracingGenerator := swagger.Generator{BasePath: basePath}
tracingGenerator.Write(hardcoded.MustAsset("../_hardcoded/tracing.go"))
if err := tracingGenerator.WriteFile("tracing/tracing.go"); err != nil {
log.Fatalf("Failed to copy tracing.go: %s", err)
Expand All @@ -171,24 +166,24 @@ func generateTracing(tracingPath, goPackagePath string) error {
return nil
}

func generateDynamo(dynamoPath, goPackageName, goPackagePath, outputPath string, swaggerSpec spec.Swagger) error {
func generateDynamo(dynamoPath, goPackageName, basePath, outputPath string, swaggerSpec spec.Swagger) error {
if err := prepareDir(dynamoPath); err != nil {
return err
}
if err := gendb.GenerateDB(goPackageName, goPackagePath, &swaggerSpec, outputPath); err != nil {
if err := gendb.GenerateDB(goPackageName, basePath, &swaggerSpec, outputPath); err != nil {
return fmt.Errorf("Failed to generate database: %s", err)
}
return nil
}

func generateGoClient(goClientPath, goPackageName, goPackagePath string, swaggerSpec spec.Swagger) error {
if err := prepareDir(goClientPath); err != nil {
func generateGoClient(goPackageName, basePath string, swaggerSpec spec.Swagger) error {
if err := prepareDir(filepath.Join(basePath, "client")); err != nil {
return err
}
if err := goclient.Generate(goPackageName, goPackagePath, swaggerSpec); err != nil {
if err := goclient.Generate(goPackageName, basePath, swaggerSpec); err != nil {
return fmt.Errorf("Failed generating go client %s", err)
}
doerGenerator := swagger.Generator{PackagePath: goPackagePath}
doerGenerator := swagger.Generator{BasePath: basePath}
doerGenerator.Write(hardcoded.MustAsset("../_hardcoded/doer.go"))
if err := doerGenerator.WriteFile("client/doer.go"); err != nil {
return fmt.Errorf("Failed to copy doer.go: %s", err)
Expand Down Expand Up @@ -273,20 +268,20 @@ func (c *config) setGoPaths(outputPath, goPackageName string) error {
return fmt.Errorf("go-package is required")
}
// if the repo does not use modules, the package name is equivalent to the package path
c.goPackagePath = goPackageName
c.goAbsolutePackagePath = filepath.Join(os.Getenv("GOPATH"), "src", goPackageName)
} else {
defer modFile.Close()
// TODO: do not rely on GOPATH when repo uses modules
goPath := os.Getenv("GOPATH")
if goPath == "" {
return fmt.Errorf("GOPATH must be set")
}
if outputPath == "" {
return fmt.Errorf("output-path is required")
}

c.goPackagePath = getModulePackagePath(goPath, path.Clean(outputPath))
absolutePath, err := filepath.Abs(outputPath)
if err != nil {
return fmt.Errorf("converting output-path to absolute path: %v", err)
}
c.goAbsolutePackagePath = absolutePath
*c.goPackageName = getModulePackageName(modFile, path.Clean(outputPath))

}
return nil
}
Expand Down Expand Up @@ -322,14 +317,6 @@ func (c *config) setClientLanguage(clientLanguage, jsModulePath string) error {
// setGeneratedFilePaths determines where to output the generated files.
func (c *config) setGeneratedFilePaths() {
const serverDir = "server"
const tracingDir = "tracing"

// determine paths for generated files
c.goAbsolutePackagePath = filepath.Join(os.Getenv("GOPATH"), "src", c.goPackagePath)
c.modelsPath = filepath.Join(c.goAbsolutePackagePath, "models")
c.serverPath = filepath.Join(c.goAbsolutePackagePath, serverDir)
c.tracingPath = filepath.Join(c.goAbsolutePackagePath, tracingDir)
c.goClientPath = filepath.Join(c.goAbsolutePackagePath, "client")

if c.generateDynamo {
// set path of generated dynamo code if none specified
Expand All @@ -344,15 +331,6 @@ func (c *config) setGeneratedFilePaths() {
}
}

func getModulePackagePath(goPath, outputPath string) string {
pwd, err := os.Getwd()
if err != nil {
log.Fatalf("Error getting current directory: %s", err.Error())
}
goSrcPath := fmt.Sprintf("%v%v", goPath, "/src/")
return path.Join(strings.TrimPrefix(pwd, goSrcPath), outputPath)
}

// getModulePackageName gets the package name of the generated code
// Example: if packagePath = github.com/Clever/wag/v8/gen-go and the module name is github.com/Clever/wag/v8/v2
// the function will return github.com/Clever/wag/v8/v2/gen-go
Expand Down
12 changes: 0 additions & 12 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func Test_config_validate(t *testing.T) {
outputPath: swag.String("output-path"),
goPackageName: swag.String("github.com/Clever/wag/v8/output-path"),
jsModulePath: swag.String("jsModulePath"),
goPackagePath: "github.com/Clever/wag/output-path",
relativeDynamoPath: swag.String("server/db"),
generateDynamo: true,
generateServer: true,
Expand All @@ -48,7 +47,6 @@ func Test_config_validate(t *testing.T) {
outputPath: swag.String("output-path"),
goPackageName: swag.String("github.com/Clever/wag/v8/output-path"),
jsModulePath: swag.String("jsModulePath"),
goPackagePath: "github.com/Clever/wag/output-path",
generateServer: false,
generateDynamo: false,
generateTracing: true,
Expand All @@ -70,7 +68,6 @@ func Test_config_validate(t *testing.T) {
clientLanguage: swag.String("go"),
outputPath: swag.String("output-path"),
goPackageName: swag.String("github.com/Clever/wag/v8/output-path"),
goPackagePath: "github.com/Clever/wag/output-path",
generateServer: false,
generateDynamo: false,
generateTracing: true,
Expand All @@ -94,7 +91,6 @@ func Test_config_validate(t *testing.T) {
outputPath: swag.String("output-path"),
goPackageName: swag.String("github.com/Clever/wag/v8/output-path"),
jsModulePath: swag.String("jsModulePath"),
goPackagePath: "github.com/Clever/wag/output-path",
generateServer: false,
generateDynamo: false,
generateTracing: false,
Expand All @@ -117,7 +113,6 @@ func Test_config_validate(t *testing.T) {
goPackageName: swag.String("github.com/Clever/wag/v8/output-path"),
jsModulePath: swag.String("jsModulePath"),
relativeDynamoPath: swag.String("server/db"),
goPackagePath: "github.com/Clever/wag/output-path",
generateServer: true,
generateDynamo: true,
generateTracing: true,
Expand All @@ -138,7 +133,6 @@ func Test_config_validate(t *testing.T) {
outputPath: swag.String("output-path"),
goPackageName: swag.String("github.com/Clever/wag/v8/output-path"),
relativeDynamoPath: swag.String("server/db"),
goPackagePath: "github.com/Clever/wag/output-path",
generateServer: true,
generateTracing: true,
generateGoClient: true,
Expand Down Expand Up @@ -177,7 +171,6 @@ func Test_config_validate(t *testing.T) {
output: config{
outputPath: swag.String("output-path"),
goPackageName: swag.String("github.com/Clever/wag/v8/output-path"),
goPackagePath: "github.com/Clever/wag/output-path",
relativeDynamoPath: swag.String("gen-db/db"),
dynamoOnly: swag.Bool(true),
generateServer: false,
Expand All @@ -198,7 +191,6 @@ func Test_config_validate(t *testing.T) {
output: config{
outputPath: swag.String("output-path"),
goPackageName: swag.String("github.com/Clever/wag/v8/output-path"),
goPackagePath: "github.com/Clever/wag/output-path",
relativeDynamoPath: swag.String("db"),
dynamoOnly: swag.Bool(true),
generateServer: false,
Expand All @@ -217,10 +209,6 @@ func Test_config_validate(t *testing.T) {
t.Errorf("config.validate() error = %v, wantErr %v", err, tt.wantErr)
} else if !tt.wantErr {
// clear paths so they are not diffed
tt.input.modelsPath = ""
tt.input.serverPath = ""
tt.input.tracingPath = ""
tt.input.goClientPath = ""
tt.input.jsClientPath = ""
tt.input.dynamoPath = ""
tt.input.goAbsolutePackagePath = ""
Expand Down
16 changes: 8 additions & 8 deletions models/genmodels.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

// Generate writes the files to the client directories
func Generate(packagePath string, s spec.Swagger) error {
func Generate(basePath string, s spec.Swagger) error {

tmpFile, err := swagger.WriteToFile(&s)
if err != nil {
Expand All @@ -29,7 +29,7 @@ func Generate(packagePath string, s spec.Swagger) error {
genopts := generator.GenOpts{
Spec: tmpFile,
ModelPackage: "models",
Target: fmt.Sprintf("%s/src/%s/", os.Getenv("GOPATH"), packagePath),
Target: basePath,
IncludeModel: true,
IncludeHandler: false,
IncludeSupport: false,
Expand All @@ -45,18 +45,18 @@ func Generate(packagePath string, s spec.Swagger) error {
return fmt.Errorf("error generating go-swagger models: %s", err)
}

if err := generateOutputs(packagePath, s); err != nil {
if err := generateOutputs(basePath, s); err != nil {
return fmt.Errorf("error generating outputs: %s", err)
}
if err := generateInputs(packagePath, s); err != nil {
if err := generateInputs(basePath, s); err != nil {
return fmt.Errorf("error generating inputs: %s", err)
}
return nil
}

func generateInputs(packagePath string, s spec.Swagger) error {
func generateInputs(basePath string, s spec.Swagger) error {

g := swagger.Generator{PackagePath: packagePath}
g := swagger.Generator{BasePath: basePath}

g.Printf(`
package models
Expand Down Expand Up @@ -305,8 +305,8 @@ var queryParamStr = `
{{end}}
`

func generateOutputs(packagePath string, s spec.Swagger) error {
g := swagger.Generator{PackagePath: packagePath}
func generateOutputs(basePath string, s spec.Swagger) error {
g := swagger.Generator{BasePath: basePath}

g.Printf("package models\n\n")

Expand Down
4 changes: 2 additions & 2 deletions server/gendb/gendb.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func findCompositeAttribute(config XDBConfig, attributeName string) *CompositeAt
}

// GenerateDB generates DB code for schemas annotated with the x-db extension.
func GenerateDB(packageName, packagePath string, s *spec.Swagger, outputPath string) error {
func GenerateDB(packageName, basePath string, s *spec.Swagger, outputPath string) error {
var schemaNames []string
for schemaName := range s.Definitions {
schemaNames = append(schemaNames, schemaName)
Expand Down Expand Up @@ -175,7 +175,7 @@ func GenerateDB(packageName, packagePath string, s *spec.Swagger, outputPath str
return err
}

g := swagger.Generator{PackagePath: packagePath}
g := swagger.Generator{BasePath: basePath}
g.Printf(tmpBuf.String())
return g.WriteFile(outputFilename)
}
Expand Down
Loading