Skip to content

Commit

Permalink
type should contain package name
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed May 24, 2022
1 parent 254dc9b commit 742e2f2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/gop/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ func TestTypeName(t *testing.T) {
type c complex128
type b byte

g.Eq(gop.Plain(f(1)), "f(1.0)")
g.Eq(gop.Plain(i(1)), "i(1)")
g.Eq(gop.Plain(c(1)), "c(1+0i)")
g.Eq(gop.Plain(b('a')), "b(97)")
g.Eq(gop.Plain(f(1)), "gop_test.f(1.0)")
g.Eq(gop.Plain(i(1)), "gop_test.i(1)")
g.Eq(gop.Plain(c(1)), "gop_test.c(1+0i)")
g.Eq(gop.Plain(b('a')), "gop_test.b(97)")
}

func TestSliceCapNotEqual(t *testing.T) {
Expand Down
23 changes: 12 additions & 11 deletions lib/gop/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func tokenize(sn seen, p path, v reflect.Value) []*Token {
{Comment, wrapComment(formatUintptr(v.Pointer()))}}
}
return []*Token{{Func, "make"}, {ParenOpen, "("}, {Chan, "chan"},
typeName(v.Type().Elem().Name()), {InlineComma, ","},
typeName(v.Type().Elem().String()), {InlineComma, ","},
{Number, strconv.FormatInt(int64(v.Cap()), 10)}, {ParenClose, ")"},
{Comment, wrapComment(formatUintptr(v.Pointer()))}}

Expand Down Expand Up @@ -328,31 +328,32 @@ func tokenizeCollection(sn seen, p path, v reflect.Value) []*Token {
func tokenizeNumber(v reflect.Value) []*Token {
t := &Token{Nil, ""}
ts := []*Token{}
tname := v.Type().String()

switch v.Kind() {
case reflect.Int:
t.Type = Number
t.Literal = strconv.FormatInt(v.Int(), 10)
if v.Type().Name() != "int" {
ts = append(ts, typeName(v.Type().Name()), &Token{ParenOpen, "("}, t, &Token{ParenClose, ")"})
if tname != "int" {
ts = append(ts, typeName(tname), &Token{ParenOpen, "("}, t, &Token{ParenClose, ")"})
} else {
ts = append(ts, t)
}

case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
ts = append(ts, typeName(v.Type().Name()), &Token{ParenOpen, "("})
ts = append(ts, typeName(tname), &Token{ParenOpen, "("})
t.Type = Number
t.Literal = strconv.FormatInt(v.Int(), 10)
ts = append(ts, t, &Token{ParenClose, ")"})

case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
ts = append(ts, typeName(v.Type().Name()), &Token{ParenOpen, "("})
ts = append(ts, typeName(tname), &Token{ParenOpen, "("})
t.Type = Number
t.Literal = strconv.FormatUint(v.Uint(), 10)
ts = append(ts, t, &Token{ParenClose, ")"})

case reflect.Float32:
ts = append(ts, typeName(v.Type().Name()), &Token{ParenOpen, "("})
ts = append(ts, typeName(tname), &Token{ParenOpen, "("})
t.Type = Number
t.Literal = strconv.FormatFloat(v.Float(), 'f', -1, 32)
ts = append(ts, t, &Token{ParenClose, ")"})
Expand All @@ -363,14 +364,14 @@ func tokenizeNumber(v reflect.Value) []*Token {
if !strings.Contains(t.Literal, ".") {
t.Literal += ".0"
}
if v.Type().Name() != "float64" {
ts = append(ts, typeName(v.Type().Name()), &Token{ParenOpen, "("}, t, &Token{ParenClose, ")"})
if tname != "float64" {
ts = append(ts, typeName(tname), &Token{ParenOpen, "("}, t, &Token{ParenClose, ")"})
} else {
ts = append(ts, t)
}

case reflect.Complex64:
ts = append(ts, typeName(v.Type().Name()), &Token{ParenOpen, "("})
ts = append(ts, typeName(tname), &Token{ParenOpen, "("})
t.Type = Number
t.Literal = strconv.FormatComplex(v.Complex(), 'f', -1, 64)
t.Literal = t.Literal[1 : len(t.Literal)-1]
Expand All @@ -380,8 +381,8 @@ func tokenizeNumber(v reflect.Value) []*Token {
t.Type = Number
t.Literal = strconv.FormatComplex(v.Complex(), 'f', -1, 128)
t.Literal = t.Literal[1 : len(t.Literal)-1]
if v.Type().Name() != "complex128" {
ts = append(ts, typeName(v.Type().Name()), &Token{ParenOpen, "("}, t, &Token{ParenClose, ")"})
if tname != "complex128" {
ts = append(ts, typeName(tname), &Token{ParenOpen, "("}, t, &Token{ParenClose, ")"})
} else {
ts = append(ts, t)
}
Expand Down

0 comments on commit 742e2f2

Please sign in to comment.