Skip to content

Add lua includes #10185

Open
Open
@allewell

Description

@allewell

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions