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
1514const (
@@ -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
5367func (c * Converter ) typeForMethod () types.Type {
@@ -97,7 +111,7 @@ func defaultOutputFile(name string) string {
97111}
98112
99113func 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.\n Move 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.\n Move the extend below the output:format setting." )
187+ return fmt .Errorf ("Cannot change output:format after extend functions have been added.\n Move 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.\n Move 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
0 commit comments