Skip to content

Commit 060d18a

Browse files
authored
refactor: simplify decoder, add tests (#89)
1 parent f0c21df commit 060d18a

17 files changed

Lines changed: 341 additions & 571 deletions

decoder/adapters.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package decoder
2+
3+
import (
4+
"github.com/abemedia/go-don/internal/byteconv"
5+
"github.com/abemedia/httprouter"
6+
"github.com/valyala/fasthttp"
7+
)
8+
9+
type Map map[string][]string
10+
11+
func (m Map) Get(key string) string {
12+
if m == nil {
13+
return ""
14+
}
15+
if vs := m[key]; len(vs) > 0 {
16+
return vs[0]
17+
}
18+
return ""
19+
}
20+
21+
func (m Map) Values(key string) []string {
22+
if m == nil {
23+
return nil
24+
}
25+
return m[key]
26+
}
27+
28+
type Args fasthttp.Args
29+
30+
func (ps *Args) Get(key string) string {
31+
return byteconv.Btoa((*fasthttp.Args)(ps).Peek(key))
32+
}
33+
34+
func (ps *Args) Values(key string) []string {
35+
args := (*fasthttp.Args)(ps).PeekMulti(key)
36+
if len(args) == 0 {
37+
return nil
38+
}
39+
40+
res := make([]string, len(args))
41+
for i := range args {
42+
res[i] = byteconv.Btoa(args[i])
43+
}
44+
45+
return res
46+
}
47+
48+
type Header fasthttp.RequestHeader
49+
50+
func (ps *Header) Get(key string) string {
51+
return byteconv.Btoa((*fasthttp.RequestHeader)(ps).Peek(key))
52+
}
53+
54+
func (ps *Header) Values(key string) []string {
55+
args := (*fasthttp.RequestHeader)(ps).PeekAll(key)
56+
if len(args) == 0 {
57+
return nil
58+
}
59+
60+
res := make([]string, len(args))
61+
for i := range args {
62+
res[i] = byteconv.Btoa(args[i])
63+
}
64+
65+
return res
66+
}
67+
68+
type Params httprouter.Params
69+
70+
func (ps Params) Get(key string) string {
71+
for i := range ps {
72+
if ps[i].Key == key {
73+
return ps[i].Value
74+
}
75+
}
76+
return ""
77+
}
78+
79+
func (ps Params) Values(key string) []string {
80+
for i := range ps {
81+
if ps[i].Key == key {
82+
return []string{ps[i].Value}
83+
}
84+
}
85+
return nil
86+
}

decoder/adapters_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package decoder_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/abemedia/go-don/decoder"
7+
"github.com/abemedia/httprouter"
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/valyala/fasthttp"
10+
)
11+
12+
func TestAdapters(t *testing.T) {
13+
t.Run("Args", func(t *testing.T) {
14+
in := &fasthttp.Args{}
15+
in.Add("string", "string")
16+
testAdapter(t, (*decoder.Args)(in))
17+
})
18+
19+
t.Run("Header", func(t *testing.T) {
20+
in := &fasthttp.RequestHeader{}
21+
in.Add("string", "string")
22+
testAdapter(t, (*decoder.Header)(in))
23+
})
24+
25+
t.Run("Params", func(t *testing.T) {
26+
in := httprouter.Params{{Key: "string", Value: "string"}}
27+
testAdapter(t, decoder.Params(in))
28+
})
29+
}
30+
31+
func testAdapter(t *testing.T, in decoder.Getter) {
32+
t.Helper()
33+
34+
type item struct {
35+
String string `field:"string"`
36+
Strings []string `field:"string"`
37+
}
38+
39+
want := &item{
40+
String: "string",
41+
Strings: []string{"string"},
42+
}
43+
44+
dec, err := decoder.NewCached(item{}, "field")
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
49+
got := &item{}
50+
if err = dec.Decode(in, got); err != nil {
51+
t.Fatal(err)
52+
}
53+
54+
if diff := cmp.Diff(want, got); diff != "" {
55+
t.Errorf(diff)
56+
}
57+
}

decoder/args.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

decoder/benchmark_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func BenchmarkDecoder(b *testing.B) {
3333
NestedPtr *child
3434
}
3535

36-
in := decoder.MapGetter{
36+
in := decoder.Map{
3737
"string": {"string"},
3838
"strings": {"string", "string"},
3939
"int": {"1"},
@@ -63,7 +63,7 @@ func BenchmarkDecoder(b *testing.B) {
6363
})
6464

6565
b.Run("DonCached", func(b *testing.B) {
66-
dec, err := decoder.NewCachedDecoder(test{}, "schema")
66+
dec, err := decoder.NewCached(test{}, "schema")
6767
if err != nil {
6868
b.Fatal(err)
6969
}
@@ -77,7 +77,7 @@ func BenchmarkDecoder(b *testing.B) {
7777
})
7878

7979
b.Run("Don", func(b *testing.B) {
80-
dec := decoder.NewDecoder("schema")
80+
dec := decoder.New("schema")
8181

8282
for i := 0; i < b.N; i++ {
8383
out := &test{}

decoder/cached.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)