Skip to content
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

feat: Add indent function to support multi-line content #929

Merged
merged 1 commit into from
Jun 6, 2023

Conversation

aparra
Copy link
Contributor

@aparra aparra commented Jun 2, 2023

We want to extract Lua code from kong.yaml file for two reasons:

  1. Better syntax highlight in the IDE for .lua files
  2. Write unit tests for the functions

So, we would like to have a setup like that:

kong.yaml

---
_format_version: "3.0"
plugins:
  - name: pre-function
    config:
      log: |
        ${{ env "DECK_FILE_LOG_FUNCTION" | indent 8 }}

file_log.lua

function parse_traceid(str)str = string.sub(str,1,8)
  local uint = 0
  for i = 1, #str do
    uint = uint + str:byte(i) * 0x100^(i-1)
  end
  return string.format("%.0f", uint)
end

kong.log.set_serialize_value("trace_id", parse_traceid(ngx.ctx.KONG_SPANS[1].trace_id))
kong.log.set_serialize_value("span_id", parse_traceid(ngx.ctx.KONG_SPANS[1].span_id))

However, without the indent function, it is not possible to convert the yaml to json. It fails due indentation required by yaml. This issue #616 worked around this problem, replacing yaml with json, where you have better control of quotes.

But we would like consistency in the setup; it means using yaml everywhere. Inspired by k8s helm, where function indent is available https://helm.sh/docs/chart_template_guide/function_list/#indent to deal with multi-line block, I added the indent function to deck.

@aparra aparra requested a review from a team as a code owner June 2, 2023 15:35
@CLAassistant
Copy link

CLAassistant commented Jun 2, 2023

CLA assistant check
All committers have signed the CLA.

@aboudreault
Copy link
Contributor

@aparra Thanks for this PR. tested quickly and it seems to work well. I will check to merge this one soon

@aboudreault aboudreault merged commit 4f6bd9a into Kong:main Jun 6, 2023
18 of 33 checks passed
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

Successfully merging this pull request may close these issues.

None yet

3 participants