2024-12-29 20:38:20 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2025-01-02 18:44:28 +00:00
|
|
|
"sync"
|
2024-12-29 20:38:20 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetValue_string(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
type args struct {
|
|
|
|
vals map[string]any
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
args: args{
|
|
|
|
vals: map[string]any{
|
|
|
|
"1": "123",
|
|
|
|
"2": "234",
|
|
|
|
},
|
|
|
|
key: "1",
|
|
|
|
},
|
|
|
|
want: "123",
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "value not presented",
|
|
|
|
args: args{
|
|
|
|
vals: map[string]any{
|
|
|
|
"1": "123",
|
|
|
|
"2": "234",
|
|
|
|
},
|
|
|
|
key: "3",
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil map",
|
|
|
|
args: args{
|
|
|
|
vals: nil,
|
|
|
|
key: "1",
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid type",
|
|
|
|
args: args{
|
|
|
|
vals: map[string]any{
|
|
|
|
"1": "123",
|
|
|
|
"2": 234,
|
|
|
|
},
|
|
|
|
key: "2",
|
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2025-01-02 18:44:28 +00:00
|
|
|
got, err := GetValue[string](_mapToSyncMap(tt.args.vals), tt.args.key)
|
2024-12-29 20:38:20 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetValue() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetValue() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetValue_struct(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type val struct {
|
|
|
|
a int
|
|
|
|
b string
|
|
|
|
c bool
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
vals map[string]any
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want val
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
args: args{
|
|
|
|
vals: map[string]any{
|
|
|
|
"1": val{
|
|
|
|
a: 1,
|
|
|
|
b: "2",
|
|
|
|
c: true,
|
|
|
|
},
|
|
|
|
"2": "234",
|
|
|
|
},
|
|
|
|
key: "1",
|
|
|
|
},
|
|
|
|
want: val{
|
|
|
|
a: 1,
|
|
|
|
b: "2",
|
|
|
|
c: true,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "value not presented",
|
|
|
|
args: args{
|
|
|
|
vals: map[string]any{
|
|
|
|
"1": "123",
|
|
|
|
"2": "234",
|
|
|
|
},
|
|
|
|
key: "3",
|
|
|
|
},
|
|
|
|
want: val{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil map",
|
|
|
|
args: args{
|
|
|
|
vals: nil,
|
|
|
|
key: "1",
|
|
|
|
},
|
|
|
|
want: val{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid type",
|
|
|
|
args: args{
|
|
|
|
vals: map[string]any{
|
|
|
|
"1": "123",
|
|
|
|
"2": 234,
|
|
|
|
},
|
|
|
|
key: "2",
|
|
|
|
},
|
|
|
|
want: val{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2025-01-02 18:44:28 +00:00
|
|
|
got, err := GetValue[val](_mapToSyncMap(tt.args.vals), tt.args.key)
|
2024-12-29 20:38:20 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetValue() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetValue() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetValue_structptr(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type val struct {
|
|
|
|
a int
|
|
|
|
b string
|
|
|
|
c bool
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
vals map[string]any
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *val
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
args: args{
|
|
|
|
vals: map[string]any{
|
|
|
|
"1": &val{
|
|
|
|
a: 1,
|
|
|
|
b: "2",
|
|
|
|
c: true,
|
|
|
|
},
|
|
|
|
"2": "234",
|
|
|
|
},
|
|
|
|
key: "1",
|
|
|
|
},
|
|
|
|
want: &val{
|
|
|
|
a: 1,
|
|
|
|
b: "2",
|
|
|
|
c: true,
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "value not presented",
|
|
|
|
args: args{
|
|
|
|
vals: map[string]any{
|
|
|
|
"1": "123",
|
|
|
|
"2": "234",
|
|
|
|
},
|
|
|
|
key: "3",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "nil map",
|
|
|
|
args: args{
|
|
|
|
vals: nil,
|
|
|
|
key: "1",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid type",
|
|
|
|
args: args{
|
|
|
|
vals: map[string]any{
|
|
|
|
"1": "123",
|
|
|
|
"2": 234,
|
|
|
|
},
|
|
|
|
key: "2",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2025-01-02 18:44:28 +00:00
|
|
|
got, err := GetValue[*val](_mapToSyncMap(tt.args.vals), tt.args.key)
|
2024-12-29 20:38:20 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GetValue() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
|
|
t.Errorf("GetValue() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2025-01-02 18:44:28 +00:00
|
|
|
|
|
|
|
func _mapToSyncMap(m map[string]any) *sync.Map {
|
|
|
|
out := &sync.Map{}
|
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
out.Store(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|