Skip to content

Add lua includes #10185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
allewell opened this issue Apr 9, 2025 · 0 comments
Open

Add lua includes #10185

allewell opened this issue Apr 9, 2025 · 0 comments

Comments

@allewell
Copy link

allewell commented Apr 9, 2025

Is your feature request related to a problem? Please describe.
Lua filters call an entrypoint function either in-line (code) or from a file (script). Code within the entrypoint function can call other functions within the same block/file. However, those other functions are not sharable across different filters:

Inline example:

pipeline:
  filters:
    # WORKS
    - name: lua
      match: appxml
      call: entrypoint
      code: |
        function shared_function(someInput)
            -- Do something
        end
        function entrypoint(tag, timestamp, record)
            bleep = shared_function(bloop)
        end
    # DOES NOT WORK
    - name: lua
      match: appxml
      call: other_entrypoint
      code: |
        function other_entrypoint(tag, timestamp, record)
            bleep = shared_function(bloop)
            -- ^ FAILS! `attempt to call global 'shared_function' (a nil value)`
        end

For maintainability purposes, it can be desirable to define lua functions that get re-used in a single location, rather than duplicated across multiple filters.

Describe the solution you'd like
Provide the ability to include lua code in a more global context that can be accessed by multiple different filters. Perhaps by including all functions from a lua file:

includes.lua:

function shared_function(someInput)
    -- Do something
end

pipeline:

  filters:
    - name: lua
      match: appxml
      include: includes.lua
      call: entrypoint
      code: |
        function entrypoint(tag, timestamp, record)
            blah = shared_function(something)
            -- some things
        end

  filters:
    - name: lua
      match: appxml
      include: includes.lua
      call: do_something_else
      code: |
        function do_something_else(tag, timestamp, record)
            blah = shared_function(something)
            -- do some other things
        end

Describe alternatives you've considered
It is possible although somewhat dubious to define lua code within a variable, then re-use that variable within individual lua filters:

env:
  SOME_SHARED_FUNCTION: |
    function shared_function(input)
        -- do something
     end

pipeline:
  filters:
    - name: lua
      match: appxml
      call: entrypoint
      code: |
        function entrypoint(someInput)
            ${SOME_SHARED_FUNCTION}
            blah = shared_function(something)
            -- do some other things
        end

Additional context
While code can currently be placed inside an external .lua file and called in the filter via script from multiple filters, no custom input can be provided to the entrypoint function in each filter, it only taking (tag, timestamp, record). Providing 'includes' seems to be a versatile solution, since any custom input could be defined in the filter's code block, the shared code being loaded from the external file mentioned inincludes

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

No branches or pull requests

1 participant