Skip to content
PlantUML Class Diagram Generator for golang projects
Go
Branch: master
Clone or download
jfeliu007 Merge pull request #27 from jfeliu007/issue-25
Fixes #25 Supports pointers of builtin when checked as strings to be …
Latest commit e97387d Jul 16, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
cmd/goplantuml Fixes #2 Jun 5, 2019
example Fixes #19 Jul 15, 2019
parser Fixes #25 Supports pointers of builtin when checked as strings to be … Jul 16, 2019
testingsupport Added support testing for the new functionality Jul 16, 2019
.gitignore Initial commit May 26, 2019
.travis.yml Fixes #11 Jul 9, 2019
ClassDiagram.puml Updated the class diagram Jul 9, 2019
LICENSE Initial commit May 26, 2019
README.md Update README.md Jul 15, 2019

README.md

godoc reference Go Report Card codecov License: MIT GitHub release Build Status

GoPlantUML

PlantUML Class Diagram Generator for golang projects. Generates class diagram text compatible with plantuml with the information of all structures and interfaces as well as the relationship among them.

Prerequisites

golang 1.10 or above

Installing

go get github.com/jfeliu007/goplantuml/parser
go get github.com/jfeliu007/goplantuml/cmd/goplantuml
cd $GOPATH/src/github.com/jfeliu007/goplantuml
go install ./...

This will install the command goplantuml in your GOPATH bin folder.

Usage

goplantuml [-recursive] path/to/gofiles
goplantuml [-recursive] path/to/gofiles > diagram_file_name.puml

Example

goplantuml $GOPATH/src/github.com/jfeliu007/goplantuml/parser
// echoes

@startuml
namespace parser {
    class Struct {
        + Functions []*Function
        + Fields []*Parameter
        + Type string
        + Composition []string
        + Extends []string

    }
    class LineStringBuilder {
        + WriteLineWithDepth(depth int, str string) 

    }
    class ClassParser {
        - structure <font color=blue>map</font>[string]<font color=blue>map</font>[string]*Struct
        - currentPackageName string
        - allInterfaces <font color=blue>map</font>[string]<font color=blue>struct</font>{}
        - allStructs <font color=blue>map</font>[string]<font color=blue>struct</font>{}

        - structImplementsInterface(st *Struct, inter *Struct) 
        - parsePackage(node ast.Node) 
        - parseFileDeclarations(node ast.Decl) 
        - addMethodToStruct(s *Struct, method *ast.Field) 
        - getFunction(f *ast.FuncType, name string) 
        - addFieldToStruct(s *Struct, field *ast.Field) 
        - addToComposition(s *Struct, fType string) 
        - addToExtends(s *Struct, fType string) 
        - getOrCreateStruct(name string) 
        - getStruct(structName string) 
        - getFieldType(exp ast.Expr, includePackageName bool) 

        + Render() 

    }
    class Parameter {
        + Name string
        + Type string

    }
    class Function {
        + Name string
        + Parameters []*Parameter
        + ReturnValues []string

    }
}
strings.Builder *-- parser.LineStringBuilder


@enduml
goplantuml $GOPATH/src/github.com/jfeliu007/goplantuml/parser > ClassDiagram.puml
// Generates a file ClassDiagram.plum with the previous specifications

There are two different relationships considered in goplantuml:

  • Interface implementation
  • Type Composition

The following example contains interface implementations and composition. Notice how the signature of the functions

package testingsupport

//MyInterface only has one method, notice the signature return value
type MyInterface interface {
	foo() bool
}

//MyStruct1 will implement the foo() bool function so it will have an "extends" association with MyInterface
type MyStruct1 struct {
}

func (s1 *MyStruct1) foo() bool {
	return true
}

//MyStruct2 will be direclty composed of MyStruct1 so it will have a composition relationship with it
type MyStruct2 struct {
	MyStruct1
}

//MyStruct3 will have a foo() function but the return value is not a bool, so it will not have any relationship with MyInterface
type MyStruct3 struct {
}

func (s3 *MyStruct3) foo() {

}

This will be generated from the previous code

@startuml
namespace testingsupport {
    interface MyInterface  {
        - foo() bool

    }
    class MyStruct1 << (S,Aquamarine) >> {
        - foo() bool

    }
    class MyStruct2 << (S,Aquamarine) >> {
    }
    class MyStruct3 << (S,Aquamarine) >> {
        - foo() 

    }
}
testingsupport.MyStruct1 *-- testingsupport.MyStruct2

testingsupport.MyInterface <|-- testingsupport.MyStruct1

@enduml

alt text

The following diagram is generated based on the file in https://raw.githubusercontent.com/jfeliu007/goplantuml/master/ClassDiagram.puml

Alt text

You can’t perform that action at this time.