structurizr-python icon indicating copy to clipboard operation
structurizr-python copied to clipboard

Syntactic sugar around relationship creation

Open yt-ms opened this issue 5 years ago • 1 comments

Checklist

Is your feature related to a problem? Please describe it.

Currently, to add a relationship between two elements is a little clunky (mirroring the Java API):

c1 = Container("Container 1")
c2 = Container("Container 2")
r = c1.add_relationship(destination=c2, description="Sends events to")

I think we can do better in Python.

Describe the solution you would like.

By overriding __rshift__ and __irshift__ on Element then we can achieve something much cleaner:

c1 = Container("Container 1")
c2 = Container("Container 2")
c1 >> "Sends events to" >> c2

The result of this expression would be the Relationship so you could continue to add technologies, tags, etc. We could also provide a shortcut that creates a general relationship with description "Uses":

c1 >> c2

And we should also support constructing the relationship explicitly:

c1 >> Relationship("Sends events to", technologies="Kafka") >> c2

This becomes even more useful if people choose to subtype Relationship:

c1 >> Kafka("Sends events to", topic="eventStream") >> c2

(here the topic would be added to the properties collection of the Relationship)

yt-ms avatar Sep 27 '20 10:09 yt-ms

Yes, I must admit that I liked the syntax when I saw it at https://diagrams.mingrammer.com/ so I'm all for it!

I would only change one thing, I think, if you instantiate the class, it should not be done in a statement involving shift but involve all arguments, i.e., instead of

c1 >> Relationship("Sends events to", technologies="Kafka") >> c2

I propose

Relationship(source=c1, destination=c2, description="Sends events to", technologies="Kafka")

Midnighter avatar Sep 27 '20 13:09 Midnighter