From 77b47f0fc889980c39b5a7676794987fcbfb94a4 Mon Sep 17 00:00:00 2001 From: Chrstopher Hunter <8398225+crhntr@users.noreply.github.com> Date: Thu, 26 Dec 2024 23:06:40 -0800 Subject: [PATCH] fix: add newline between routes and execute func --- internal/source/go.go | 10 ++++++++-- routes_test.go | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/internal/source/go.go b/internal/source/go.go index fd053ce..1de403c 100644 --- a/internal/source/go.go +++ b/internal/source/go.go @@ -1,8 +1,10 @@ package source import ( + "bytes" "fmt" "go/ast" + "go/format" "go/printer" "go/token" "net/http" @@ -39,11 +41,15 @@ func IterateValueSpecs(files []*ast.File) func(func(*ast.File, *ast.ValueSpec) b } func Format(node ast.Node) string { - var buf strings.Builder + var buf bytes.Buffer if err := printer.Fprint(&buf, token.NewFileSet(), node); err != nil { return fmt.Sprintf("formatting error: %v", err) } - return buf.String() + out, err := format.Source(buf.Bytes()) + if err != nil { + return fmt.Sprintf("formatting error: %v", err) + } + return string(bytes.ReplaceAll(out, []byte("\n}\nfunc "), []byte("\n}\n\nfunc "))) } func evaluateStringLiteralExpressionList(wd string, set *token.FileSet, list []ast.Expr) ([]string, error) { diff --git a/routes_test.go b/routes_test.go index 1671aa2..8291f06 100644 --- a/routes_test.go +++ b/routes_test.go @@ -48,6 +48,7 @@ func routes(mux *http.ServeMux, receiver RoutesReceiver) { execute(response, request, true, "GET /", http.StatusOK, request) }) } + func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) { buf := bytes.NewBuffer(nil) if err := templates.ExecuteTemplate(buf, name, data); err != nil { @@ -82,6 +83,7 @@ func routes(mux *http.ServeMux, receiver RoutesReceiver) { execute(response, request, true, "GET / F()", http.StatusOK, data) }) } + func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) { buf := bytes.NewBuffer(nil) if err := templates.ExecuteTemplate(buf, name, data); err != nil { @@ -120,6 +122,7 @@ func routes(mux *http.ServeMux, receiver RoutesReceiver) { execute(response, request, false, "GET /project/{projectID}/task/{taskID} F(ctx, response, request, projectID, taskID)", http.StatusOK, data) }) } + func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) { buf := bytes.NewBuffer(nil) if err := templates.ExecuteTemplate(buf, name, data); err != nil { @@ -164,6 +167,7 @@ func routes(mux *http.ServeMux, receiver RoutesReceiver) { execute(response, request, true, "GET /age/{username} F(username)", http.StatusOK, data) }) } + func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) { buf := bytes.NewBuffer(nil) if err := templates.ExecuteTemplate(buf, name, data); err != nil { @@ -199,6 +203,7 @@ func routes(mux *http.ServeMux, receiver Server) { execute(response, request, true, "GET / F()", http.StatusOK, data) }) } + func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) { buf := bytes.NewBuffer(nil) if err := templates.ExecuteTemplate(buf, name, data); err != nil { @@ -246,6 +251,7 @@ func routes(mux *http.ServeMux, receiver RoutesReceiver) { execute(response, request, true, "GET /age/{username} F(username)", http.StatusOK, data) }) } + func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) { buf := bytes.NewBuffer(nil) if err := templates.ExecuteTemplate(buf, name, data); err != nil { @@ -368,6 +374,7 @@ import( ) func routes(mux *http.ServeMux, receiver RoutesReceiver) {} + func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) { response.WriteHeader(code) _ = templates.ExecuteTemplate(response, name, data) @@ -385,6 +392,7 @@ type RoutesReceiver interface { func routes(mux *http.ServeMux, receiver RoutesReceiver) { } + func execute(response http.ResponseWriter, request *http.Request, writeHeader bool, name string, code int, data any) { buf := bytes.NewBuffer(nil) if err := templates.ExecuteTemplate(buf, name, data); err != nil {