Skip to content

Commit 9c9b978

Browse files
committed
feat: support query:"<name>" tag
1 parent 1da7200 commit 9c9b978

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

cmd/happy/main.go

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"unicode/utf8"
1717

1818
"github.com/alecthomas/kong"
19+
"github.com/fatih/structtag"
1920
"golang.org/x/tools/go/packages"
2021

2122
"github.com/thnxdev/happy/codewriter"
@@ -351,7 +352,14 @@ func genQueryDecoderFunc(gctx *genContext, paramType types.Type) (name string, e
351352
w = w.Push()
352353
for i := 0; i < strct.NumFields(); i++ {
353354
field := strct.Field(i)
355+
tags, err := structtag.Parse(strct.Tag(i))
356+
if err != nil {
357+
return "", fmt.Errorf("invalid struct tag on %s.%s: %w", typeRef, field.Name(), err)
358+
}
354359
fieldName := lcFirst(field.Name())
360+
if tag, err := tags.Get("query"); err == nil {
361+
fieldName = tag.Name
362+
}
355363
w.L("if q, ok := p[%q]; ok {", fieldName)
356364
w = w.Push()
357365
fieldType := field.Type()

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.19
44

55
require (
66
github.com/alecthomas/kong v0.7.1
7+
github.com/fatih/structtag v1.2.0
78
golang.org/x/tools v0.3.0
89
)
910

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4
44
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
55
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
66
github.com/alecthomas/repr v0.1.0/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8=
7+
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
8+
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
79
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
810
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
911
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=

testdata/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ func (s *Service) CreateUser(r *http.Request, user User) error {
9090
}
9191

9292
type Paginate struct {
93-
Page int
94-
Size int
93+
Page int `query:"p"`
94+
Size int `query:"s"`
9595
Sparse *bool
9696
}
9797

testdata/main_api.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)