Compare commits

...

1 Commits
v3 ... wrappers

Author SHA1 Message Date
35fcef4b8a initial stuff for wrappers
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2022-12-03 01:59:01 +03:00
2 changed files with 74 additions and 4 deletions

View File

@ -235,12 +235,71 @@ func (f *unwrap) format(v reflect.Value) {
return return
} }
// Handle pointers specially. if (kind == reflect.Ptr) && (!reflect.Indirect(v).IsValid()) {
if kind == reflect.Ptr {
f.formatPtr(v) f.formatPtr(v)
return return
} }
// Handle pointers specially.
if kind == reflect.Ptr {
fmt.Printf("AAAA %s\n", reflect.Indirect(v).Type().String())
switch reflect.Indirect(v).Type().String() {
case "sql.NullBool":
if eva := reflect.Indirect(v).FieldByName("Valid"); eva.IsValid() && eva.Bool() {
v = reflect.Indirect(v).FieldByName("Bool")
kind = v.Kind()
}
case "sql.NullByte":
if eva := reflect.Indirect(v).FieldByName("Valid"); eva.IsValid() && eva.Bool() {
v = reflect.Indirect(v).FieldByName("Byte")
kind = v.Kind()
}
case "sql.NullFloat64":
if eva := reflect.Indirect(v).FieldByName("Valid"); eva.IsValid() && eva.Bool() {
v = reflect.Indirect(v).FieldByName("Float64")
kind = v.Kind()
}
case "sql.NullInt16":
if eva := reflect.Indirect(v).FieldByName("Valid"); eva.IsValid() && eva.Bool() {
v = reflect.Indirect(v).FieldByName("Int16")
kind = v.Kind()
}
case "sql.NullInt32":
if eva := reflect.Indirect(v).FieldByName("Valid"); eva.IsValid() && eva.Bool() {
v = reflect.Indirect(v).FieldByName("Int32")
kind = v.Kind()
}
case "sql.NullInt64":
if eva := reflect.Indirect(v).FieldByName("Valid"); eva.IsValid() && eva.Bool() {
v = reflect.Indirect(v).FieldByName("Int64")
kind = v.Kind()
}
case "sql.NullString":
fmt.Printf("AAAAAAAAAA")
if eva := reflect.Indirect(v).FieldByName("Valid"); eva.IsValid() && eva.Bool() {
v = reflect.Indirect(v).FieldByName("String")
kind = v.Kind()
}
case "sql.NullTime":
if eva := reflect.Indirect(v).FieldByName("Valid"); eva.IsValid() && eva.Bool() {
v = reflect.Indirect(v).FieldByName("Time")
kind = v.Kind()
}
case "wrapperspb.BoolValue", "wrapperspb.BytesValue",
"wrapperspb.DoubleValue", "wrapperspb.FloatValue",
"wrapperspb.Int32Value", "wrapperspb.Int64Value",
"wrapperspb.UInt32Value", "wrapperspb.UInt64Value",
"wrapperspb.StringValue":
if eva := reflect.Indirect(v).FieldByName("Value"); eva.IsValid() {
v = eva
kind = v.Kind()
}
default:
f.formatPtr(v)
return
}
}
// get type information unless already handled elsewhere. // get type information unless already handled elsewhere.
if !f.ignoreNextType && f.s.Flag('#') { if !f.ignoreNextType && f.s.Flag('#') {
if v.Type().Kind() != reflect.Map && if v.Type().Kind() != reflect.Map &&

View File

@ -1,6 +1,7 @@
package unwrap package unwrap
import ( import (
"database/sql"
"fmt" "fmt"
"strings" "strings"
"testing" "testing"
@ -8,6 +9,15 @@ import (
"go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/codec"
) )
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)
}
func TestUnwrap(t *testing.T) { func TestUnwrap(t *testing.T) {
string1 := "string1" string1 := "string1"
string2 := "string2" string2 := "string2"
@ -22,8 +32,9 @@ func TestUnwrap(t *testing.T) {
v1 := &val1{ar: []*string{&string1, &string2}, str: &string1, val: &val1{str: &string2}, mp: map[string]string{"key": "val"}} v1 := &val1{ar: []*string{&string1, &string2}, str: &string1, val: &val1{str: &string2}, mp: map[string]string{"key": "val"}}
buf := fmt.Sprintf("%#v", Unwrap(v1)) 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 { 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"}}`
t.Fatalf("not proper written %s", buf) if strings.Compare(buf, chk) != 0 {
t.Fatalf("not proper written\n%s\n%s", buf, chk)
} }
type val2 struct { type val2 struct {