Skip to content

Commit

Permalink
fix: multiple input one output and package mismatches
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed Oct 22, 2023
1 parent c8046ce commit 8f7cb9e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
5 changes: 4 additions & 1 deletion generator/filemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type fileManager struct {

type managedFile struct {
Package string
Initial *config.Converter
Content *jen.File
}

Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestScenario(t *testing.T) {
files, err := generateConvertersRaw(
&GenerateConfig{
WorkingDir: execDir,
PackagePatterns: []string{"github.com/jmattheis/goverter/execution"},
PackagePatterns: []string{"github.com/jmattheis/goverter/execution/..."},
Global: config.RawLines{
Lines: global,
Location: "scenario global",
Expand Down
53 changes: 53 additions & 0 deletions scenario/multiple_input_one_output.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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
}
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
}
35 changes: 35 additions & 0 deletions scenario/output_package_mismatch.yml
Original file line number Diff line number Diff line change
@@ -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 ouput file:
/ABSOLUTE/execution/generated/generated.go

0 comments on commit 8f7cb9e

Please sign in to comment.