Skip to content

Commit f8b32ea

Browse files
authored
fix: use error's marshaler, circular ref in Handler, lint issues (#28)
1 parent 8d7d992 commit f8b32ea

9 files changed

Lines changed: 66 additions & 53 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,4 @@ jobs:
6464
key: golangci-lint.cache-${{ hashFiles('**/go.sum') }}
6565
restore-keys: golangci-lint.cache-
6666

67-
- run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.44.2
68-
- run: golangci-lint run
69-
70-
# Not supported with Go 1.18
71-
# - uses: golangci/golangci-lint-action@v3
72-
# with:
73-
# version: v1.44
67+
- uses: golangci/golangci-lint-action@v3

.golangci.yml

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
11
linters:
22
enable-all: true
33
disable:
4+
- deadcode # deprecated
45
- exhaustive
56
- exhaustivestruct
7+
- exhaustruct
68
- forcetypeassert
79
- funlen
810
- gochecknoinits
911
- gochecknoglobals
10-
- gci
1112
- godox
1213
- golint # deprecated
1314
- gomnd
14-
- ifshort # false positives
15+
- ifshort # false positives - deprecated
1516
- interfacer # deprecated
1617
- ireturn
1718
- maligned # deprecated
19+
- nosnakecase # deprecated
1820
- nlreturn
1921
- paralleltest
2022
- scopelint # deprecated
23+
- structcheck # deprecated
2124
- testpackage
2225
- varnamelen
26+
- varcheck # deprecated
2327
- wrapcheck
24-
# no Go 1.18 support
25-
- bodyclose
26-
- contextcheck
27-
- gocritic
28-
- gosimple
29-
- govet
30-
- nilerr
31-
- noctx
32-
- rowserrcheck
33-
- sqlclosecheck
34-
- staticcheck
35-
- structcheck
36-
- stylecheck
37-
- tparallel
38-
- typecheck # false positives
39-
- unparam
40-
- unused
41-
- wastedassign
28+
- wsl
4229

4330
linters-settings:
44-
govet:
45-
enable-all: true
4631
depguard:
4732
list-type: denylist
4833
include-go-root: true
4934
packages:
5035
- encoding/json
36+
- github.com/pkg/errors
37+
gci:
38+
sections:
39+
- standard
40+
- default
41+
- prefix(github.com/optimusmine/optimusmine)
42+
gofumpt:
43+
extra-rules: true
44+
govet:
45+
enable-all: true
46+
nolintlint:
47+
allow-leading-space: false
48+
require-specific: true
5149

5250
issues:
51+
fix: true
5352
exclude-rules:
5453
- path: (.+)_test.go
5554
linters:
5655
- errcheck
5756
- funlen
58-
- ifshort
5957
- gosec
60-
- linters: [wsl]
61-
text: return statements should not be cuddled if block has more than two lines
62-
- linters: [wsl]
63-
text: only cuddled expressions if assigning variable or using from line above
58+
- path: (.+)_test.go
59+
linters: [govet]
60+
text: '^fieldalignment:'
61+
- linters: [govet]
62+
text: '^shadow: declaration of "err" shadows declaration'

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ You can create sub-routers using the `Group` function:
219219
```go
220220
r := don.New(nil)
221221
sub := r.Group("/api")
222-
sub.Get("/hello")
222+
sub.Get("/hello", don.H(Hello))
223223
```
224224

225225
## Middleware

api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type Router interface {
2727
}
2828

2929
type API struct {
30-
router *httprouter.Router
31-
config *Config
32-
mw []Middleware
33-
3430
NotFound fasthttp.RequestHandler
3531
MethodNotAllowed fasthttp.RequestHandler
3632
PanicHandler func(*fasthttp.RequestCtx, interface{})
33+
34+
router *httprouter.Router
35+
config *Config
36+
mw []Middleware
3737
}
3838

