Skip to content

Commit

Permalink
fix: add newline between routes and execute func
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Dec 27, 2024
1 parent 189dcc3 commit 77b47f0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions internal/source/go.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package source

import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/printer"
"go/token"
"net/http"
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 77b47f0

Please sign in to comment.