Skip to content

Commit 1f3e8fe

Browse files
authored
feat: add pointer func (#198)
1 parent d6a6fe8 commit 1f3e8fe

File tree

5 files changed

+150
-23
lines changed

5 files changed

+150
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- chore: improve kakfa component
66
- chore: improve route not found method
7+
- feat(utils): add func pointer
8+
- feat: add sonic for encoding
79

810
## v1.10.0
911

go.mod

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22
55
require (
66
github.com/IBM/sarama v1.45.1
77
github.com/alicebob/miniredis/v2 v2.15.1
8+
github.com/bytedance/sonic v1.14.1
89
github.com/cenkalti/backoff/v4 v4.2.1
910
github.com/dgraph-io/ristretto v0.1.0
1011
github.com/elastic/go-elasticsearch/v7 v7.17.10
@@ -29,7 +30,6 @@ require (
2930
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
3031
github.com/hashicorp/consul/api v1.11.0
3132
github.com/hibiken/asynq v0.23.0
32-
github.com/json-iterator/go v1.1.12
3333
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
3434
github.com/nacos-group/nacos-sdk-go v1.1.1
3535
github.com/nats-io/nats.go v1.16.1-0.20220906180156-a1017eec10b0
@@ -88,10 +88,10 @@ require (
8888
github.com/armon/go-metrics v0.3.10 // indirect
8989
github.com/beorn7/perks v1.0.1 // indirect
9090
github.com/buger/jsonparser v1.1.1 // indirect
91-
github.com/bytedance/sonic v1.11.2 // indirect
91+
github.com/bytedance/gopkg v0.1.3 // indirect
92+
github.com/bytedance/sonic/loader v0.3.0 // indirect
9293
github.com/cespare/xxhash/v2 v2.2.0 // indirect
93-
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
94-
github.com/chenzhuoyu/iasm v0.9.1 // indirect
94+
github.com/cloudwego/base64x v0.1.6 // indirect
9595
github.com/coreos/go-semver v0.3.0 // indirect
9696
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
9797
github.com/davecgh/go-spew v1.1.1 // indirect
@@ -143,8 +143,9 @@ require (
143143
github.com/jinzhu/now v1.1.5 // indirect
144144
github.com/jmespath/go-jmespath v0.4.0 // indirect
145145
github.com/josharian/intern v1.0.0 // indirect
146+
github.com/json-iterator/go v1.1.12 // indirect
146147
github.com/klauspost/compress v1.17.11 // indirect
147-
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
148+
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
148149
github.com/leodido/go-urn v1.4.0 // indirect
149150
github.com/lestrrat-go/strftime v1.0.5 // indirect
150151
github.com/lufia/plan9stats v0.0.0-20230110061619-bbe2e5e100de // indirect

go.sum

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,27 @@ github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
9090
github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
9191
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
9292
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
93-
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
94-
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
95-
github.com/bytedance/sonic v1.11.2 h1:ywfwo0a/3j9HR8wsYGWsIWl2mvRsI950HyoxiBERw5A=
96-
github.com/bytedance/sonic v1.11.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
93+
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
94+
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
95+
github.com/bytedance/sonic v1.14.1 h1:FBMC0zVz5XUmE4z9wF4Jey0An5FueFvOsTKKKtwIl7w=
96+
github.com/bytedance/sonic v1.14.1/go.mod h1:gi6uhQLMbTdeP0muCnrjHLeCUPyb70ujhnNlhOylAFc=
97+
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
98+
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
9799
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
98100
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
99101
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
100102
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
101103
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
102104
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
103105
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
104-
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
105-
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
106-
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
107-
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
108-
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
109-
github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0=
110-
github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
111106
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
112107
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
113108
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
114109
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
115110
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
116111
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
112+
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
113+
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
117114
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
118115
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
119116
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
@@ -466,10 +463,8 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
466463
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
467464
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
468465
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
469-
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
470-
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
471-
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
472-
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
466+
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
467+
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
473468
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
474469
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
475470
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
@@ -824,7 +819,6 @@ go.uber.org/zap v1.15.0/go.mod h1:Mb2vm2krFEG5DV0W9qcHBYFtp/Wku1cvYaqPsS/WYfc=
824819
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
825820
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
826821
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
827-
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
828822
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
829823
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
830824
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
@@ -1295,9 +1289,7 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
12951289
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
12961290
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
12971291
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
1298-
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
12991292
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
1300-
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
13011293
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
13021294
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
13031295
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=

pkg/utils/pointer/pointer.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package pointer
2+
3+
import "reflect"
4+
5+
// Ptr returns a pointer to its argument.
6+
// For example, Ptr[int](100) returns *int pointing to 100.
7+
func Ptr[T any](t T) *T { return &t }
8+
9+
// Value returns the value that the pointer p points to.
10+
// If p is nil, it returns the zero value of the type T.
11+
func Value[T any](p *T) T {
12+
if p == nil {
13+
var zero T
14+
return zero
15+
}
16+
return *p
17+
}
18+
19+
// IsStructPtr checks if the given interface is a pointer to a struct.
20+
func IsStructPtr(x any) bool {
21+
if x == nil {
22+
return false
23+
}
24+
t := reflect.TypeOf(x)
25+
return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
26+
}

pkg/utils/pointer/pointer_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package pointer
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestPtr(t *testing.T) {
9+
t.Run("int", func(t *testing.T) {
10+
val := 100
11+
ptr := Ptr(val)
12+
13+
if ptr == nil {
14+
t.Fatal("ToPtr() returned nil")
15+
}
16+
if *ptr != val {
17+
t.Errorf("expected value %d, got %d", val, *ptr)
18+
}
19+
if reflect.TypeOf(ptr).Kind() != reflect.Ptr {
20+
t.Errorf("expected a pointer type, got %T", ptr)
21+
}
22+
})
23+
24+
t.Run("string", func(t *testing.T) {
25+
val := "hello"
26+
ptr := Ptr(val)
27+
28+
if ptr == nil {
29+
t.Fatal("Ptr() returned nil")
30+
}
31+
if *ptr != val {
32+
t.Errorf("expected value %q, got %q", val, *ptr)
33+
}
34+
})
35+
}
36+
37+
func TestIsStructPtr(t *testing.T) {
38+
type testStruct struct {
39+
Name string
40+
}
41+
var nilStructPtr *testStruct
42+
43+
testCases := []struct {
44+
name string
45+
input any
46+
expected bool
47+
}{
48+
{"pointer to struct", &testStruct{Name: "test"}, true},
49+
{"nil pointer to struct", nilStructPtr, true},
50+
{"struct value", testStruct{Name: "test"}, false},
51+
{"pointer to int", Ptr(123), false},
52+
{"int value", 123, false},
53+
{"pointer to string", Ptr("hello"), false},
54+
{"string value", "hello", false},
55+
{"nil interface", nil, false},
56+
{"pointer to pointer to struct", Ptr(&testStruct{}), false},
57+
}
58+
59+
for _, tc := range testCases {
60+
t.Run(tc.name, func(t *testing.T) {
61+
result := IsStructPtr(tc.input)
62+
if result != tc.expected {
63+
t.Errorf("IsStructPtr(%v) = %v; want %v", tc.input, result, tc.expected)
64+
}
65+
})
66+
}
67+
}
68+
69+
func TestValue(t *testing.T) {
70+
type testStruct struct {
71+
Name string
72+
}
73+
74+
testCases := []struct {
75+
name string
76+
input any
77+
expected any
78+
}{
79+
{"non-nil int pointer", Ptr(123), 123},
80+
{"nil int pointer", (*int)(nil), 0},
81+
{"non-nil string pointer", Ptr("hello"), "hello"},
82+
{"nil string pointer", (*string)(nil), ""},
83+
{"non-nil struct pointer", &testStruct{Name: "test"}, testStruct{Name: "test"}},
84+
{"nil struct pointer", (*testStruct)(nil), testStruct{}},
85+
}
86+
87+
for _, tc := range testCases {
88+
t.Run(tc.name, func(t *testing.T) {
89+
var result any
90+
switch v := tc.input.(type) {
91+
case *int:
92+
result = Value(v)
93+
case *string:
94+
result = Value(v)
95+
case *testStruct:
96+
result = Value(v)
97+
default:
98+
t.Fatalf("unhandled test case type: %T", tc.input)
99+
}
100+
101+
if result != tc.expected {
102+
t.Errorf("Value() = %v; want %v", result, tc.expected)
103+
}
104+
})
105+
}
106+
}

0 commit comments

Comments
 (0)