mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-27 19:41:11 +01:00
refactor: replace gperr with standard errors package and simplify string parsing
- Replace gperr.Error return types with standard error across test files - Replace gperr.New with errors.New in validation and serialization tests - Update API documentation in README files to use error instead of gperr.Error - Simplify string parsing using strings.Cut in docker/label.go - Update benchmarks to use NewTestEntrypoint and remove task package dependency
This commit is contained in:
@@ -2,11 +2,12 @@ package serialization_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/yusing/godoxy/internal/serialization"
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
type TestStruct struct {
|
||||
@@ -14,18 +15,17 @@ type TestStruct struct {
|
||||
Value2 int `json:"value2"`
|
||||
}
|
||||
|
||||
func (t *TestStruct) Validate() gperr.Error {
|
||||
func (t *TestStruct) Validate() error {
|
||||
if t.Value == "" {
|
||||
return gperr.New("value is required")
|
||||
return errors.New("value is required")
|
||||
}
|
||||
if t.Value2 != 0 && (t.Value2 < 5 || t.Value2 > 10) {
|
||||
return gperr.New("value2 must be between 5 and 10")
|
||||
return errors.New("value2 must be between 5 and 10")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestGinBinding(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
@@ -40,7 +40,7 @@ func TestGinBinding(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var dst TestStruct
|
||||
body := bytes.NewBufferString(tt.input)
|
||||
req := httptest.NewRequest("POST", "/", body)
|
||||
req := httptest.NewRequest(http.MethodPost, "/", body)
|
||||
err := serialization.GinJSONBinding{}.Bind(req, &dst)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("%s: Bind() error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
||||
|
||||
@@ -1,24 +1,23 @@
|
||||
package serialization
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
// Test cases for when *T implements CustomValidator but T is passed in
|
||||
type CustomValidatingInt int
|
||||
|
||||
func (c *CustomValidatingInt) Validate() gperr.Error {
|
||||
func (c *CustomValidatingInt) Validate() error {
|
||||
if c == nil {
|
||||
return gperr.New("pointer int cannot be nil")
|
||||
return errors.New("pointer int cannot be nil")
|
||||
}
|
||||
if *c <= 0 {
|
||||
return gperr.New("int must be positive")
|
||||
return errors.New("int must be positive")
|
||||
}
|
||||
if *c > 100 {
|
||||
return gperr.New("int must be <= 100")
|
||||
return errors.New("int must be <= 100")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -26,12 +25,12 @@ func (c *CustomValidatingInt) Validate() gperr.Error {
|
||||
// Test cases for when T implements CustomValidator but *T is passed in
|
||||
type CustomValidatingFloat float64
|
||||
|
||||
func (c CustomValidatingFloat) Validate() gperr.Error {
|
||||
func (c CustomValidatingFloat) Validate() error {
|
||||
if c < 0 {
|
||||
return gperr.New("float must be non-negative")
|
||||
return errors.New("float must be non-negative")
|
||||
}
|
||||
if c > 1000 {
|
||||
return gperr.New("float must be <= 1000")
|
||||
return errors.New("float must be <= 1000")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,23 +1,22 @@
|
||||
package serialization
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
type CustomValidatingPointerString string
|
||||
|
||||
func (c *CustomValidatingPointerString) Validate() gperr.Error {
|
||||
func (c *CustomValidatingPointerString) Validate() error {
|
||||
if c == nil {
|
||||
return gperr.New("pointer string cannot be nil")
|
||||
return errors.New("pointer string cannot be nil")
|
||||
}
|
||||
if *c == "" {
|
||||
return gperr.New("string cannot be empty")
|
||||
return errors.New("string cannot be empty")
|
||||
}
|
||||
if len(*c) < 2 {
|
||||
return gperr.New("string must be at least 2 characters")
|
||||
return errors.New("string must be at least 2 characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
package serialization
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
type CustomValidatingString string
|
||||
|
||||
func (c CustomValidatingString) Validate() gperr.Error {
|
||||
func (c CustomValidatingString) Validate() error {
|
||||
if c == "" {
|
||||
return gperr.New("string cannot be empty")
|
||||
return errors.New("string cannot be empty")
|
||||
}
|
||||
if len(c) < 2 {
|
||||
return gperr.New("string must be at least 2 characters")
|
||||
return errors.New("string must be at least 2 characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,25 +1,24 @@
|
||||
package serialization
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
type CustomValidatingPointerStruct struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func (c *CustomValidatingPointerStruct) Validate() gperr.Error {
|
||||
func (c *CustomValidatingPointerStruct) Validate() error {
|
||||
if c == nil {
|
||||
return gperr.New("pointer struct cannot be nil")
|
||||
return errors.New("pointer struct cannot be nil")
|
||||
}
|
||||
if c.Value == "" {
|
||||
return gperr.New("value cannot be empty")
|
||||
return errors.New("value cannot be empty")
|
||||
}
|
||||
if len(c.Value) < 3 {
|
||||
return gperr.New("value must be at least 3 characters")
|
||||
return errors.New("value must be at least 3 characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
package serialization
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
gperr "github.com/yusing/goutils/errs"
|
||||
)
|
||||
|
||||
type CustomValidatingStruct struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func (c CustomValidatingStruct) Validate() gperr.Error {
|
||||
func (c CustomValidatingStruct) Validate() error {
|
||||
if c.Value == "" {
|
||||
return gperr.New("value cannot be empty")
|
||||
return errors.New("value cannot be empty")
|
||||
}
|
||||
if len(c.Value) < 3 {
|
||||
return gperr.New("value must be at least 3 characters")
|
||||
return errors.New("value must be at least 3 characters")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user