terrastruct / d2

D2 is a modern diagram scripting language that turns text to diagrams.
https://d2lang.com
Mozilla Public License 2.0
16.49k stars 413 forks source link

Feature Request: generate and connect N objects programmatically without needing to explicity declare each one #1284

Closed HariSekhon closed 1 year ago

HariSekhon commented 1 year ago

Is it possible to generate objects and connections programmatically instead of having to explicitly declare each one?

Compare these two pieces of code from D2 vs Python diagrams module:

for _ in range(15):
    opentsdb[_] = Custom(f"OpenTSDB pod {_}", opentsdb_icon)
classes: {
  opentsdb: {
    icon: ./images/opentsdb.png
  }
}

tsdb1: OpenTSDB pod1 {
  class: opentsdb
}
tsdb2: OpenTSDB pod2 {
  class: opentsdb
}
tsdb3: OpenTSDB pod3 {
  class: opentsdb
}
tsdb4: OpenTSDB pod4 {
  class: opentsdb
}
tsdb5: OpenTSDB pod5 {
  class: opentsdb
}
tsdb6: OpenTSDB pod6 {
  class: opentsdb
}
tsdb7: OpenTSDB pod7 {
  class: opentsdb
}
tsdb8: OpenTSDB pod8 {
  class: opentsdb
}
tsdb9: OpenTSDB pod9 {
  class: opentsdb
}
tsdb10: OpenTSDB pod10 {
  class: opentsdb
}
tsdb11: OpenTSDB pod11 {
  class: opentsdb
}
tsdb12: OpenTSDB pod12 {
  class: opentsdb
}
tsdb13: OpenTSDB pod13 {
  class: opentsdb
}
tsdb14: OpenTSDB pod14 {
  class: opentsdb
}
tsdb15: OpenTSDB pod15 {
  class: opentsdb
}

It then gets worse... because I then add 12 HBase nodes which underpin the OpenTSDB cluster...

... and then want a cartesian production of connections between each of the 15 OpenTSDB nodes vs the 12 HBase nodes... so 180 connections - programmatically in Python that's just a nested for loop iteration, but I can't see how to do this efficiently in D2.

alixander commented 1 year ago

D2 is a library too, so you can do that in Go (which has loops), and that'd be the equivalent of Python's Diagrams. See this post for more: https://terrastruct.com/blog/post/generate-diagrams-programmatically/

With text, there's no loops of course, as in all declarative languages. I think this feature would help though (once implemented): https://github.com/terrastruct/d2/issues/742