Skip to content

Commit

Permalink
feat: add indent function to support multi-line content (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
aparra committed Jun 6, 2023
1 parent f33f7e6 commit cda8acb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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 }}

0 comments on commit cda8acb

Please sign in to comment.