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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions file/readfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,18 @@ func toFloat(key string) (float64, error) {
return strconv.ParseFloat(key, 64)
}

func indent(spaces int, v string) string {
pad := strings.Repeat(" ", spaces)
return strings.Replace(v, "\n", "\n"+pad, -1)
}

func renderTemplate(content string) (string, error) {
t := template.New("state").Funcs(template.FuncMap{
"env": getPrefixedEnvVar,
"toBool": toBool,
"toInt": toInt,
"toFloat": toFloat,
"indent": indent,
}).Delims("${{", "}}")
t, err := t.Parse(content)
if err != nil {
Expand Down
41 changes: 40 additions & 1 deletion file/readfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ func Test_getContent(t *testing.T) {
args: args{[]string{"testdata/file.yaml"}},
envVars: map[string]string{
"DECK_SVC2_HOST": "2.example.com",
"DECK_FILE_LOG_FUNCTION": `
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))`,
},
want: &Content{
Services: []FService{
Expand All @@ -197,6 +208,25 @@ func Test_getContent(t *testing.T) {
Name: kong.String("prometheus"),
},
},
{
Plugin: kong.Plugin{
Name: kong.String("pre-function"),
Config: kong.Configuration{
"log": `
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))
`,
},
},
},
},
},
wantErr: false,
Expand All @@ -220,7 +250,8 @@ func Test_getContent(t *testing.T) {
name: "multiple files",
args: args{[]string{"testdata/file.yaml", "testdata/file.json"}},
envVars: map[string]string{
"DECK_SVC2_HOST": "2.example.com",
"DECK_SVC2_HOST": "2.example.com",
"DECK_FILE_LOG_FUNCTION": "kong.log.set_serialize_value('trace_id', 1))",
},
want: &Content{
Services: []FService{
Expand All @@ -246,6 +277,14 @@ func Test_getContent(t *testing.T) {
Name: kong.String("prometheus"),
},
},
{
Plugin: kong.Plugin{
Name: kong.String("pre-function"),
Config: kong.Configuration{
"log": "kong.log.set_serialize_value('trace_id', 1))\n",
},
},
},
},
Consumers: []FConsumer{
{
Expand Down
4 changes: 4 additions & 0 deletions file/testdata/file.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ services:
- '<' # verifies that the templating engine does not perform character escaping
plugins:
- name: prometheus
- name: pre-function
config:
log: |
${{ env "DECK_FILE_LOG_FUNCTION" | indent 8 }}