2022-10-11 00:30:42 +03:00
|
|
|
package unwrap
|
|
|
|
|
|
|
|
import (
|
2022-11-19 15:20:42 +03:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2022-10-11 00:30:42 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUnwrap(t *testing.T) {
|
|
|
|
string1 := "string1"
|
|
|
|
string2 := "string2"
|
|
|
|
|
|
|
|
type val1 struct {
|
|
|
|
mp map[string]string
|
|
|
|
val *val1
|
|
|
|
str *string
|
|
|
|
ar []*string
|
|
|
|
}
|
|
|
|
|
|
|
|
v1 := &val1{ar: []*string{&string1, &string2}, str: &string1, val: &val1{str: &string2}, mp: map[string]string{"key": "val"}}
|
|
|
|
|
2022-11-19 15:20:42 +03:00
|
|
|
buf := fmt.Sprintf("%#v", Unwrap(v1))
|
|
|
|
if strings.Compare(buf, `&unwrap.val1{mp:map[string]string{"key":"val"}, val:(*unwrap.val1){mp:map[string]string<nil>, val:(*unwrap.val1)<nil>, str:(*string)"string2", ar:[]*string<nil>}, str:(*string)"string1", ar:[]*string{<*><shown>, <*>"string2"}}`) != 0 {
|
|
|
|
t.Fatalf("not proper written %s", buf)
|
|
|
|
}
|
2022-10-11 00:30:42 +03:00
|
|
|
|
|
|
|
type val2 struct {
|
|
|
|
mp map[string]string
|
|
|
|
val *val2
|
|
|
|
str string
|
|
|
|
ar []string
|
|
|
|
}
|
|
|
|
|
|
|
|
v2 := &val2{ar: []string{string1, string2}, str: string1, val: &val2{str: string2}, mp: map[string]string{"key": "val"}}
|
2022-11-19 15:20:42 +03:00
|
|
|
_ = v2
|
|
|
|
// t.Logf("output: %#v", v2)
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
|
|
|
|
2022-11-19 15:20:42 +03:00
|
|
|
func TestCodec(t *testing.T) {
|
2022-10-11 00:30:42 +03:00
|
|
|
type val struct {
|
|
|
|
MP map[string]string `json:"mp"`
|
|
|
|
STR string `json:"str"`
|
|
|
|
AR []string `json:"ar"`
|
|
|
|
}
|
|
|
|
|
|
|
|
v1 := &val{AR: []string{"string1", "string2"}, STR: "string", MP: map[string]string{"key": "val"}}
|
|
|
|
|
2022-11-19 15:20:42 +03:00
|
|
|
buf := fmt.Sprintf("%#v", Unwrap(v1, Codec(codec.NewCodec())))
|
|
|
|
if strings.Compare(buf, `{"mp":{"key":"val"},"str":"string","ar":["string1","string2"]}`) != 0 {
|
|
|
|
t.Fatalf("not proper written %s", buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOmit(t *testing.T) {
|
|
|
|
type val struct {
|
|
|
|
Key1 string `logger:"omit"`
|
|
|
|
Key2 string `logger:"take"`
|
2022-12-29 23:16:58 +03:00
|
|
|
Key3 string
|
2022-11-19 15:20:42 +03:00
|
|
|
}
|
2022-12-29 23:16:58 +03:00
|
|
|
v1 := &val{Key1: "val1", Key2: "val2", Key3: "val3"}
|
2022-11-19 15:20:42 +03:00
|
|
|
buf := fmt.Sprintf("%#v", Unwrap(v1))
|
2022-12-29 23:16:58 +03:00
|
|
|
if strings.Compare(buf, `&unwrap.val{Key2:"val2", Key3:"val3"}`) != 0 {
|
2022-11-19 15:20:42 +03:00
|
|
|
t.Fatalf("not proper written %s", buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTagged(t *testing.T) {
|
|
|
|
type val struct {
|
|
|
|
Key1 string `logger:"take"`
|
|
|
|
Key2 string
|
|
|
|
}
|
|
|
|
|
|
|
|
v1 := &val{Key1: "val1", Key2: "val2"}
|
|
|
|
buf := fmt.Sprintf("%#v", Unwrap(v1, Tagged(true)))
|
|
|
|
if strings.Compare(buf, `&unwrap.val{Key1:"val1"}`) != 0 {
|
|
|
|
t.Fatalf("not proper written %s", buf)
|
|
|
|
}
|
2022-10-11 00:30:42 +03:00
|
|
|
}
|
2022-12-29 23:16:58 +03:00
|
|
|
|
|
|
|
func TestTaggedNested(t *testing.T) {
|
|
|
|
type val struct {
|
|
|
|
key string `logger:"take"`
|
|
|
|
val string `logger:"omit"`
|
|
|
|
unk string
|
|
|
|
}
|
|
|
|
type str struct {
|
2023-01-06 22:41:57 +03:00
|
|
|
key string `logger:"omit"`
|
2023-02-03 23:33:24 +03:00
|
|
|
val *val `logger:"take"`
|
2022-12-29 23:16:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var iface interface{}
|
2023-02-03 23:33:24 +03:00
|
|
|
v := &str{val: &val{key: "test", unk: "unk"}}
|
2022-12-29 23:16:58 +03:00
|
|
|
iface = v
|
|
|
|
buf := fmt.Sprintf("%#v", Unwrap(iface, Tagged(true)))
|
|
|
|
if strings.Compare(buf, `&unwrap.str{val:(*unwrap.val){key:"test"}}`) != 0 {
|
|
|
|
t.Fatalf("not proper written %s", buf)
|
|
|
|
}
|
|
|
|
}
|