Skip to content

Commit

Permalink
Merge pull request #213 from movableink/uri-decode
Browse files Browse the repository at this point in the history
Add urid format string to decode URI values
  • Loading branch information
itchyny authored May 28, 2023
2 parents 36b374f + dbba5e7 commit 850177b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5906,6 +5906,22 @@
expected: |
"q=%5B%22%3C%3E%22%2C%22%7B%26%7D%22%5D&q=%5B%22%3C%3E%22%2C%22%7B%26%7D%22%2C%22%3C%3E%22%2C%22%7B%26%7D%22%5D"
- name: format strings @urid
args:
- '@urid'
input: |
"%5B1%2C%7B%22foo%22%3A%22%3Cdiv%3E%26%27%5C%22%28%29%3C%2Fdiv%3E%22%7D%5D"
expected: |
"[1,{\"foo\":\"<div>&'\\\"()</div>\"}]"
- name: format strings @urid error
args:
- '@urid'
input: |
"%5B1%2C%"
error: |
@urid cannot be applied: invalid URL escape "%"
- name: format strings @csv
args:
- '@csv'
Expand Down
2 changes: 2 additions & 0 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,8 @@ func formatToFunc(format string) *Func {
return &Func{Name: "_tohtml"}
case "@uri":
return &Func{Name: "_touri"}
case "@urid":
return &Func{Name: "_tourid"}
case "@csv":
return &Func{Name: "_tocsv"}
case "@tsv":
Expand Down
14 changes: 14 additions & 0 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func init() {
"format": argFunc1(funcFormat),
"_tohtml": argFunc0(funcToHTML),
"_touri": argFunc0(funcToURI),
"_tourid": argFunc0(funcToURId),
"_tocsv": argFunc0(funcToCSV),
"_totsv": argFunc0(funcToTSV),
"_tosh": argFunc0(funcToSh),
Expand Down Expand Up @@ -836,6 +837,19 @@ func funcToURI(v any) any {
}
}

func funcToURId(v any) any {
switch x := funcToString(v).(type) {
case string:
escaped, err := url.QueryUnescape(x)
if err != nil {
return fmt.Errorf("@urid cannot be applied: %s", err.Error())
}
return string(escaped)
default:
return x
}
}

var csvEscaper = strings.NewReplacer(
`"`, `""`,
"\x00", `\0`,
Expand Down

0 comments on commit 850177b

Please sign in to comment.