2022-10-11 00:30:42 +03:00
|
|
|
package unwrap
|
|
|
|
|
|
|
|
import (
|
2022-12-03 01:59:01 +03:00
|
|
|
"database/sql"
|
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"
|
|
|
|
)
|
|
|
|
|
2022-12-03 01:59:01 +03:00
|
|
|
func TestSql(t *testing.T) {
|
|
|
|
type val struct {
|
|
|
|
S sql.NullString
|
|
|
|
}
|
|
|
|
v := &val{S: sql.NullString{Valid: true, String: "test"}}
|
|
|
|
buf := fmt.Sprintf("%#v", Unwrap(v))
|
|
|
|
t.Logf(buf)
|
|
|
|
}
|
|
|
|
|
2022-10-11 00:30:42 +03:00
|
|
|
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))
|
2022-12-03 01:59:01 +03:00
|
|
|
chk := `&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"}}`
|
|
|
|
if strings.Compare(buf, chk) != 0 {
|
|
|
|
t.Fatalf("not proper written\n%s\n%s", buf, chk)
|
2022-11-19 15:20:42 +03:00
|
|
|
}
|
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"`
|
|
|
|
}
|
|
|
|
v1 := &val{Key1: "val1", Key2: "val2"}
|
|
|
|
buf := fmt.Sprintf("%#v", Unwrap(v1))
|
|
|
|
if strings.Compare(buf, `&unwrap.val{Key2:"val2"}`) != 0 {
|
|
|
|
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
|
|
|
}
|