Skip to content

Commit

Permalink
docgen: add package name to types
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakoth authored and antonmedv committed Apr 23, 2020
1 parent d676309 commit 8d1f4e8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
24 changes: 19 additions & 5 deletions docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type TypeName string
type Context struct {
Variables map[Identifier]*Type `json:"variables"`
Types map[TypeName]*Type `json:"types"`
pkgPath string
}

type Type struct {
Expand Down Expand Up @@ -52,6 +53,7 @@ func CreateDoc(i interface{}) *Context {
c := &Context{
Variables: make(map[Identifier]*Type),
Types: make(map[TypeName]*Type),
pkgPath: dereference(reflect.TypeOf(i)).PkgPath(),
}

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

for t.Kind() == reflect.Ptr {
t = t.Elem()
}
t = dereference(t)

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

appendix:
name := TypeName(t.Name())
anonymous := name == ""

name := TypeName(t.String())
if c.pkgPath == t.PkgPath() {
name = TypeName(t.Name())
}
anonymous := t.Name() == ""

a, ok := c.Types[name]

Expand Down Expand Up @@ -219,3 +223,13 @@ func isPrivate(s string) bool {
func isProtobuf(s string) bool {
return strings.HasPrefix(s, "XXX_")
}

func dereference(t reflect.Type) reflect.Type {
if t == nil {
return nil
}
if t.Kind() == reflect.Ptr {
t = dereference(t.Elem())
}
return t
}
53 changes: 47 additions & 6 deletions docgen/docgen_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package docgen_test

import (
"github.com/stretchr/testify/require"
"math"
"testing"

. "github.com/antonmedv/expr/docgen"
"github.com/sanity-io/litter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math"
"testing"
"time"
)

type Tweet struct {
Expand All @@ -21,6 +21,15 @@ type Env struct {
MaxSize int32
}
Env map[string]interface{}
// NOTE: conflicting type name
TimeWeekday time.Weekday
Weekday Weekday
}

type Weekday int

func (Weekday) String() string {
return ""
}

type Duration int
Expand Down Expand Up @@ -64,6 +73,14 @@ func TestCreateDoc(t *testing.T) {
},
Return: &Type{Kind: "struct", Name: "Duration"},
},
"TimeWeekday": {
Name: "time.Weekday",
Kind: "struct",
},
"Weekday": {
Name: "Weekday",
Kind: "struct",
},
},
Types: map[TypeName]*Type{
"Tweet": {
Expand All @@ -85,6 +102,30 @@ func TestCreateDoc(t *testing.T) {
},
},
},
"time.Weekday": {
Kind: "struct",
Fields: map[Identifier]*Type{
"String": {
Kind: "func",
Arguments: []*Type{},
Return: &Type{
Kind: "string",
},
},
},
},
"Weekday": {
Kind: "struct",
Fields: map[Identifier]*Type{
"String": {
Kind: "func",
Arguments: []*Type{},
Return: &Type{
Kind: "string",
},
},
},
},
},
}

Expand All @@ -108,7 +149,7 @@ func TestCreateDoc_FromMap(t *testing.T) {
Kind: "array",
Type: &Type{
Kind: "struct",
Name: "Tweet",
Name: "docgen_test.Tweet",
},
},
"Config": {
Expand All @@ -127,7 +168,7 @@ func TestCreateDoc_FromMap(t *testing.T) {
},
},
Types: map[TypeName]*Type{
"Tweet": {
"docgen_test.Tweet": {
Kind: "struct",
Fields: map[Identifier]*Type{
"Size": {Kind: "int"},
Expand Down

0 comments on commit 8d1f4e8

Please sign in to comment.