@@ -216,7 +216,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
216216 }
217217 formatted := out
218218 if ! useSingleFile {
219- formatted , err = FormatCode (out )
219+ formatted , err = p . formatCode (out )
220220 if err != nil {
221221 return nil , err
222222 }
@@ -256,7 +256,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
256256 if err != nil {
257257 return nil , fmt .Errorf ("error generating code for %s: %w" , tmpl , err )
258258 }
259- formatted , err := FormatCode (out )
259+ formatted , err := p . formatCode (out )
260260 if err != nil {
261261 return nil , err
262262 }
@@ -272,7 +272,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
272272 }
273273 formatted := out
274274 if ! useSingleFile {
275- formatted , err = FormatCode (out )
275+ formatted , err = p . formatCode (out )
276276 if err != nil {
277277 return nil , fmt .Errorf ("error formatting %s: %w" , tmpl , err )
278278 }
@@ -306,7 +306,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
306306 }
307307 }
308308 // Scaffold files are always separate files, so always format them
309- formattedMiddleware , err := FormatCode (middlewareOut )
309+ formattedMiddleware , err := p . formatCode (middlewareOut )
310310 if err != nil {
311311 return nil , fmt .Errorf ("error formatting middleware: %w" , err )
312312 }
@@ -334,7 +334,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
334334 }
335335
336336 // Scaffold files are always separate files, so always format them
337- formatted , err := FormatCode (out )
337+ formatted , err := p . formatCode (out )
338338 if err != nil {
339339 return nil , fmt .Errorf ("error formatting service: %w" , err )
340340 }
@@ -376,7 +376,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
376376 }
377377 }
378378
379- formatted , err := FormatCode (out )
379+ formatted , err := p . formatCode (out )
380380 if err != nil {
381381 return nil , fmt .Errorf ("error formatting server: %w" , err )
382382 }
@@ -401,7 +401,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
401401 }
402402 formatted := out
403403 if ! useSingleFile {
404- formatted , err = FormatCode (out )
404+ formatted , err = p . formatCode (out )
405405 if err != nil {
406406 return nil , fmt .Errorf ("error formatting MCP tools: %w" , err )
407407 }
@@ -419,7 +419,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
419419 if err != nil {
420420 return nil , fmt .Errorf ("error generating code for validator: %w" , err )
421421 }
422- formatted , err := FormatCode (out )
422+ formatted , err := p . formatCode (out )
423423 if err != nil {
424424 return nil , err
425425 }
@@ -438,7 +438,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
438438 }
439439 formatted := out
440440 if ! useSingleFile {
441- formatted , err = FormatCode (out )
441+ formatted , err = p . formatCode (out )
442442 if err != nil {
443443 return nil , err
444444 }
@@ -484,7 +484,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
484484 }
485485 formatted := out
486486 if ! useSingleFile {
487- formatted , err = FormatCode (out )
487+ formatted , err = p . formatCode (out )
488488 if err != nil {
489489 return nil , err
490490 }
@@ -508,7 +508,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
508508 }
509509 formatted := out
510510 if ! useSingleFile {
511- formatted , err = FormatCode (out )
511+ formatted , err = p . formatCode (out )
512512 if err != nil {
513513 return nil , err
514514 }
@@ -540,7 +540,7 @@ func (p *Parser) Parse() (GeneratedCode, error) {
540540 res += code + "\n "
541541 }
542542
543- formatted , err := FormatCode (res )
543+ formatted , err := p . formatCode (res )
544544 if err != nil {
545545 println (res )
546546 return nil , err
@@ -575,6 +575,40 @@ func (p *Parser) ParseTemplates(templates []string, data any) (string, error) {
575575 return strings .Join (generatedTemplates , "\n " ), nil
576576}
577577
578+ // formatCode formats the provided Go code, respecting the skip-fmt config option.
579+ // When skip-fmt is true, only sanitization is performed (skips import optimization and gofmt).
580+ func (p * Parser ) formatCode (src string ) (string , error ) {
581+ src = strings .Trim (src , "\n " ) + "\n "
582+ if src == "\n " || src == "" {
583+ return src , nil
584+ }
585+
586+ // remove any byte-order-marks which break Go-Code
587+ // See: https://groups.google.com/forum/#!topic/golang-nuts/OToNIPdfkks
588+ src = strings .ReplaceAll (src , "\uFEFF " , "" )
589+
590+ if p .cfg .Output != nil && p .cfg .Output .SkipFmt {
591+ return src , nil
592+ }
593+
594+ return FormatCode (src )
595+ }
596+
597+ // FormatCode formats the provided Go code by optimizing imports and running gofmt.
598+ func FormatCode (src string ) (string , error ) {
599+ res , err := imports .Process ("gen.go" , []byte (src ), nil )
600+ if err != nil {
601+ return "" , fmt .Errorf ("error optimizing imports: %w" , err )
602+ }
603+
604+ res , err = format .Source (res )
605+ if err != nil {
606+ return "" , fmt .Errorf ("error formatting code: %w" , err )
607+ }
608+
609+ return string (res ), nil
610+ }
611+
578612func loadTemplates (cfg Configuration ) (* template.Template , error ) {
579613 tpl := template .New ("templates" ).Funcs (TemplateFunctions )
580614
@@ -627,43 +661,6 @@ func loadTemplates(cfg Configuration) (*template.Template, error) {
627661 return tpl , nil
628662}
629663
630- // FormatCode formats the provided Go code.
631- // It optimizes imports and formats the code using gofmt.
632- func FormatCode (src string ) (string , error ) {
633- src = strings .Trim (src , "\n " ) + "\n "
634- if src == "\n " || src == "" {
635- return src , nil
636- }
637-
638- res , err := optimizeImports ([]byte (src ))
639- if err != nil {
640- return "" , fmt .Errorf ("error optimizing imports: %w" , err )
641- }
642-
643- res , err = format .Source (res )
644- if err != nil {
645- return "" , fmt .Errorf ("error formatting code: %w" , err )
646- }
647-
648- return sanitizeCode (string (res )), nil
649- }
650-
651- // sanitizeCode runs sanitizers across the generated Go code to ensure the
652- // generated code will be able to compile.
653- func sanitizeCode (src string ) string {
654- // remove any byte-order-marks which break Go-Code
655- // See: https://groups.google.com/forum/#!topic/golang-nuts/OToNIPdfkks
656- return strings .ReplaceAll (src , "\uFEFF " , "" )
657- }
658-
659- func optimizeImports (src []byte ) ([]byte , error ) {
660- outBytes , err := imports .Process ("gen.go" , src , nil )
661- if err != nil {
662- return nil , err
663- }
664- return outBytes , nil
665- }
666-
667664func getSpecLocationOutName (specLocation SpecLocation ) string {
668665 switch specLocation {
669666 case SpecLocationPath :
0 commit comments