diff --git a/generator/filemanager.go b/generator/filemanager.go index 8e0f37ec..7a8c9b2b 100644 --- a/generator/filemanager.go +++ b/generator/filemanager.go @@ -16,6 +16,7 @@ type fileManager struct { type managedFile struct { Package string + Initial *config.Converter Content *jen.File } @@ -26,6 +27,7 @@ func (m *fileManager) Get(c *config.Converter, workDirectory string) (*jen.File, if !ok { f = &managedFile{ Package: c.OutputPackage, + Initial: c, } parts := strings.SplitN(c.OutputPackage, ":", 2) @@ -43,7 +45,8 @@ func (m *fileManager) Get(c *config.Converter, workDirectory string) (*jen.File, } if f.Package != c.OutputPackage { - return nil, fmt.Errorf("damn") // TODO fix + return nil, fmt.Errorf("Error creating converters\n %s\n %s\nand\n %s\n %s\n\nCannot use different packages\n %s\n %s\nin the same output file:\n %s", + c.FileSource, c.Type, f.Initial.FileSource, f.Initial.Type, c.OutputPackage, f.Initial.OutputPackage, output) } return f.Content, nil @@ -63,7 +66,7 @@ func (m *fileManager) renderFiles() (map[string][]byte, error) { func getOutputDir(c *config.Converter, cwd string) string { if strings.HasPrefix(c.OutputFile, "@cwd/") { - return filepath.Join(cwd, c.OutputFile) + return filepath.Join(cwd, strings.TrimPrefix(c.OutputFile, "@cwd/")) } return filepath.Join(filepath.Dir(c.FileSource), c.OutputFile) } diff --git a/runner_test.go b/runner_test.go index d7bec812..1fbb7bce 100644 --- a/runner_test.go +++ b/runner_test.go @@ -50,10 +50,15 @@ func TestScenario(t *testing.T) { global := append([]string{"outputPackage github.com/jmattheis/goverter/execution/" + genPkgName}, scenario.Global...) + patterns := scenario.Patterns + if len(patterns) == 0 { + patterns = append(patterns, "github.com/jmattheis/goverter/execution") + } + files, err := generateConvertersRaw( &GenerateConfig{ WorkingDir: execDir, - PackagePatterns: []string{"github.com/jmattheis/goverter/execution"}, + PackagePatterns: patterns, Global: config.RawLines{ Lines: global, Location: "scenario global", @@ -138,7 +143,8 @@ type Scenario struct { Input map[string]string `yaml:"input"` Global []string `yaml:"global,omitempty"` - Success []*OutputFile `yaml:"success,omitempty"` + Patterns []string `yaml:"patterns,omitempty"` + Success []*OutputFile `yaml:"success,omitempty"` // for error cases, use either Error or ErrorStartsWith, not both Error string `yaml:"error,omitempty"` ErrorStartsWith string `yaml:"error_starts_with,omitempty"` diff --git a/scenario/cwd.yml b/scenario/cwd.yml new file mode 100644 index 00000000..53863ef6 --- /dev/null +++ b/scenario/cwd.yml @@ -0,0 +1,33 @@ +input: + pkg1/input.go: | + package pkg1 + + // goverter:converter + // goverter:output @cwd/generated/output.go + type Converter interface { + Convert(source Input) Output + } + + type Input struct { + ID int + } + type Output struct { + ID int + } +patterns: + - github.com/jmattheis/goverter/execution/pkg1 +success: + - generated/output.go: | + // Code generated by github.com/jmattheis/goverter, DO NOT EDIT. + + package generated + + import pkg1 "github.com/jmattheis/goverter/execution/pkg1" + + type ConverterImpl struct{} + + func (c *ConverterImpl) Convert(source pkg1.Input) pkg1.Output { + var pkg1Output pkg1.Output + pkg1Output.ID = source.ID + return pkg1Output + } diff --git a/scenario/multiple_package_one_output.yml b/scenario/multiple_package_one_output.yml new file mode 100644 index 00000000..7fd5ac68 --- /dev/null +++ b/scenario/multiple_package_one_output.yml @@ -0,0 +1,56 @@ +input: + model/model.go: | + package model + + type Input struct { + ID int + } + type Output struct { + ID int + } + pkg1/input.go: | + package pkg1 + + import model "github.com/jmattheis/goverter/execution/model" + + // goverter:converter + // goverter:output ../generated/output.go + type Converter interface { + Convert(source model.Input) model.Output + } + pkg2/input.go: | + package pkg2 + + import model "github.com/jmattheis/goverter/execution/model" + + // goverter:converter + // goverter:output ../generated/output.go + type Converter2 interface { + Convert(source model.Input) model.Output + } +patterns: + - github.com/jmattheis/goverter/execution/pkg1 + - github.com/jmattheis/goverter/execution/pkg2 +success: + - generated/output.go: | + // Code generated by github.com/jmattheis/goverter, DO NOT EDIT. + + package generated + + import model "github.com/jmattheis/goverter/execution/model" + + type Converter2Impl struct{} + + func (c *Converter2Impl) Convert(source model.Input) model.Output { + var modelOutput model.Output + modelOutput.ID = source.ID + return modelOutput + } + + type ConverterImpl struct{} + + func (c *ConverterImpl) Convert(source model.Input) model.Output { + var modelOutput model.Output + modelOutput.ID = source.ID + return modelOutput + } diff --git a/scenario/multiple_package_one_output_wildcard.yml b/scenario/multiple_package_one_output_wildcard.yml new file mode 100644 index 00000000..7082a134 --- /dev/null +++ b/scenario/multiple_package_one_output_wildcard.yml @@ -0,0 +1,55 @@ +input: + model/model.go: | + package model + + type Input struct { + ID int + } + type Output struct { + ID int + } + pkg1/input.go: | + package pkg1 + + import model "github.com/jmattheis/goverter/execution/model" + + // goverter:converter + // goverter:output ../generated/output.go + type Converter interface { + Convert(source model.Input) model.Output + } + pkg2/input.go: | + package pkg2 + + import model "github.com/jmattheis/goverter/execution/model" + + // goverter:converter + // goverter:output ../generated/output.go + type Converter2 interface { + Convert(source model.Input) model.Output + } +patterns: + - github.com/jmattheis/goverter/execution/... +success: + - generated/output.go: | + // Code generated by github.com/jmattheis/goverter, DO NOT EDIT. + + package generated + + import model "github.com/jmattheis/goverter/execution/model" + + type Converter2Impl struct{} + + func (c *Converter2Impl) Convert(source model.Input) model.Output { + var modelOutput model.Output + modelOutput.ID = source.ID + return modelOutput + } + + type ConverterImpl struct{} + + func (c *ConverterImpl) Convert(source model.Input) model.Output { + var modelOutput model.Output + modelOutput.ID = source.ID + return modelOutput + } diff --git a/scenario/output_package_mismatch.yml b/scenario/output_package_mismatch.yml new file mode 100644 index 00000000..0368fcdc --- /dev/null +++ b/scenario/output_package_mismatch.yml @@ -0,0 +1,35 @@ +input: + input.go: | + package structs + + // goverter:converter + // goverter:outputPackage pkg1 + type Converter interface { + Convert(source Input) Output + } + + // goverter:converter + // goverter:outputPackage pkg2 + type Converter2 interface { + Convert(source Input) Output + } + + type Input struct { + ID int + } + type Output struct { + ID int + } +error: |- + Error creating converters + /ABSOLUTE/execution/input.go + github.com/jmattheis/goverter/execution.Converter + and + /ABSOLUTE/execution/input.go + github.com/jmattheis/goverter/execution.Converter2 + + Cannot use different packages + pkg1 + pkg2 + in the same output file: + /ABSOLUTE/execution/generated/generated.go