terrastruct / d2

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

Doing weird things with D2 #1841

Open zekenie opened 4 months ago

zekenie commented 4 months ago

I have some ideas for slightly unconventional use cases for D2 and I'm looking for feedback. For example, I think D2 could make a quite AI-enabled prototyping environment for a sort of single-file http server.

Imagine the following D2 example

image
http: {
  users: {
    sign_up
  }

  tasks: {
    create
    assign
    complete
    defer
  }
}

# creates a user
http.users.sign_up -> users

# ...
http.tasks.* -> tasks

tasks: {
  shape: sql_table
  id: int {constraint: primary_key}
  last_updated: timestamp with time zone
  assignee_id: int {constraint: foreign_key}
  state: string
}

users: {
  shape: sql_table
  id: int {constraint: primary_key}
  name: timestamp with time zone
  email: int {constraint: foreign_key}
}

tasks -> users

Now imagine you could run

$ some-magic ./this-file.d2 --port 8080

and have a fully running HTTP server, backed by sqlite or something, that would do more or less what your diagram described. If you add in the possibility that you could make comments processed by gen-ai, it could get pretty interesting.

I'm interested in hacking on this on a weekend project some time in the next few months. What's getting in the way for me is understanding how I can parse D2. I don't want to write my own parser. I'm not a go expert. I'd love some help understanding the prior art and how feasible this idea is.

andrewh commented 4 months ago

I'm not on this project but a prerequisite is probably understanding a bit more about the D2 parser :) If that means learning a bit more Go, that's an entry point.

Disregarding gen-ai for now, maybe a good short term goal is to compile D2 into a target that runs the services you want? You can do that by any quick and dirty means you like, you don't necessarily need to use the Go parser.

I like the idea, it just needs a bit more legwork.

zekenie commented 4 months ago

I've been thinking about it and i think if i can understand a json structure I could get out of the parser, then, i could do a lot of the work and treat the go/parser part as a solvable problem.

I also was thinking about it and the d2 spec doesn't quite have enough information. It's so close, though. For example with the database tables, i can't tell which column points to which table. I know that two tables are related, and I know that a column is a foreign key, but i don't know which col points to which table. i might be able to derive it from the name in some cases....

I almost wonder if I should treat D2 as an inspiration for a diagrammable prototype DSL, instead of thinking of it as the literal DSL i want. Like maybe i make a new DSL that can compile to D2 but that I have more control over.

andrewh commented 4 months ago

Which spec are you looking at? I had a quick search and couldn't find it.

Since this project has goals around extensibility, a JSON representation from the parser doesn't seem too far-fetched.

Happy to help as I'm keen to learn more about D2.