Skip to content

Commit b698c7a

Browse files
committed
refactor: get rid of unnecessary dep
1 parent 7c0726c commit b698c7a

25 files changed

+495
-322
lines changed

.github/CODEOWNERS

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

.github/PULL_REQUEST_TEMPLATE.md

Whitespace-only changes.

go.mod

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@ module github.com/cloudwego/frugal
22

33
go 1.20
44

5-
require (
6-
github.com/cloudwego/gopkg v0.1.2
7-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
8-
github.com/stretchr/testify v1.9.0
9-
)
5+
require github.com/cloudwego/gopkg v0.2.0
106

11-
require (
12-
github.com/bytedance/gopkg v0.1.1 // indirect
13-
github.com/kr/text v0.2.0 // indirect
14-
github.com/pmezard/go-difflib v1.0.0 // indirect
15-
gopkg.in/yaml.v3 v3.0.1 // indirect
16-
)
7+
require github.com/bytedance/gopkg v0.1.4 // indirect

go.sum

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
1-
github.com/bytedance/gopkg v0.1.1 h1:3azzgSkiaw79u24a+w9arfH8OfnQQ4MHUt9lJFREEaE=
2-
github.com/bytedance/gopkg v0.1.1/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
3-
github.com/cloudwego/gopkg v0.1.2 h1:650t+RiZGht8qX+y0hl49JXJCuO44GhbGZuxDzr2PyI=
4-
github.com/cloudwego/gopkg v0.1.2/go.mod h1:WoNTdXDPdvL97cBmRUWXVGkh2l2UFmpd9BUvbW2r0Aw=
5-
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
6-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
7-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
9-
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
10-
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
11-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
14-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
15-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
16-
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
17-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
18-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1+
github.com/bytedance/gopkg v0.1.4 h1:oZnQwnX82KAIWb7033bEwtxvTqXcYMxDBaQxo5JJHWM=
2+
github.com/bytedance/gopkg v0.1.4/go.mod h1:v1zWfPm21Fb+OsyXN2VAHdL6TBb2L88anLQgdyje6R4=
3+
github.com/cloudwego/gopkg v0.2.0 h1:EU8Ahrj0rCfKZQdah50zKnlrQ1o2AdPYM87UclIqLME=
4+
github.com/cloudwego/gopkg v0.2.0/go.mod h1:WjQPYI8PesfQalIVcLzVJBb1EAopioZ+D+3UGJ+dNBs=

internal/assert/assert.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright 2025 CloudWeGo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package assert provides minimal test assertion helpers.
16+
// All functions stop the test immediately on failure (t.Fatalf).
17+
//
18+
// Design: keep this package small — avoid adding new functions unless
19+
// there is a clear need that cannot be covered by the existing ones.
20+
// Prefer: Equal for comparable types, BytesEqual for []byte,
21+
// DeepEqual for structs/maps/slices, True for boolean conditions,
22+
// and Nil for nil checks.
23+
package assert
24+
25+
import (
26+
"bytes"
27+
"fmt"
28+
"reflect"
29+
"testing"
30+
)
31+
32+
// Equal asserts that two comparable values are equal using ==.
33+
func Equal[T comparable](t testing.TB, expected, actual T, a ...any) {
34+
if expected != actual {
35+
t.Helper()
36+
t.Fatalf("expected %#v, got %#v%s", expected, actual, fmtInputs(a))
37+
}
38+
}
39+
40+
// BytesEqual asserts that two byte slices are equal.
41+
// On failure it reports the length mismatch or the first differing byte,
42+
// avoiding logging raw byte slices which can be large and unreadable.
43+
func BytesEqual(t testing.TB, expected, actual []byte, a ...any) {
44+
if bytes.Equal(expected, actual) {
45+
return
46+
}
47+
t.Helper()
48+
if len(expected) != len(actual) {
49+
t.Fatalf("bytes differ in length: expected %d, got %d%s", len(expected), len(actual), fmtInputs(a))
50+
return
51+
}
52+
for i := range expected {
53+
if expected[i] != actual[i] {
54+
t.Fatalf("bytes differ at [%d]: expected 0x%02x, got 0x%02x%s", i, expected[i], actual[i], fmtInputs(a))
55+
return
56+
}
57+
}
58+
}
59+
60+
// DeepEqual asserts that two values are deeply equal using reflect.DeepEqual.
61+
// Use for structs, maps, and non-byte slices. Use BytesEqual for []byte.
62+
func DeepEqual(t testing.TB, expected, actual any, a ...any) {
63+
if !reflect.DeepEqual(expected, actual) {
64+
t.Helper()
65+
t.Fatalf("expected %#v, got %#v%s", expected, actual, fmtInputs(a))
66+
}
67+
}
68+
69+
// True asserts that cond is true.
70+
func True(t testing.TB, cond bool, a ...any) {
71+
if !cond {
72+
t.Helper()
73+
t.Fatalf("expected true%s", fmtInputs(a))
74+
}
75+
}
76+
77+
// Nil asserts that v is nil. Uses reflect to handle typed nils (e.g. (*T)(nil)).
78+
func Nil(t testing.TB, v any, a ...any) {
79+
if !isNil(v) {
80+
t.Helper()
81+
t.Fatalf("expected nil, got: %v%s", v, fmtInputs(a))
82+
}
83+
}
84+
85+
func isNil(v any) bool {
86+
if v == nil {
87+
return true
88+
}
89+
rv := reflect.ValueOf(v)
90+
switch rv.Kind() {
91+
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
92+
return rv.IsNil()
93+
case reflect.UnsafePointer:
94+
return rv.Pointer() == 0
95+
}
96+
return false
97+
}
98+
99+
func fmtInputs(a []any) string {
100+
if len(a) == 0 {
101+
return ""
102+
}
103+
return ": " + fmt.Sprint(a...)
104+
}

