Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request body form parsing #13

Merged
merged 26 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f2079bc
remove unused variable
crhntr Aug 24, 2024
9672cc5
add template param to Generate
crhntr Aug 26, 2024
7547b18
refactor: rename pattern variable to name
crhntr Aug 26, 2024
12519f9
rename ast -> go
crhntr Aug 26, 2024
204380d
refactor: add helper function for generating status code expressions
crhntr Aug 27, 2024
2583799
remove unused template name scope
crhntr Aug 27, 2024
e78bdc9
refactor: factor our errVar
crhntr Aug 27, 2024
aead362
refactor: move err checkout of assignment func
crhntr Aug 27, 2024
63695a5
refactor: pass in str source
crhntr Aug 27, 2024
63f079e
refactor: use Atoi for int and factor out lit to source pkg
crhntr Aug 27, 2024
1af4420
refactor: move token out of assign func
crhntr Aug 27, 2024
e9368d4
refactor: move parse statement generator to new func
crhntr Aug 27, 2024
3861359
refactor: make parsing strings more general
crhntr Aug 27, 2024
be77fa1
add form declaration to func literal
crhntr Aug 28, 2024
141f401
pass add form arg from request.Form
crhntr Aug 29, 2024
0489c1d
add field parsing from tag
crhntr Aug 29, 2024
d003913
refactor: add helper function to get field name
crhntr Aug 29, 2024
8ead025
add test for tag parsing
crhntr Aug 29, 2024
1802f2b
parse forms fields with non-string basic types
crhntr Aug 29, 2024
3b7a490
add error message for unsupported type
crhntr Aug 29, 2024
87c0586
consolidate happy path tests into higher level
crhntr Aug 29, 2024
2015f94
test: consolidate error tests
crhntr Aug 29, 2024
660b89f
refactor: method nil checks
crhntr Aug 29, 2024
db2e1f6
test: refactor constants to use use executeGo
crhntr Aug 29, 2024
8fa6a0c
feat: support field lists
crhntr Aug 29, 2024
14636ad
add feature level test
crhntr Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/muxt/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func generateCommand(args []string, workingDirectory string, getEnv func(string)
return err
}
out := log.New(stdout, "", 0)
s, err := muxt.Generate(templateNames, g.goPackage, g.templatesVariable, g.routesFunction, g.receiverIdent, g.Package.Fset, g.Package.Syntax, g.Package.Syntax, out)
s, err := muxt.Generate(templateNames, ts, g.goPackage, g.templatesVariable, g.routesFunction, g.receiverIdent, g.Package.Fset, g.Package.Syntax, g.Package.Syntax, out)
if err != nil {
return err
}
Expand Down
93 changes: 93 additions & 0 deletions cmd/muxt/testdata/generate/form.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
muxt generate --receiver-static-type=T

cat template_routes.go

exec go test -cover

-- template.gohtml --
{{define "POST / Method(form)" }}<script>var _ = {{.}}</script>{{end}}

-- go.mod --
module server

go 1.22

-- template.go --
package server

import (
"embed"
"html/template"
)

//go:embed *.gohtml
var formHTML embed.FS

var templates = template.Must(template.ParseFS(formHTML, "*"))

type Form struct {
Count []int `json:"count"`
Str string `input:"some-string" json:"str"`
}

type T struct {
spy func(Form) Form
}

func (t T) Method(form Form) Form {
return t.spy(form)
}
-- template_test.go --
package server

import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"slices"
"strings"
"testing"
)

func Test(t *testing.T) {
mux := http.NewServeMux()

var service T

service.spy = func(form Form) Form {
if exp := []int{7, 14, 21, 29}; !slices.Equal(exp, form.Count) {
t.Errorf("exp %v, got %v", exp, form.Count)
}
if exp := "apple"; form.Str != exp {
t.Errorf("exp %v, got %v", exp, form.Str)
}
return form
}

routes(mux, service)

req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(url.Values{
"some-string": []string{"apple"},
"Count": []string{"7", "14", "21", "29"},
}.Encode()))
req.Header.Set("content-type", "application/x-www-form-urlencoded")
rec := httptest.NewRecorder()

mux.ServeHTTP(rec, req)

res := rec.Result()

if res.StatusCode != http.StatusOK {
t.Error("expected OK")
}

body, err := io.ReadAll(res.Body)
if err != nil {
t.Error(err)
}

if exp := `<script>var _ = {"count":[7,14,21,29],"str":"apple"}</script>`; string(body) != exp {
t.Errorf("exp %v, got %v", exp, string(body))
}
}
Loading
Loading