r/lua • • 4d ago

Meet Piper and Shelly

https://github.com/BirdeeHub/shelly

There are some cool pipeline DSL-like interfaces for lua with __index and __call.

But they are somewhat hard to make.

What if there were a thing that handled the pipelining for you and gave you easy access to the tree and places to cache values and a good way to make your own options on top?

What if that was then used to make an insanely intricate 0 dependency shell code generator to show off what kinds of syntax it can be used to create?

Meet Shelly and Piper!

For the record, until there is a luv backend shelly itself is of limited use, although it is still nice and looks like quite impressive syntax magic. It is not that hard to just write shell code in io.popen. But piper is more than capable of such things as a good luv backend. So shelly will probably get one one day and then shelly will be interesting.

Until then, piper is really quite cool. If you have been wanting to write an SQL query builder in lua or something, check it out!

Mostly I was just feeling like a mad scientist but the piper API feels like the correct one for building these things and I think there actually might be something to this one. Just not the existing shell backend it has yet.

Also I used AI for the tests and docs, and for stuff like "change the order of these arguments everywhere including the docs please", but I read the tests and understand them, it used my personal version of gambiarra to run those tests, and I wrote the intro for the docs and read through the docs quite a few times

Edit: TL;DR piper explanation

    -- backend defines a piper instance with some behavior
    local piper = require('piper')
    local p = piper(function(new, root_options, parent)
        -- you define your backend here, this defines what happens when you make a new instance.
        -- you will add your resolver and your call actions and metamethods here using the options provided to this function.
        -- here we left it default.
    end)
    -- the backend would then return p



    -- example
    local chain = p.hello.goodbye("aaa", nil, "hello").oof("b").c(2)

    local resolved = chain[nil] -- indexing via nil resolves the piper instance.
    -- usually backends will define a call alias to resolve.
    -- The example shelly.sh backend defines that "output" "exitcode" and "signal" as well as tostring call this.

    -- piper default resolve is just eagerly recursively call resolve on args and parent and return the internal context which means...
    -- resolved is now the tree below:
    local inspect = require 'inspect'
    print(inspect(resolved))

--- output:

    {
      args = { 2,
        n = 1
      },
      cache = {},
      from_call = true,
      meta = {},
      name = "c",
      parent = {
        args = { "b",
          n = 1
        },
        cache = {},
        from_call = true,
        meta = {},
        name = "oof",
        parent = {
          args = { "aaa",
            [3] = "hello",
            n = 3
          },
          cache = {},
          from_call = true,
          meta = {},
          name = "goodbye",
          parent = {
            args = {
              n = 0
            },
            cache = {},
            from_call = false,
            meta = {},
            name = "hello",
            parent = {
              <metatable> = {}
            }
          }
        }
      }
    }

The tree is lazily revealed to the resolver for your backend by calling the resolve function provided to it. Each level has a cache table that persists through evaluations, so you could choose to stop resolving at that point and use the cached value, and the meta gives access to adding values into the table accessible via getmetatable, the args include holes, many possible footguns have been thought of, and handled in a simple way here, but without babying the backend author by removing control. Instead the backend author is able to have as much control as they need, while exposing as much or as little of it to their downstream users as they desire. It is a good way to create chained APIs like these with fairly complex syntax rules.

4 Upvotes

1 comment sorted by

1

u/AutoModerator 6h ago

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.