Skip to content

Commit f02110c

Browse files
committed
fix: allow unexported :extend for inferred packages
1 parent 635b101 commit f02110c

9 files changed

+109
-35
lines changed

config/converter.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/jmattheis/goverter/config/parse"
1010
"github.com/jmattheis/goverter/enum"
1111
"github.com/jmattheis/goverter/method"
12-
"github.com/jmattheis/goverter/pkgload"
1312
)
1413

1514
const (
@@ -46,8 +45,23 @@ type Converter struct {
4645
FileName string
4746
typ types.Type
4847
Methods []*Method
49-
5048
Location string
49+
50+
packageConfigured bool
51+
}
52+
53+
func (c *Converter) inferOutputPackage(ctx *context) {
54+
if !c.packageConfigured || c.OutputPackagePath == "" {
55+
if targetPackage, err := resolvePackage(c.FileName, c.Package, c.OutputFile); err == nil {
56+
c.OutputPackagePath = targetPackage
57+
}
58+
}
59+
60+
if !c.packageConfigured || c.OutputPackageName == "" {
61+
if pkg := ctx.Loader.GetUncheckedPkg(c.OutputPackagePath); pkg != nil {
62+
c.OutputPackageName = pkg.Types.Name()
63+
}
64+
}
5165
}
5266

5367
func (c *Converter) typeForMethod() types.Type {
@@ -97,7 +111,7 @@ func defaultOutputFile(name string) string {
97111
}
98112

99113
func parseConverter(ctx *context, rawConverter *RawConverter, global RawLines) (*Converter, error) {
100-
c, err := initConverter(ctx.Loader, rawConverter)
114+
c, err := initConverter(ctx, rawConverter)
101115
if err != nil {
102116
return nil, err
103117
}
@@ -109,34 +123,11 @@ func parseConverter(ctx *context, rawConverter *RawConverter, global RawLines) (
109123
return nil, err
110124
}
111125

112-
resolveOutputPackage(ctx, c)
113-
114126
err = parseMethods(ctx, rawConverter, c)
115127
return c, err
116128
}
117129

118-
func resolveOutputPackage(ctx *context, c *Converter) {
119-
targetPackage, err := resolvePackage(c.FileName, c.Package, c.OutputFile)
120-
if err != nil {
121-
return
122-
}
123-
124-
if c.OutputPackagePath == "" {
125-
c.OutputPackagePath = targetPackage
126-
}
127-
128-
pkg := ctx.Loader.GetUncheckedPkg(targetPackage)
129-
130-
if pkg == nil {
131-
return
132-
}
133-
134-
if c.OutputPackageName == "" {
135-
c.OutputPackageName = pkg.Types.Name()
136-
}
137-
}
138-
139-
func initConverter(loader *pkgload.PackageLoader, rawConverter *RawConverter) (*Converter, error) {
130+
func initConverter(ctx *context, rawConverter *RawConverter) (*Converter, error) {
140131
c := &Converter{
141132
FileName: rawConverter.FileName,
142133
Package: rawConverter.PackagePath,
@@ -145,13 +136,14 @@ func initConverter(loader *pkgload.PackageLoader, rawConverter *RawConverter) (*
145136

146137
if rawConverter.InterfaceName != "" {
147138
c.ConverterConfig = DefaultConfigInterface
148-
_, interfaceObj, err := loader.GetOneRaw(c.Package, rawConverter.InterfaceName)
139+
_, interfaceObj, err := ctx.Loader.GetOneRaw(c.Package, rawConverter.InterfaceName)
149140
if err != nil {
150141
return nil, err
151142
}
152143

153144
c.typ = interfaceObj.Type()
154145
c.Name = rawConverter.InterfaceName + "Impl"
146+
c.inferOutputPackage(ctx)
155147
return c, nil
156148
}
157149

@@ -185,10 +177,14 @@ func parseConverterLine(ctx *context, c *Converter, value string) (err error) {
185177
case "output:raw":
186178
c.OutputRaw = append(c.OutputRaw, rest)
187179
case configOutputFile:
180+
if len(c.Extend) != 0 {
181+
return fmt.Errorf("Cannot change output:file after extend functions have been added.\nMove the extend below the output:* setting.")
182+
}
188183
c.OutputFile, err = parse.File(ctx.WorkDir, rest)
184+
c.inferOutputPackage(ctx)
189185
case "output:format":
190186
if len(c.Extend) != 0 {
191-
return fmt.Errorf("Cannot change output:format after extend functions have been added.\nMove the extend below the output:format setting.")
187+
return fmt.Errorf("Cannot change output:format after extend functions have been added.\nMove the extend below the output:* setting.")
192188
}
193189

194190
c.OutputFormat, err = parse.Enum(false, rest, FormatFunction, FormatStruct, FormatVariable)
@@ -203,6 +199,11 @@ func parseConverterLine(ctx *context, c *Converter, value string) (err error) {
203199
return fmt.Errorf("unsupported format for goverter:converter")
204200
}
205201
case "output:package":
202+
if len(c.Extend) != 0 {
203+
return fmt.Errorf("Cannot change output:package after extend functions have been added.\nMove the extend below the output:* setting.")
204+
}
205+
c.packageConfigured = true
206+
206207
c.OutputPackageName = ""
207208
var pkg string
208209
pkg, err = parse.String(rest)
@@ -215,6 +216,7 @@ func parseConverterLine(ctx *context, c *Converter, value string) (err error) {
215216
case 1:
216217
c.OutputPackagePath = parts[0]
217218
}
219+
c.inferOutputPackage(ctx)
218220
case "struct:comment":
219221
if err = c.requireStruct(); err != nil {
220222
return err

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import GH from './GH.vue';
66

77
## unreleased
88

9+
- Allow unexported [`extend`](./reference/extend.md) functions with inferred
10+
[`output:package`](./reference/output.md) from [`output:file`](./reference/output.md).
11+
<GH issue="218" pr="219"/>
12+
913
## v1.9.2
1014

1115
- Fix comparison of signatures with aliases in
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
input:
2+
input.go: |
3+
package execution
4+
5+
// goverter:converter
6+
// goverter:output:file ./generated.go
7+
// goverter:extend intToString
8+
type Converter interface {
9+
Convert(source Input) Output
10+
}
11+
func intToString(int) string {
12+
return ""
13+
}
14+
15+
type Input struct {
16+
Age int
17+
}
18+
type Output struct {
19+
Age string
20+
}
21+
success:
22+
- generated.go: |
23+
// Code generated by github.com/jmattheis/goverter, DO NOT EDIT.
24+
25+
package execution
26+
27+
type ConverterImpl struct{}
28+
29+
func (c *ConverterImpl) Convert(source Input) Output {
30+
var executionOutput Output
31+
executionOutput.Age = intToString(source.Age)
32+
return executionOutput
33+
}

scenario/fmtfunc_use_interface2.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ error: |-
2626
github.com/jmattheis/goverter/execution.Converter
2727
2828
Cannot change output:format after extend functions have been added.
29-
Move the extend below the output:format setting.
29+
Move the extend below the output:* setting.

scenario/into_source_package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ input:
33
package structs
44
55
// goverter:converter
6-
// goverter:extend StringPToString
76
// goverter:output:file ./generated.go
87
// goverter:output:package github.com/jmattheis/goverter/execution:structs
8+
// goverter:extend StringPToString
99
type Converter interface {
1010
ConvertPerson(source Input) Output
1111
}

scenario/into_source_package_infer_full.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ input:
33
package structs
44
55
// goverter:converter
6-
// goverter:extend StringPToString
76
// goverter:output:file ./generated.go
7+
// goverter:extend StringPToString
88
type Converter interface {
99
ConvertPerson(source Input) Output
1010
}

scenario/into_source_package_infer_path.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ input:
33
package structs
44
55
// goverter:converter
6-
// goverter:extend StringPToString
76
// goverter:output:file ./generated.go
87
// goverter:output:package :structs
8+
// goverter:extend StringPToString
99
type Converter interface {
1010
ConvertPerson(source Input) Output
1111
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
input:
2+
input.go: |
3+
package structs
4+
5+
// goverter:converter
6+
// goverter:output:package pkg1:cde
7+
type Converter interface {
8+
Convert(source Input) Output
9+
}
10+
11+
// goverter:converter
12+
// goverter:output:package pkg1:abc
13+
type Converter2 interface {
14+
Convert(source Input) Output
15+
}
16+
17+
type Input struct {
18+
ID int
19+
}
20+
type Output struct {
21+
ID int
22+
}
23+
error: |-
24+
Error creating converters
25+
@workdir/input.go:5
26+
github.com/jmattheis/goverter/execution.Converter
27+
and
28+
@workdir/input.go:11
29+
github.com/jmattheis/goverter/execution.Converter2
30+
31+
Cannot use different packages
32+
pkg1:cde
33+
pkg1:abc
34+
in the same output file:
35+
@workdir/generated/generated.go

scenario/var_output_package_mismatch.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ error: |-
2525
var definition
2626
2727
Cannot use different packages
28-
pkg2:structs
29-
pkg1:structs
28+
pkg2
29+
pkg1
3030
in the same output file:
3131
@workdir/input.gen.go

0 commit comments

Comments
 (0)