Skip to content

Commit

Permalink
Merge pull request #24 from afmahmuda/generics
Browse files Browse the repository at this point in the history
parse *ast.IndexExpr for generics
  • Loading branch information
bykof authored Nov 20, 2022
2 parents ea5f3b0 + 1b7f1c5 commit c48f903
Show file tree
Hide file tree
Showing 9 changed files with 538 additions and 55 deletions.
17 changes: 17 additions & 0 deletions astParser/mappers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package astParser

import (
"fmt"
"go/ast"

"github.com/bykof/go-plantuml/domain"
Expand Down Expand Up @@ -99,6 +100,22 @@ func structTypeToField(fieldName string, structType *ast.StructType) domain.Fiel
Type: "interface{}",
}
}
func indexExprToField(fieldName string, indexExpr *ast.IndexExpr) (domain.Field, error) {
innerType, err := exprToField("", indexExpr.Index)
if err != nil {
return domain.Field{}, err
}

outerType, err := exprToField("", indexExpr.X)
if err != nil {
return domain.Field{}, err
}

return domain.Field{
Name: fieldName,
Type: domain.Type(fmt.Sprintf("%s[%s]", outerType.Type, innerType.Type)),
}, nil
}

func chanTypeToField(fieldName string, chanType *ast.ChanType) domain.Field {
var valueFieldType string
Expand Down
14 changes: 14 additions & 0 deletions astParser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ func ParseFile(filePath string) domain.Package {
case ast.Typ:
typeSpec := object.Decl.(*ast.TypeSpec)

if typeSpec.TypeParams != nil {
var params []string
for _, v := range typeSpec.TypeParams.List {
for _, v := range v.Names {
params = append(params, v.String())
}
}
if len(params) > 0 {
name = name + "[" + strings.Join(params, ",") + "]"
}
}
switch typeSpec.Type.(type) {
case *ast.StructType:
structType := typeSpec.Type.(*ast.StructType)
Expand Down Expand Up @@ -241,6 +252,9 @@ func exprToField(fieldName string, expr ast.Expr) (*domain.Field, error) {
case *ast.ChanType:
field := chanTypeToField(fieldName, fieldType)
return &field, nil
case *ast.IndexExpr: // generic goes here
field, err := indexExprToField(fieldName, fieldType)
return &field, err
default:
return nil, fmt.Errorf("unknown Field Type %s", reflect.TypeOf(expr).String())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
options = append(options, astParser.WithRecursive())
}

if exclusion!=""{
if exclusion != "" {
options = append(options, astParser.WithFileExclusion(exclusion))
}

Expand Down
6 changes: 6 additions & 0 deletions domain/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ type (
)

func (domainInterface Interface) IsImplementedByClass(class Class) bool {

// no functions most probably due this being not an interface but a constrains
if len(domainInterface.Functions) == 0 {
return false
}

for _, interfaceFunction := range domainInterface.Functions {
var interfaceFunctionIsImplemented = false
for _, classFunction := range class.Functions {
Expand Down
4 changes: 2 additions & 2 deletions formatter/plantUml.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func FormatPlantUML(packages domain.Packages) string {
}

func FormatRelation(class domain.Class, class2 domain.Class) string {
return fmt.Sprintf("%s --> %s", class.Name, class2.Name)
return fmt.Sprintf(`"%s" --> "%s"`, class.Name, class2.Name)
}

func FormatRelations(classes domain.Classes) string {
Expand All @@ -221,7 +221,7 @@ func FormatRelations(classes domain.Classes) string {
}

func FormatImplementationRelation(class domain.Class, domainInterface domain.Interface) string {
return fmt.Sprintf("%s --|> %s", class.Name, domainInterface.Name)
return fmt.Sprintf(`"%s" --|> "%s"`, class.Name, domainInterface.Name)
}

func FormatImplementationRelations(classes domain.Classes, domainInterfaces domain.Interfaces) string {
Expand Down
31 changes: 16 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
module github.com/bykof/go-plantuml

go 1.18
go 1.19

require (
github.com/magiconair/properties v1.8.6
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.10.1
github.com/stretchr/testify v1.7.1
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit c48f903

Please sign in to comment.