1
0
mirror of https://github.com/ggicci/httpin.git synced 2024-11-24 08:32:45 +02:00

* use directive

This commit is contained in:
Alec Sammon 2024-05-18 10:06:36 +01:00
parent ac0357ca6c
commit c7707e1303
No known key found for this signature in database
6 changed files with 29 additions and 8 deletions

View File

@ -56,7 +56,7 @@ type ListUsersInput struct {
Page int `in:"query=page;default=1"`
PerPage int `in:"query=per_page;default=20"`
IsMember bool `in:"query=is_member"`
Search *string `in:"query=search,omitempty"`
Search *string `in:"query=search;omitempty"`
}
func ListUsers(rw http.ResponseWriter, r *http.Request) {

View File

@ -17,6 +17,7 @@ func init() {
RegisterDirective("default", &DirectiveDefault{})
RegisterDirective("nonzero", &DirectiveNonzero{})
registerDirective("path", defaultPathDirective)
registerDirective("omitempty", &DirectiveOmitEmpty{})
// decoder is a special executor which does nothing, but is an indicator of
// overriding the decoder for a specific field.

View File

@ -2,7 +2,6 @@ package core
import (
"github.com/ggicci/httpin/internal"
"slices"
)
type FormEncoder struct {
@ -10,8 +9,12 @@ type FormEncoder struct {
}
func (e *FormEncoder) Execute(rtm *DirectiveRuntime) error {
if rtm.Value.IsZero() && slices.Contains(rtm.Directive.Argv, "omitempty") {
return nil
if rtm.Value.IsZero() {
for _, d := range rtm.Resolver.Directives {
if d.Name == "omitempty" {
return nil
}
}
}
if rtm.IsFieldSet() {

View File

@ -29,8 +29,8 @@ func TestDirectiveHeader_Decode(t *testing.T) {
func TestDirectiveHeader_NewRequest(t *testing.T) {
type ApiQuery struct {
ApiUid int `in:"header=x-api-uid,omitempty"`
ApiToken *string `in:"header=X-Api-Token,omitempty"`
ApiUid int `in:"header=x-api-uid;omitempty"`
ApiToken *string `in:"header=X-Api-Token;omitempty"`
}
t.Run("with all values", func(t *testing.T) {

17
core/omitempty.go Normal file
View File

@ -0,0 +1,17 @@
// directive: "omitempty"
// https://ggicci.github.io/httpin/directives/omitempty
package core
// DirectiveOmitEmpty is used with the DirectiveQuery, DirectiveForm, and DirectiveHeader to indicate that the field
// should be omitted when the value is empty.
// It does not have any affect when used by itself
type DirectiveOmitEmpty struct{}
func (*DirectiveOmitEmpty) Decode(_ *DirectiveRuntime) error {
return nil
}
func (*DirectiveOmitEmpty) Encode(_ *DirectiveRuntime) error {
return nil
}

View File

@ -33,7 +33,7 @@ func TestDirectiveQuery_Decode(t *testing.T) {
func TestDirectiveQuery_NewRequest(t *testing.T) {
type SearchQuery struct {
Name string `in:"query=name"`
Age int `in:"query=age,omitempty"`
Age int `in:"query=age;omitempty"`
Enabled bool `in:"query=enabled"`
Price float64 `in:"query=price"`
@ -41,7 +41,7 @@ func TestDirectiveQuery_NewRequest(t *testing.T) {
AgeList []int `in:"query=age_list[]"`
NamePointer *string `in:"query=name_pointer"`
AgePointer *int `in:"query=age_pointer,omitempty"`
AgePointer *int `in:"query=age_pointer;omitempty"`
}
t.Run("with all values", func(t *testing.T) {