Skip to content

Commit 1f716f9

Browse files
authoredMar 15, 2025··
Refactor/ Make module become zero-dependencies (#7)
- Remove these dependencies: - `github.com/pkg/errors` - `github.com/stretchr/testify` - Update unit tests and err messages the the module returns - Update codecov config
1 parent ef685fc commit 1f716f9

8 files changed

+419
-166
lines changed
 

‎.codecov.yml

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
coverage:
2+
round: down
3+
precision: 2
24
status:
5+
project:
6+
default:
7+
target: 95%
8+
threshold: 0.1%
39
patch:
410
default:
511
target: 90%

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![PkgGoDev](https://pkg.go.dev/badge/github.com/sonh/qs)](https://pkg.go.dev/github.com/sonh/qs)
77
[![MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/sonh/qs/blob/main/LICENSE)
88

9-
Package sonh/qs encodes structs into url.Values.
9+
Zero-dependencies package to encodes structs into url.Values.
1010

1111
## Installation
1212
```bash

‎encode.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package qs
22

33
import (
4-
"github.com/pkg/errors"
54
"net/url"
65
"reflect"
76
"strings"
@@ -80,14 +79,14 @@ func (e *Encoder) Values(v interface{}) (url.Values, error) {
8079
val := reflect.ValueOf(v)
8180
for val.Kind() == reflect.Ptr {
8281
if val.IsNil() {
83-
return nil, errors.Errorf("expects struct input, got %v", val.Kind())
82+
return nil, InvalidInputErr{InputKind: val.Kind()}
8483
}
8584
val = val.Elem()
8685
}
8786

8887
switch val.Kind() {
8988
case reflect.Invalid:
90-
return nil, errors.Errorf("expects struct input, got %v", val.Kind())
89+
return nil, InvalidInputErr{InputKind: val.Kind()}
9190
case reflect.Struct:
9291
enc := e.dataPool.Get().(*encoder)
9392
enc.values = make(url.Values)
@@ -99,7 +98,7 @@ func (e *Encoder) Values(v interface{}) (url.Values, error) {
9998
e.dataPool.Put(enc)
10099
return values, nil
101100
default:
102-
return nil, errors.Errorf("expects struct input, got %v", val.Kind())
101+
return nil, InvalidInputErr{InputKind: val.Kind()}
103102
}
104103
}
105104

@@ -109,14 +108,14 @@ func (e *Encoder) Encode(v interface{}, values url.Values) error {
109108
val := reflect.ValueOf(v)
110109
for val.Kind() == reflect.Ptr {
111110
if val.IsNil() {
112-
return errors.Errorf("expects struct input, got %v", val.Kind())
111+
return InvalidInputErr{InputKind: val.Kind()}
113112
}
114113
val = val.Elem()
115114
}
116115

117116
switch val.Kind() {
118117
case reflect.Invalid:
119-
return errors.Errorf("expects struct input, got %v", val.Kind())
118+
return InvalidInputErr{InputKind: val.Kind()}
120119
case reflect.Struct:
121120
enc := e.dataPool.Get().(*encoder)
122121
err := enc.encodeStruct(val, values, nil)
@@ -125,7 +124,7 @@ func (e *Encoder) Encode(v interface{}, values url.Values) error {
125124
}
126125
return nil
127126
default:
128-
return errors.Errorf("expects struct input, got %v", val.Kind())
127+
return InvalidInputErr{InputKind: val.Kind()}
129128
}
130129
}
131130

‎encode_cache_test.go

+41-15
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,73 @@
11
package qs
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"reflect"
65
"testing"
76
)
87

98
func TestCacheStore(t *testing.T) {
10-
test := assert.New(t)
9+
t.Parallel()
1110

1211
s := &basicVal{}
1312

1413
cacheStore := newCacheStore()
15-
test.NotNil(cacheStore)
14+
if cacheStore == nil {
15+
t.Error("cache store should not be nil")
16+
t.FailNow()
17+
}
1618

1719
fields := cachedFields{&float64Field{}}
1820
cacheStore.Store(reflect.TypeOf(s), fields)
1921
cachedFlds := cacheStore.Retrieve(reflect.TypeOf(s))
2022

21-
test.NotNil(cachedFlds)
22-
test.Len(cachedFlds, len(fields))
23-
test.True(&fields[0] == &cachedFlds[0])
23+
if cachedFlds == nil {
24+
t.Error("cache store should not be nil")
25+
t.FailNow()
26+
}
27+
if len(cachedFlds) != len(fields) {
28+
t.Error("cache store should have the same number of fields")
29+
t.FailNow()
30+
}
31+
if &fields[0] != &cachedFlds[0] {
32+
t.Error("cache store should have the same fields")
33+
t.FailNow()
34+
}
2435
}
2536

2637
func TestNewCacheField(t *testing.T) {
27-
test := assert.New(t)
38+
t.Parallel()
39+
2840
name := []byte(`abc`)
2941
opts := [][]byte{[]byte(`omitempty`)}
3042

3143
cacheField := newCachedFieldByKind(reflect.ValueOf("").Kind(), name, opts)
32-
if stringField, ok := cacheField.(*stringField); ok {
33-
test.Equal(string(name), stringField.name)
34-
test.True(stringField.omitEmpty)
35-
} else {
36-
test.FailNow("")
44+
45+
strField, ok := cacheField.(*stringField)
46+
if !ok {
47+
t.Error("strField should be stringField")
48+
t.FailNow()
49+
}
50+
if string(name) != strField.name {
51+
t.Errorf("strField.name should be %s, but %s", string(name), strField.name)
52+
t.FailNow()
53+
}
54+
if !strField.omitEmpty {
55+
t.Error("omitEmpty should be true")
56+
t.FailNow()
57+
}
58+
if !reflect.DeepEqual(reflect.TypeOf(new(stringField)), reflect.TypeOf(cacheField)) {
59+
t.Error("cache field is not of type *stringField")
60+
t.FailNow()
3761
}
38-
test.IsType(&stringField{}, cacheField)
3962
}
4063

4164
func TestNewCacheField2(t *testing.T) {
42-
test := assert.New(t)
65+
t.Parallel()
4366

4467
var strPtr *string
4568
cacheField := newCachedFieldByKind(reflect.ValueOf(strPtr).Kind(), nil, nil)
46-
test.Nil(cacheField)
69+
if cacheField != nil {
70+
t.Error("expect cacheField to be nil")
71+
t.FailNow()
72+
}
4773
}

0 commit comments

Comments
 (0)
Please sign in to comment.