3939
type Config struct {
@@ -89,7 +89,7 @@ func (r *API) Handle(method, path string, handle httprouter.Handle) {
8989
}
9090

9191
func (r *API) Handler(method, path string, handle http.Handler) {
92-
r.Handler(method, path, handle)
92+
r.router.Handler(method, path, handle)
9393
}
9494

9595
func (r *API) HandleFunc(method, path string, handle http.HandlerFunc) {

decoder/compile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ var ErrUnsupportedType = errors.New("decoder: unsupported type")
1414

1515
type decoder func(reflect.Value, Getter) error
1616

17+
var unmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
18+
1719
//nolint:cyclop
1820
func compile(typ reflect.Type, tagKey string, isPtr bool) (decoder, error) {
1921
decoders := []decoder{}
2022

21-
unmarshalerType := reflect.TypeOf(new(encoding.TextUnmarshaler)).Elem()
22-
2323
for i := 0; i < typ.NumField(); i++ {
2424
f := typ.Field(i)
2525
if f.PkgPath != "" {

decoder/decode.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func NewDecoder(tag string) *Decoder {
1414
return &Decoder{tag: tag}
1515
}
1616

17-
func (d *Decoder) Decode(data Getter, v any) (err error) {
17+
func (d *Decoder) Decode(data Getter, v any) error {
1818
val := reflect.ValueOf(v).Elem()
1919
if val.Kind() != reflect.Struct {
2020
return ErrUnsupportedType
@@ -24,6 +24,7 @@ func (d *Decoder) Decode(data Getter, v any) (err error) {
2424

2525
dec, ok := d.cache.Load(t)
2626
if !ok {
27+
var err error
2728
dec, err = compile(t, d.tag, t.Kind() == reflect.Ptr)
2829
if err != nil {
2930
return err

errors.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding"
77
"encoding/xml"
8+
"errors"
89
"net/http"
910
"strconv"
1011

@@ -32,8 +33,8 @@ func (e *HTTPError) Error() string {
3233
}
3334

3435
func (e *HTTPError) StatusCode() int {
35-
//nolint:errorlint
36-
if sc, ok := e.err.(StatusCoder); ok {
36+
var sc StatusCoder
37+
if errors.As(e.err, &sc) {
3738
return sc.StatusCode()
3839
}
3940

@@ -45,25 +46,44 @@ func (e *HTTPError) StatusCode() int {
4546
}
4647

4748
func (e *HTTPError) MarshalText() ([]byte, error) {
49+
var m encoding.TextMarshaler
50+
if errors.As(e.err, &m) {
51+
return m.MarshalText()
52+
}
53+
4854
return []byte(e.Error()), nil
4955
}
5056

5157
func (e *HTTPError) MarshalJSON() ([]byte, error) {
52-
var buf bytes.Buffer
58+
var m json.Marshaler
59+
if errors.As(e.err, &m) {
60+
return m.MarshalJSON()
61+
}
5362

63+
var buf bytes.Buffer
5464
buf.WriteString(`{"message":`)
5565
buf.WriteString(strconv.Quote(e.Error()))
5666
buf.WriteRune('}')
5767

5868
return buf.Bytes(), nil
5969
}
6070

61-
func (e *HTTPError) MarshalXML(enc *xml.Encoder, _ xml.StartElement) error {
62-
start := xml.StartElement{Name: xml.Name{Local: "message"}}
71+
func (e *HTTPError) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
72+
var m xml.Marshaler
73+
if errors.As(e.err, &m) {
74+
return m.MarshalXML(enc, start)
75+
}
76+
77+
start = xml.StartElement{Name: xml.Name{Local: "message"}}
6378
return enc.EncodeElement(e.Error(), start)
6479
}
6580

6681
func (e *HTTPError) MarshalYAML() (interface{}, error) {
82+
var m yaml.Marshaler
83+
if errors.As(e.err, &m) {
84+
return m.MarshalYAML()
85+
}
86+
6787
return map[string]string{"message": e.Error()}, nil
6888
}
6989

handler.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@ type Headerer interface {
2222
}
2323

2424
// Handle is the type for your handlers.
25-
type Handle[T any, O any] func(ctx context.Context, request T) (O, error)
25+
type Handle[T, O any] func(ctx context.Context, request T) (O, error)
2626

2727
// H wraps your handler function with the Go generics magic.
28-
//nolint:gocognit,cyclop
29-
func H[T any, O any](handle Handle[T, O]) httprouter.Handle {
28+
func H[T, O any](handle Handle[T, O]) httprouter.Handle { //nolint:gocognit,cyclop
3029
var (
3130
decodeHeader *decoder.HeaderDecoder
3231
decodePath *decoder.ParamsDecoder
3332
decodeQuery *decoder.ArgsDecoder
34-
isNil = makeNilCheck(*new(O))
33+
isNil = makeNilCheck(*new(O)) //nolint:gocritic
3534
)
3635

3736
{

nilcheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func makeNilCheck(zero any) func(v any) bool {
1010
// Return true for nil interfaces.
11-
if zero == *new(any) {
11+
if zero == any(nil) {
1212
return func(v any) bool { return v == nil }
1313
}
1414

0 commit comments

Comments
 (0)