Skip to content
This repository has been archived by the owner on Jan 21, 2023. It is now read-only.

Syntactic sugar around relationship creation #28

Open
1 task done
yt-ms opened this issue Sep 27, 2020 · 1 comment
Open
1 task done

Syntactic sugar around relationship creation #28

yt-ms opened this issue Sep 27, 2020 · 1 comment

Comments

@yt-ms
Copy link
Collaborator

yt-ms commented Sep 27, 2020

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)

@Midnighter
Copy link
Owner

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")

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants