Skip to content

Commit

Permalink
chore: remove interface
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscouto committed Mar 13, 2024
1 parent ada8107 commit 8051f48
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 22 deletions.
17 changes: 3 additions & 14 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,11 @@ import (

const queryTag = "query"

type Decoder[T any] interface {
DecodeQueryParams(values url.Values) T
DecodeQueryParamsWithCustomTag(values url.Values, customTag string) T
func DecodeQueryParams[T any](values url.Values) T {
return DecodeQueryParamsWithCustomTag[T](values, queryTag)
}

type decoder[T any] struct{}

func New[T any]() Decoder[T] {
return &decoder[T]{}
}

func (d decoder[T]) DecodeQueryParams(values url.Values) T {
return d.DecodeQueryParamsWithCustomTag(values, queryTag)
}

func (d decoder[T]) DecodeQueryParamsWithCustomTag(values url.Values, customTag string) T {
func DecodeQueryParamsWithCustomTag[T any](values url.Values, customTag string) T {
var t T
targetValue := reflect.ValueOf(&t).Elem()
for i := 0; i < targetValue.NumField(); i++ {
Expand Down
10 changes: 2 additions & 8 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ type ModelQuery struct {
SomeDate time.Time `query:"some_date"`
}

type ModelQueryInvalid struct {
id string `query:"id"`
}

type DecodeSuiteTest struct {
suite.Suite
}
Expand All @@ -38,8 +34,7 @@ func (s *DecodeSuiteTest) TestDecodeQueryParams() {
values.Add("some_date", "2024-02-01T10:54:12Z")

s.T().Run("should decode query params", func(t *testing.T) {
model := New[ModelQuery]()
response := model.DecodeQueryParams(values)
response := DecodeQueryParams[ModelQuery](values)
assert.Equal(t, "new_id", response.ID)
assert.Equal(t, int32(1), response.SomeInt)
assert.Equal(t, float32(3.1314), response.SomeFloat)
Expand All @@ -57,8 +52,7 @@ func (s *DecodeSuiteTest) TestDecodeQueryParamsWithCustomTag() {
values.Add("some_date", "2024-02-01T10:54:12Z")

s.T().Run("should decode query params", func(t *testing.T) {
model := New[ModelQuery]()
response := model.DecodeQueryParamsWithCustomTag(values, "query")
response := DecodeQueryParamsWithCustomTag[ModelQuery](values, "query")
assert.Equal(t, "new_id", response.ID)
assert.Equal(t, int32(1), response.SomeInt)
assert.Equal(t, float32(3.1314), response.SomeFloat)
Expand Down

0 comments on commit 8051f48

Please sign in to comment.