internal/assert/assert_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Copyright 2025 CloudWeGo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package assert
16+
17+
import (
18+
"fmt"
19+
"strings"
20+
"testing"
21+
"unsafe"
22+
)
23+
24+
// mockT captures Fatalf calls without stopping execution.
25+
type mockT struct {
26+
testing.TB // embed to satisfy interface; only Helper/Fatalf are called
27+
msg string
28+
}
29+
30+
func (m *mockT) Helper() {}
31+
func (m *mockT) Fatalf(f string, a ...any) { m.msg = fmt.Sprintf(f, a...) }
32+
func (m *mockT) failed() bool { return m.msg != "" }
33+
34+
func TestEqual(t *testing.T) {
35+
m := &mockT{}
36+
Equal(m, 1, 1)
37+
True(t, !m.failed())
38+
39+
Equal(m, 1, 2)
40+
True(t, m.failed())
41+
}
42+
43+
func TestBytesEqual(t *testing.T) {
44+
m := &mockT{}
45+
46+
BytesEqual(m, []byte("abc"), []byte("abc"))
47+
True(t, !m.failed())
48+
49+
// length diff
50+
BytesEqual(m, []byte("ab"), []byte("abc"))
51+
True(t, strings.Contains(m.msg, "length"))
52+
53+
// content diff
54+
m.msg = ""
55+
BytesEqual(m, []byte{0x01, 0x02}, []byte{0x01, 0x03})
56+
True(t, strings.Contains(m.msg, "[1]"))
57+
True(t, strings.Contains(m.msg, "0x02"))
58+
True(t, strings.Contains(m.msg, "0x03"))
59+
}
60+
61+
func TestDeepEqual(t *testing.T) {
62+
m := &mockT{}
63+
64+
DeepEqual(m, []int{1, 2}, []int{1, 2})
65+
True(t, !m.failed())
66+
67+
DeepEqual(m, []int{1, 2}, []int{1, 3})
68+
True(t, m.failed())
69+
}
70+
71+
func TestTrue(t *testing.T) {
72+
m := &mockT{}
73+
74+
True(m, true)
75+
True(t, !m.failed())
76+
77+
True(m, false)
78+
True(t, m.failed())
79+
}
80+
81+
func TestNil(t *testing.T) {
82+
m := &mockT{}
83+
84+
// untyped nil
85+
Nil(m, nil)
86+
True(t, !m.failed())
87+
88+
// typed nil pointer
89+
var p *int
90+
Nil(m, p)
91+
True(t, !m.failed())
92+
93+
// typed nil slice
94+
var s []byte
95+
Nil(m, s)
96+
True(t, !m.failed())
97+
98+
// typed nil map
99+
var mp map[string]int
100+
Nil(m, mp)
101+
True(t, !m.failed())
102+
103+
// unsafe.Pointer nil
104+
Nil(m, unsafe.Pointer(nil))
105+
True(t, !m.failed())
106+
107+
// non-nil value should fail
108+
x := 42
109+
Nil(m, &x)
110+
True(t, m.failed())
111+
}
112+
113+
func TestFmtInputs(t *testing.T) {
114+
m := &mockT{}
115+
116+
Equal(m, 1, 2, "context", "info")
117+
True(t, strings.Contains(m.msg, "context"))
118+
}

