Skip to content

Commit

Permalink
feat: Implemented the URLDecode() function.
Browse files Browse the repository at this point in the history
  • Loading branch information
skyzyx committed Nov 10, 2024
1 parent 677fa27 commit a91d530
Show file tree
Hide file tree
Showing 9 changed files with 404 additions and 279 deletions.
14 changes: 14 additions & 0 deletions .lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ pre-commit:
# git diff-index --check "$(git hash-object -t tree /dev/null)"
# stage_fixed: true

gofmt:
tags: "always,go,formatting"
glob: "**/*.go"
run: >-
gofmt -w -s -r 'interface{} -> any' -r 'a[b:len(a)] -> a[b:]' {staged_files}
stage_fixed: true

gofumpt:
tags: "always,go,formatting"
glob: "**/*.go"
run: >-
gofumpt -w -e {staged_files}
stage_fixed: true

markdownlint:
tags: "always,docs,formatting"
glob: "**/*.md"
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ install-tools-go:
$(GO) install golang.org/x/tools/cmd/godoc@latest
$(GO) install golang.org/x/vuln/cmd/govulncheck@latest
$(GO) install gotest.tools/gotestsum@latest
$(GO) install mvdan.cc/gofumpt@latest

.PHONY: install-tools-mac
## install-tools-mac: [tools]* Install/upgrade the required tools for macOS, including Go packages.
Expand Down
19 changes: 19 additions & 0 deletions corefunc/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package corefunc

import (
"fmt"
neturl "net/url"

"github.com/nlnwa/whatwg-url/canonicalizer"
"github.com/nlnwa/whatwg-url/url"

Expand Down Expand Up @@ -57,3 +60,19 @@ func URLParse(rawURL string, canon ...types.URLCanonicalizer) (*url.Url, error)
// Default
return url.Parse(rawURL) // lint:allow_unwrapped_errors
}

/*
URLDecode decodes a URL-encoded string.
----
- s (string): An encoded URL.
*/
func URLDecode(s string) (string, error) {
q, err := neturl.QueryUnescape(s)
if err != nil {
return "", fmt.Errorf("failed to decode URL '%s': %w", q, err)
}

return q, nil
}
29 changes: 29 additions & 0 deletions corefunc/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ func ExampleURLParse_googleSafeBrowsing() {
// .80
}

func ExampleURLDecode() {
output, err := URLDecode("hello%20%E4%B8%96%E7%95%8C")
if err != nil {
panic(err)
}

fmt.Println(output)

// Output:
// hello 世界
}

func TestURLParse(t *testing.T) { // lint:allow_complexity
for name, tc := range testfixtures.URLParseTestTable {
t.Run(name, func(t *testing.T) {
Expand Down Expand Up @@ -176,3 +188,20 @@ func TestURLParse(t *testing.T) { // lint:allow_complexity
})
}
}

func TestURLDecode(t *testing.T) { // lint:allow_complexity
for name, tc := range testfixtures.URLDecodeTestTable {
t.Run(name, func(t *testing.T) {
output, err := URLDecode(tc.Input)

// We expect an error.
if err != nil && !tc.ExpectedErr {
t.Errorf("Unexpected error: %v", err)
}

if output != tc.Expected {
t.Errorf("Expected %s, got %s", tc.Expected, output)
}
})
}
}
2 changes: 1 addition & 1 deletion terratest/functions/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var (
// Both must be installed first.
binaries = []string{
"terraform", // 1.8.0+
"tofu", // 1.7.0+
"tofu", // 1.7.0+
}
)

Expand Down
Loading

0 comments on commit a91d530

Please sign in to comment.