Skip to content

Commit

Permalink
👔 up: update some bind and render logic
Browse files Browse the repository at this point in the history
- upgrade gookit/goutil to v0.6.11
  • Loading branch information
inhere committed Jul 13, 2023
1 parent f0e7bee commit 5797efb
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 40 deletions.
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/gookit/color v1.5.3
github.com/gookit/goutil v0.6.10
github.com/gookit/goutil v0.6.11
github.com/gookit/validate v1.4.6
github.com/gorilla/websocket v1.5.0
github.com/monoculum/formam v3.5.5+incompatible
Expand All @@ -13,6 +13,7 @@ require (
require (
github.com/gookit/filter v1.1.4 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
)
15 changes: 8 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/gookit/filter v1.1.4 h1:SXd6PEumiP/0jtF2crQRaz1wmKwHbW9xg5Ds6/ZP16w=
github.com/gookit/filter v1.1.4/go.mod h1:0CEPQvudso375RitQf9X8HerUg9cz8N7c/yn6b1RMzM=
github.com/gookit/goutil v0.5.12/go.mod h1:6vhWm/bSYXGE8poqFbFz6IGM7jV2r6qVhyK567SX/AI=
github.com/gookit/goutil v0.5.15/go.mod h1:ozPE16eJS9f89aVbVk05ocEJsia3KPrYUqPTs8GvUTw=
github.com/gookit/goutil v0.6.10 h1:iq7CXOf+fYLvrVAh3+ZoLgufGfK65TwbzE8NpnPGtyk=
github.com/gookit/goutil v0.6.10/go.mod h1:qqrPoX+Pm6YmxqqccgkNLPirTFX7UYMES1SK+fokqQU=
github.com/gookit/goutil v0.6.11 h1:615nIGRpQHFmgJ1oaA48q/z7bTx6KzMvHmKTsp21T2E=
github.com/gookit/goutil v0.6.11/go.mod h1:bU9ghaM9uW23x2+jB0WcywRsFGbIP0hvdIKYl2OMiog=
github.com/gookit/validate v1.4.6 h1:Ix8NRy2+6z4YGHWXgZL9+emy9wRI2GWyhW2smPcIlSU=
github.com/gookit/validate v1.4.6/go.mod h1:1rjeYaYlMK/8od4oge5C+Gt/3DnHkXymLPda7+3urC8=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
Expand Down Expand Up @@ -40,6 +40,7 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -49,19 +50,19 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.9.0 h1:GRRCnKYhdQrD8kfRAdQ6Zcw1P0OcELxGLKJvtjVMZ28=
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
2 changes: 1 addition & 1 deletion pkg/binding/binder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/rux/binding"
"github.com/gookit/rux/pkg/binding"
)

func TestBinder_Name(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/binding/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gookit/goutil/netutil/httpctype"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/rux/binding"
"github.com/gookit/rux/pkg/binding"
)

var (
Expand All @@ -31,7 +31,7 @@ func TestAuto(t *testing.T) {
is := assert.New(t)
r := http.NewServeMux()

r.HandleFunc("/AutoBind", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.HandleFunc("/AutoBind", func(w http.ResponseWriter, r *http.Request) {
u := &User{}
if ctype := r.Header.Get(httpctype.Key); ctype != "" {
fmt.Printf(" - auto bind data by content type: %s\n", ctype)
Expand All @@ -41,7 +41,7 @@ func TestAuto(t *testing.T) {

err := binding.Bind(r, u)
testBoundedUserIsOK(is, err, u)
}))
})

// post Form body
w := testutil.MockRequest(r, http.MethodPost, "/AutoBind", &testutil.MD{
Expand Down
6 changes: 5 additions & 1 deletion pkg/binding/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ type stdValidator struct{}
// Validate the struct data, if fail return error
func (sv *stdValidator) Validate(obj any) error {
v := validate.New(obj)
return v.ValidateE()

if v.Validate() {
return nil
}
return v.Errors.OneError()
}

// DisableValidator for data binding
Expand Down
8 changes: 6 additions & 2 deletions pkg/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"

"github.com/gookit/goutil"
"github.com/gookit/goutil/testutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/rux"
Expand All @@ -14,11 +15,14 @@ func ExampleHTTPMethodOverrideHandler() {
r := rux.New()

h := HTTPMethodOverrideHandler(r)
http.ListenAndServe(":8080", h)
err := http.ListenAndServe(":8080", h)
if err != nil {
panic(err)
}

// can also:
h1 := r.WrapHTTPHandlers(HTTPMethodOverrideHandler)
http.ListenAndServe(":8080", h1)
goutil.PanicErr(http.ListenAndServe(":8080", h1))
}

func TestDumpRoutesHandler(t *testing.T) {
Expand Down
31 changes: 8 additions & 23 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/gookit/goutil/netutil/httpctype"
"github.com/gookit/goutil/netutil/httpreq"
)

// PrettyIndent indent string for render JSON or XML
var PrettyIndent = " "
var (
// PrettyIndent indent string for render JSON or XML
PrettyIndent = " "

// FallbackType for auto response
var FallbackType = httpctype.MIMEText
// FallbackType for auto response
FallbackType = httpctype.MIMEText
)

// Renderer interface
type Renderer interface {
Expand Down Expand Up @@ -65,7 +67,7 @@ func Blob(w http.ResponseWriter, contentType string, data []byte) (err error) {

// Auto render data to response
func Auto(w http.ResponseWriter, r *http.Request, obj any) (err error) {
accepts := parseAccept(r.Header.Get("Accept"))
accepts := httpreq.ParseAccept(r.Header.Get("Accept"))

// fallback use FallbackType
if len(accepts) == 0 {
Expand Down Expand Up @@ -123,23 +125,6 @@ func responseText(w http.ResponseWriter, obj any) error {
}
}

// from gin framework
func parseAccept(acceptHeader string) []string {
if acceptHeader == "" {
return []string{}
}

parts := strings.Split(acceptHeader, ",")
outs := make([]string, 0, len(parts))

for _, part := range parts {
if part = strings.TrimSpace(strings.Split(part, ";")[0]); part != "" {
outs = append(outs, part)
}
}
return outs
}

func writeContentType(w http.ResponseWriter, value string) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
Expand Down
File renamed without changes.

0 comments on commit 5797efb

Please sign in to comment.