1717package golang
1818
1919import (
20+ "fmt"
2021 "go/format"
2122 "os"
2223 "sort"
24+ "strings"
2325
2426 "github.com/go-spring/gs-http-gen/gen/generator"
2527 "github.com/go-spring/gs-http-gen/lib/tidl"
@@ -49,7 +51,7 @@ func (g *Generator) Gen(config *generator.Config, files map[string]tidl.Document
4951 // Collect all RPC definitions
5052 for fileName , doc := range files {
5153 if err := g .genType (ctx , fileName , doc ); err != nil {
52- return err
54+ return fmt . Errorf ( "generate type file %s error: %w" , fileName , err )
5355 }
5456 rpcs = append (rpcs , doc .RPCs ... )
5557 }
@@ -61,28 +63,48 @@ func (g *Generator) Gen(config *generator.Config, files map[string]tidl.Document
6163 // Generate server code if enabled in the configuration
6264 if config .EnableServer {
6365 if err := g .genValidate (ctx ); err != nil {
64- return err
66+ return fmt . Errorf ( "generate validate file error: %w" , err )
6567 }
6668 if err := g .genServer (ctx , rpcs ); err != nil {
67- return err
69+ return fmt . Errorf ( "generate server file error: %w" , err )
6870 }
6971 }
7072
7173 // Generate client code if enabled in the configuration
7274 if config .EnableClient {
7375 if err := g .genClient (ctx , rpcs ); err != nil {
74- return err
76+ return fmt . Errorf ( "generate client file error: %w" , err )
7577 }
7678 }
7779
7880 return nil
7981}
8082
81- // formatFile formats the given Go source code and writes it to a file.
83+ // formatFile formats Go source code using `go format`
84+ // and writes the formatted code to the given file.
8285func formatFile (fileName string , b []byte ) error {
8386 b , err := format .Source (b )
8487 if err != nil {
85- return err
88+ return fmt . Errorf ( "format source for file %s error: %w" , fileName , err )
8689 }
87- return os .WriteFile (fileName , b , os .ModePerm )
90+ err = os .WriteFile (fileName , b , os .ModePerm )
91+ if err != nil {
92+ return fmt .Errorf ("write file %s error: %w" , fileName , err )
93+ }
94+ return nil
95+ }
96+
97+ // formatComment converts a tidl.Comments into Go comments.
98+ func formatComment (c tidl.Comments ) string {
99+ var comment string
100+ for _ , s := range c .Above {
101+ comment += s .Text [0 ]
102+ }
103+ if c .Right != nil {
104+ if c .Above != nil {
105+ comment += "\n "
106+ }
107+ comment += strings .Join (c .Right .Text , "\n " )
108+ }
109+ return comment
88110}
0 commit comments