Skip to content

Commit

Permalink
Rename Path to more specific SourcePath
Browse files Browse the repository at this point in the history
Make it easier to distinguish between a generated path and a source path.
  • Loading branch information
jschaf committed Apr 18, 2021
1 parent 2e048ef commit 71a9d1d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 27 deletions.
16 changes: 10 additions & 6 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ func connectPostgres(
func parseQueryFiles(queryFiles []string, inferrer *pginfer.Inferrer) ([]codegen.QueryFile, error) {
files := make([]codegen.QueryFile, len(queryFiles))
for i, file := range queryFiles {
queryFile, err := parseQueries(file, inferrer)
srcPath, err := filepath.Abs(file)
if err != nil {
return nil, fmt.Errorf("resovle absolute path for %q: %w", file, err)
}
queryFile, err := parseQueries(srcPath, inferrer)
if err != nil {
return nil, fmt.Errorf("parse template query file %q: %w", file, err)
}
Expand All @@ -199,10 +203,10 @@ func parseQueryFiles(queryFiles []string, inferrer *pginfer.Inferrer) ([]codegen
return files, nil
}

func parseQueries(file string, inferrer *pginfer.Inferrer) (codegen.QueryFile, error) {
astFile, err := parser.ParseFile(gotok.NewFileSet(), file, nil, 0)
func parseQueries(srcPath string, inferrer *pginfer.Inferrer) (codegen.QueryFile, error) {
astFile, err := parser.ParseFile(gotok.NewFileSet(), srcPath, nil, 0)
if err != nil {
return codegen.QueryFile{}, fmt.Errorf("parse query file %q: %w", file, err)
return codegen.QueryFile{}, fmt.Errorf("parse query file %q: %w", srcPath, err)
}

// Check for duplicate query names and bad queries.
Expand Down Expand Up @@ -233,7 +237,7 @@ func parseQueries(file string, inferrer *pginfer.Inferrer) (codegen.QueryFile, e
queries = append(queries, typedQuery)
}
return codegen.QueryFile{
Path: file,
Queries: queries,
SourcePath: srcPath,
Queries: queries,
}, nil
}
4 changes: 2 additions & 2 deletions internal/codegen/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (

// QueryFile represents all of the SQL queries from a single file.
type QueryFile struct {
Path string // the path to the source SQL query file
Queries []pginfer.TypedQuery // the typed queries
SourcePath string // absolute path to the source SQL query file
Queries []pginfer.TypedQuery // the typed queries
}
2 changes: 1 addition & 1 deletion internal/codegen/golang/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewEmitter(outDir string, tmpl *template.Template) Emitter {

// EmitQueryFile emits a single query file.
func (em Emitter) EmitQueryFile(tf TemplatedFile) (mErr error) {
base := filepath.Base(tf.Path)
base := filepath.Base(tf.SourcePath)
out := filepath.Join(em.outDir, base+".go")
file, err := os.OpenFile(out, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
defer errs.Capture(&mErr, file.Close, "close emit query file")
Expand Down
2 changes: 1 addition & 1 deletion internal/codegen/golang/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Generate(opts GenerateOptions, queryFiles []codegen.QueryFile) error {

// Order for reproducible results.
sort.Slice(templatedFiles, func(i, j int) bool {
return templatedFiles[i].Path < templatedFiles[j].Path
return templatedFiles[i].SourcePath < templatedFiles[j].SourcePath
})

// Link each child to the package. Necessary so the leader can define all
Expand Down
12 changes: 6 additions & 6 deletions internal/codegen/golang/templated_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ type TemplatedPackage struct {
// TemplatedFile is the Go version of a SQL query file with all information
// needed to execute the codegen template.
type TemplatedFile struct {
Pkg TemplatedPackage // the parent package containing this file
PkgPath string // full package path, like "github.com/foo/bar"
GoPkg string // the name of the Go package to use for the generated file
Path string // the path to source SQL file
Queries []TemplatedQuery // the queries with all template information
Imports []string // Go imports
Pkg TemplatedPackage // the parent package containing this file
PkgPath string // full package path, like "github.com/foo/bar"
GoPkg string // the name of the Go package to use for the generated file
SourcePath string // absolute path to source SQL file
Queries []TemplatedQuery // the queries with all template information
Imports []string // Go imports
// True if this file is the leader file. The leader defines common code used
// by by all queries in the same directory. Only one leader per directory.
IsLeader bool
Expand Down
20 changes: 10 additions & 10 deletions internal/codegen/golang/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (tm Templater) TemplateAll(files []codegen.QueryFile) ([]TemplatedFile, err
for _, queryFile := range files {
goFile, decls, err := tm.templateFile(queryFile)
if err != nil {
return nil, fmt.Errorf("template query file %s for go: %w", queryFile.Path, err)
return nil, fmt.Errorf("template query file %s for go: %w", queryFile.SourcePath, err)
}
goQueryFiles = append(goQueryFiles, goFile)
declarers = append(declarers, decls...)
Expand All @@ -52,9 +52,9 @@ func (tm Templater) TemplateAll(files []codegen.QueryFile) ([]TemplatedFile, err
firstIndex := -1
firstName := string(unicode.MaxRune)
for i, goFile := range goQueryFiles {
if goFile.Path < firstName {
if goFile.SourcePath < firstName {
firstIndex = i
firstName = goFile.Path
firstName = goFile.SourcePath
}
}
goQueryFiles[firstIndex].IsLeader = true
Expand Down Expand Up @@ -94,7 +94,7 @@ func (tm Templater) TemplateAll(files []codegen.QueryFile) ([]TemplatedFile, err

// Remove self imports.
for i, file := range goQueryFiles {
selfPkg, err := gomod.GuessPackage(file.Path)
selfPkg, err := gomod.GuessPackage(file.SourcePath)
if err != nil || selfPkg == "" {
continue // ignore error, assume it's not a self import
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (tm Templater) templateFile(file codegen.QueryFile) (TemplatedFile, []Decla
// resolving the package isn't perfect. We'll fallback to an unqualified
// type which will likely work since the type is probably declared in this
// package.
if pkg, err := gomod.GuessPackage(file.Path); err == nil {
if pkg, err := gomod.GuessPackage(file.SourcePath); err == nil {
pkgPath = pkg
}

Expand Down Expand Up @@ -210,11 +210,11 @@ func (tm Templater) templateFile(file codegen.QueryFile) (TemplatedFile, []Decla
}

return TemplatedFile{
PkgPath: pkgPath,
GoPkg: tm.pkg,
Path: file.Path,
Queries: queries,
Imports: imports.SortedPackages(),
PkgPath: pkgPath,
GoPkg: tm.pkg,
SourcePath: file.SourcePath,
Queries: queries,
Imports: imports.SortedPackages(),
}, declarers, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pginfer/pginfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type TypedQuery struct {
// The output columns of the query.
Outputs []OutputColumn
// Qualified protocol buffer message type to use for each output row, like
//"erp.api.Product". If empty, generate our own Row type.
// "erp.api.Product". If empty, generate our own Row type.
ProtobufType string
}

Expand Down

0 comments on commit 71a9d1d

Please sign in to comment.