Skip to content

Refactor/ Make module become zero-dependencies #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 15, 2025
6 changes: 6 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
coverage:
round: down
precision: 2
status:
project:
default:
target: 95%
threshold: 0.1%
patch:
default:
target: 90%
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![PkgGoDev](https://pkg.go.dev/badge/github.com/sonh/qs)](https://pkg.go.dev/github.com/sonh/qs)
[![MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/sonh/qs/blob/main/LICENSE)

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

## Installation
```bash
Expand Down
13 changes: 6 additions & 7 deletions encode.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package qs

import (
"github.com/pkg/errors"
"net/url"
"reflect"
"strings"
Expand Down Expand Up @@ -80,14 +79,14 @@ func (e *Encoder) Values(v interface{}) (url.Values, error) {
val := reflect.ValueOf(v)
for val.Kind() == reflect.Ptr {
if val.IsNil() {
return nil, errors.Errorf("expects struct input, got %v", val.Kind())
return nil, InvalidInputErr{InputKind: val.Kind()}
}
val = val.Elem()
}

switch val.Kind() {
case reflect.Invalid:
return nil, errors.Errorf("expects struct input, got %v", val.Kind())
return nil, InvalidInputErr{InputKind: val.Kind()}
case reflect.Struct:
enc := e.dataPool.Get().(*encoder)
enc.values = make(url.Values)
Expand All @@ -99,7 +98,7 @@ func (e *Encoder) Values(v interface{}) (url.Values, error) {
e.dataPool.Put(enc)
return values, nil
default:
return nil, errors.Errorf("expects struct input, got %v", val.Kind())
return nil, InvalidInputErr{InputKind: val.Kind()}
}
}

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

switch val.Kind() {
case reflect.Invalid:
return errors.Errorf("expects struct input, got %v", val.Kind())
return InvalidInputErr{InputKind: val.Kind()}
case reflect.Struct:
enc := e.dataPool.Get().(*encoder)
err := enc.encodeStruct(val, values, nil)
Expand All @@ -125,7 +124,7 @@ func (e *Encoder) Encode(v interface{}, values url.Values) error {
}
return nil
default:
return errors.Errorf("expects struct input, got %v", val.Kind())
return InvalidInputErr{InputKind: val.Kind()}
}
}

Expand Down
56 changes: 41 additions & 15 deletions encode_cache_test.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,73 @@
package qs

import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)

func TestCacheStore(t *testing.T) {
test := assert.New(t)
t.Parallel()

s := &basicVal{}

cacheStore := newCacheStore()
test.NotNil(cacheStore)
if cacheStore == nil {
t.Error("cache store should not be nil")
t.FailNow()
}

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

test.NotNil(cachedFlds)
test.Len(cachedFlds, len(fields))
test.True(&fields[0] == &cachedFlds[0])
if cachedFlds == nil {
t.Error("cache store should not be nil")
t.FailNow()
}
if len(cachedFlds) != len(fields) {
t.Error("cache store should have the same number of fields")
t.FailNow()
}
if &fields[0] != &cachedFlds[0] {
t.Error("cache store should have the same fields")
t.FailNow()
}
}

func TestNewCacheField(t *testing.T) {
test := assert.New(t)
t.Parallel()

name := []byte(`abc`)
opts := [][]byte{[]byte(`omitempty`)}

cacheField := newCachedFieldByKind(reflect.ValueOf("").Kind(), name, opts)
if stringField, ok := cacheField.(*stringField); ok {
test.Equal(string(name), stringField.name)
test.True(stringField.omitEmpty)
} else {
test.FailNow("")

strField, ok := cacheField.(*stringField)
if !ok {
t.Error("strField should be stringField")
t.FailNow()
}
if string(name) != strField.name {
t.Errorf("strField.name should be %s, but %s", string(name), strField.name)
t.FailNow()
}
if !strField.omitEmpty {
t.Error("omitEmpty should be true")
t.FailNow()
}
if !reflect.DeepEqual(reflect.TypeOf(new(stringField)), reflect.TypeOf(cacheField)) {
t.Error("cache field is not of type *stringField")
t.FailNow()
}
test.IsType(&stringField{}, cacheField)
}

func TestNewCacheField2(t *testing.T) {
test := assert.New(t)
t.Parallel()

var strPtr *string
cacheField := newCachedFieldByKind(reflect.ValueOf(strPtr).Kind(), nil, nil)
test.Nil(cacheField)
if cacheField != nil {
t.Error("expect cacheField to be nil")
t.FailNow()
}
}
Loading
Loading