Skip to content

Commit 8d1f4e8

Browse files
Rakothantonmedv
authored andcommitted
docgen: add package name to types
1 parent d676309 commit 8d1f4e8

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

docgen/docgen.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type TypeName string
2020
type Context struct {
2121
Variables map[Identifier]*Type `json:"variables"`
2222
Types map[TypeName]*Type `json:"types"`
23+
pkgPath string
2324
}
2425

2526
type Type struct {
@@ -52,6 +53,7 @@ func CreateDoc(i interface{}) *Context {
5253
c := &Context{
5354
Variables: make(map[Identifier]*Type),
5455
Types: make(map[TypeName]*Type),
56+
pkgPath: dereference(reflect.TypeOf(i)).PkgPath(),
5557
}
5658

5759
for name, t := range conf.CreateTypesTable(i) {
@@ -102,9 +104,7 @@ func (c *Context) use(t reflect.Type, ops ...option) *Type {
102104
methods = append(methods, m)
103105
}
104106

105-
for t.Kind() == reflect.Ptr {
106-
t = t.Elem()
107-
}
107+
t = dereference(t)
108108

109109
// Only named types will have methods defined on them.
110110
// It maybe not even struct, but we gonna call then
@@ -169,8 +169,12 @@ func (c *Context) use(t reflect.Type, ops ...option) *Type {
169169
}
170170

171171
appendix:
172-
name := TypeName(t.Name())
173-
anonymous := name == ""
172+
173+
name := TypeName(t.String())
174+
if c.pkgPath == t.PkgPath() {
175+
name = TypeName(t.Name())
176+
}
177+
anonymous := t.Name() == ""
174178

175179
a, ok := c.Types[name]
176180

@@ -219,3 +223,13 @@ func isPrivate(s string) bool {
219223
func isProtobuf(s string) bool {
220224
return strings.HasPrefix(s, "XXX_")
221225
}
226+
227+
func dereference(t reflect.Type) reflect.Type {
228+
if t == nil {
229+
return nil
230+
}
231+
if t.Kind() == reflect.Ptr {
232+
t = dereference(t.Elem())
233+
}
234+
return t
235+
}

docgen/docgen_test.go

+47-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package docgen_test
22

33
import (
4-
"github.com/stretchr/testify/require"
5-
"math"
6-
"testing"
7-
84
. "github.com/antonmedv/expr/docgen"
95
"github.com/sanity-io/litter"
106
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
8+
"math"
9+
"testing"
10+
"time"
1111
)
1212

1313
type Tweet struct {
@@ -21,6 +21,15 @@ type Env struct {
2121
MaxSize int32
2222
}
2323
Env map[string]interface{}
24+
// NOTE: conflicting type name
25+
TimeWeekday time.Weekday
26+
Weekday Weekday
27+
}
28+
29+
type Weekday int
30+
31+
func (Weekday) String() string {
32+
return ""
2433
}
2534

2635
type Duration int
@@ -64,6 +73,14 @@ func TestCreateDoc(t *testing.T) {
6473
},
6574
Return: &Type{Kind: "struct", Name: "Duration"},
6675
},
76+
"TimeWeekday": {
77+
Name: "time.Weekday",
78+
Kind: "struct",
79+
},
80+
"Weekday": {
81+
Name: "Weekday",
82+
Kind: "struct",
83+
},
6784
},
6885
Types: map[TypeName]*Type{
6986
"Tweet": {
@@ -85,6 +102,30 @@ func TestCreateDoc(t *testing.T) {
85102
},
86103
},
87104
},
105+
"time.Weekday": {
106+
Kind: "struct",
107+
Fields: map[Identifier]*Type{
108+
"String": {
109+
Kind: "func",
110+
Arguments: []*Type{},
111+
Return: &Type{
112+
Kind: "string",
113+
},
114+
},
115+
},
116+
},
117+
"Weekday": {
118+
Kind: "struct",
119+
Fields: map[Identifier]*Type{
120+
"String": {
121+
Kind: "func",
122+
Arguments: []*Type{},
123+
Return: &Type{
124+
Kind: "string",
125+
},
126+
},
127+
},
128+
},
88129
},
89130
}
90131

@@ -108,7 +149,7 @@ func TestCreateDoc_FromMap(t *testing.T) {
108149
Kind: "array",
109150
Type: &Type{
110151
Kind: "struct",
111-
Name: "Tweet",
152+
Name: "docgen_test.Tweet",
112153
},
113154
},
114155
"Config": {
@@ -127,7 +168,7 @@ func TestCreateDoc_FromMap(t *testing.T) {
127168
},
128169
},
129170
Types: map[TypeName]*Type{
130-
"Tweet": {
171+
"docgen_test.Tweet": {
131172
Kind: "struct",
132173
Fields: map[Identifier]*Type{
133174
"Size": {Kind: "int"},

0 commit comments

Comments
 (0)