internal/defs/defaults_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ import (
2121
"testing"
2222
"unsafe"
2323

24-
"github.com/davecgh/go-spew/spew"
25-
"github.com/stretchr/testify/require"
24+
"github.com/cloudwego/frugal/internal/assert"
2625
)
2726

2827
type CtorTestStruct struct {
@@ -42,9 +41,7 @@ func (p *CtorTestStruct) InitDefault() {
4241
func TestDefaults_Resolve(t *testing.T) {
4342
fp := (*CtorTestStruct).InitDefault
4443
fa := **(**unsafe.Pointer)(unsafe.Pointer(&fp))
45-
spew.Dump(fa)
4644
fn, err := GetDefaultInitializer(reflect.TypeOf(CtorTestStruct{}))
47-
require.NoError(t, err)
48-
spew.Dump(fn)
49-
require.Equal(t, fa, fn)
45+
assert.Nil(t, err)
46+
assert.Equal(t, fa, fn)
5047
}

internal/defs/resolver_test.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package defs
1818

1919
import (
2020
"reflect"
21+
"strings"
2122
"testing"
2223

23-
"github.com/davecgh/go-spew/spew"
24-
"github.com/stretchr/testify/require"
24+
"github.com/cloudwego/frugal/internal/assert"
2525
)
2626

2727
type NoCopyStringFields struct {
@@ -35,10 +35,8 @@ type NoCopyStringFields struct {
3535
func TestResolver_StringOptions(t *testing.T) {
3636
var vv NoCopyStringFields
3737
ret, err := ResolveFields(reflect.TypeOf(vv))
38-
require.NoError(t, err)
39-
spew.Config.SortKeys = true
40-
spew.Config.DisablePointerMethods = true
41-
spew.Dump(ret)
38+
assert.Nil(t, err)
39+
assert.Equal(t, 5, len(ret))
4240
}
4341

4442
func TestLookupStructTag(t *testing.T) {
@@ -83,8 +81,8 @@ func TestLookupStructTag(t *testing.T) {
8381
for _, tt := range tests {
8482
t.Run(tt.name, func(t *testing.T) {
8583
result, ok := lookupStructTag(tt.tag)
86-
require.Equal(t, tt.ok, ok)
87-
require.Equal(t, tt.expected, result)
84+
assert.Equal(t, tt.ok, ok)
85+
assert.DeepEqual(t, tt.expected, result)
8886
})
8987
}
9088
}
@@ -103,17 +101,17 @@ type DuplicateIDFields struct {
103101
func TestResolveFields_ThriftTagRequiredness(t *testing.T) {
104102
var vv ThriftTagFields
105103
ret, err := ResolveFields(reflect.TypeOf(vv))
106-
require.NoError(t, err)
107-
require.Len(t, ret, 3)
104+
assert.Nil(t, err)
105+
assert.Equal(t, 3, len(ret))
108106

109-
require.Equal(t, Required, ret[0].Spec)
110-
require.Equal(t, Default, ret[1].Spec)
111-
require.Equal(t, Optional, ret[2].Spec)
107+
assert.Equal(t, Required, ret[0].Spec)
108+
assert.Equal(t, Default, ret[1].Spec)
109+
assert.Equal(t, Optional, ret[2].Spec)
112110
}
113111

114112
func TestResolveFields_DuplicateID(t *testing.T) {
115113
var vv DuplicateIDFields
116114
_, err := ResolveFields(reflect.TypeOf(vv))
117-
require.Error(t, err)
118-
require.Contains(t, err.Error(), "duplicated field ID 1")
115+
assert.True(t, err != nil)
116+
assert.True(t, strings.Contains(err.Error(), "duplicated field ID 1"))
119117
}

0 commit comments

Comments
 (0)