Skip to content

Commit

Permalink
fix: imported by useless package with duplicate names
Browse files Browse the repository at this point in the history
  • Loading branch information
Bin-Huang committed Aug 2, 2022
1 parent 523e67a commit bc82b54
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
19 changes: 18 additions & 1 deletion generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ var templ = `// Code generated by github.com/Bin-Huang/make-constructor; DO NOT
package {{.PkgName}}
import (
{{ range .Imports }}
{{ if .Name }}
{{.Name}} {{.Path}}
{{ else }}
{{.Path}}
{{ end }}
{{ end }}
)
{{ range .Constructors }}
// {{.Name}} Create a new {{.Struct}}
func {{.Name}}({{.Params}}) *{{.Struct}} {
Expand Down Expand Up @@ -70,13 +80,20 @@ func generateCode(pkgName string, importResuts []ResultImport, results []Result)

// FormatSource ...
func FormatSource(source []byte) ([]byte, error) {
return imports.Process("", source, &imports.Options{
output, err := imports.Process("", source, &imports.Options{
AllErrors: true,
Comments: true,
TabIndent: true,
TabWidth: 8,
Fragment: true,
})
if err != nil {
return output, err
}
if bytes.Equal(source, output) {
return output, nil
}
return FormatSource(output)
}

// UniqueImports remove duplicate imports, return unqiue imports
Expand Down
9 changes: 9 additions & 0 deletions test/pkg/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package errors

// NoFound ...
type NoFound struct{}

// String ...
func (NoFound) String() string {
return "no found"
}
4 changes: 3 additions & 1 deletion test/repositories/constructor_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion test/repositories/pro-repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package repositories

import "github.com/Bin-Huang/make-constructor/test/config"
import (
"github.com/Bin-Huang/make-constructor/test/config"
"github.com/Bin-Huang/make-constructor/test/pkg/errors"
)

// ProRepository a repository for example
//go:generate go run ../../../make-constructor
Expand All @@ -10,3 +13,8 @@ type ProRepository struct {
TableName string
version int
}

// FindByID find something by id
func (r *ProRepository) FindByID() errors.NoFound {
return errors.NoFound{}
}
11 changes: 8 additions & 3 deletions test/repositories/user-repository.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package repositories

import "github.com/Bin-Huang/make-constructor/test/config2"
import (
"errors"

config2 "github.com/Bin-Huang/make-constructor/test/config2"
)

// UserRepository the user repository for example
//go:generate go run ../../../make-constructor
type UserRepository struct {
conf *config.Config
conf *config2.Config
db *database
TableName string
}

// FindByID find something by id
func (r *UserRepository) FindByID() {
func (r *UserRepository) FindByID() error {
return errors.New("no found")
}

0 comments on commit bc82b54

Please sign in to comment.