jfeliu007 / goplantuml

PlantUML Class Diagram Generator for golang projects
MIT License
1.78k stars 167 forks source link

Composition doesn't work #86

Closed nkcr closed 4 years ago

nkcr commented 4 years ago

If I try to run the example in the README.md, the composition with MyStruct3 is not rendered (using go v1.12.6):

XL713e8m3BtlAte45HFnkX2yc7ZWn1TC78O4rhbr1wBykymOqG3IKzFszRtNffQ48TVKJ8b6MYqA2IGjLUfgdTPe2EuC-YXgOIraRKx65RG3pY78DuUR4uqmbP8X9Cbxr4S49M8GmXcnPgzgDx4c_hTc2h1Vubt34N6GoKQ2liLKYgGflUMiFNto1HST-xtFThZ9AefWfXcpCMoWv8zvPEyXbUhYN_G4

How to reproduce

save the following to a file:

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 {
    Foo MyStruct1
}

func (s3 *MyStruct3) foo() {

}

Then generate the plantuml:

goplantuml ./

The diagram I get is the following:

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

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

    }
    class MyStruct2 << (S,Aquamarine) >> {
    }
    class MyStruct3 << (S,Aquamarine) >> {
        + Foo MyStruct1

        - foo() 

    }
}
testingsupport.MyStruct1 *-- testingsupport.MyStruct2

testingsupport.MyInterface <|-- testingsupport.MyStruct1

@enduml
jfeliu007 commented 4 years ago

Hi, thanks for your comment. I believe the one piece you are missing is aggregation. It could be kind of confusing, but for the context of this app, Composition means type Embedding composition. In other words you will need to embed one structure into another to see the composition link. In the same way MyStruct2 includes MyStruct1

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

In the case of this code

//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 {
    Foo MyStruct1
}

Foo is a field of MyStruct3. That kind of representation, although it is a composition, it is done through an aggregation link. Aggregations are optional in the command line interface. They are only shown if you include the -show-aggregations flag in the command line call. Have you tried that?

nkcr commented 4 years ago

Ok, I finally made it work with the -show-aggregations flag. This was something I already tried before but apparently the go get github.com/jfeliu007.. commands are not fetching the lastest version, so I was using an outdated one. I had to manually download the release and build it on order to have the latest version and make aggregation work.

jfeliu007 commented 4 years ago

I’ll take a look at that to see what’s happening. Thanks. 😁

BigBoulard commented 4 years ago

Same issue here with GoPlanUML v1.5 and the show-aggregations option.

(Simplified main.go)

package main

// HtmlElement
type HtmlElement struct {
    name, text string
    elements   []HtmlElement
}
type HtmlBuilder struct {
    rootName string
    root     HtmlElement
}

Puml

@startuml
namespace main {
    class HtmlBuilder << (S,Aquamarine) >> {
        - rootName string
        - root HtmlElement

        + String() string
        + AddChildFluent(childName string, childText string) *HtmlBuilder
        + Clear() 

    }
    class HtmlElement << (S,Aquamarine) >> {
        - name string
        - elements []HtmlElement

        - string(indent int) string

        + String() string

    }
}
@enduml

png

I just have to say that even if composition and aggregation may look one and the same, the big difference is that if a composed object is destroyed, then the composing object will be as well. This is not the case of an Aggregation. So, embedded fields (unnamed), named field (by struct, interface, or a pointer to a struct) can all be either part of a composition or an aggregation depending on other relationships where a field would be involved too. From my perspective, it's an important distinction to make because a nice diagram may help to focus our search when there is a memory leak in the app for example. Aside from that, it's working pretty well and I really appreciate the effort :)

jfeliu007 commented 4 years ago

@BigBoulard private aggregations are not rendered unless explicitly ask for it with the -aggregate-private-members options.

Try goplantuml -show-aggregations -aggregate-private-members ./

This is the result I get with the code you presented

@startuml
namespace main {
    class HtmlBuilder << (S,Aquamarine) >> {
        - rootName string
        - root HtmlElement

    }
    class HtmlElement << (S,Aquamarine) >> {
        - name string
        - elements []HtmlElement

    }
}

"main.HtmlBuilder" o-- "main.HtmlElement"
"main.HtmlElement" o-- "main.HtmlElement"

@enduml

The project makes a distinction between aggregations and composition even at the visual level. :)

try this code

package main

import "os"

// HtmlElement
type HtmlElement struct {
    name, text string
    elements   []HtmlElement
    os.File
}
type HtmlBuilder struct {
    rootName string
    root     HtmlElement
}

You will see a difference in how the connections are rendered. os.File will be rendered as a composition with a filled arrow.

jfeliu007 commented 4 years ago

@nkcr , @BigBoulard is this issue still relevant?