-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Match schema://host format from input data
- Loading branch information
Showing
6 changed files
with
113 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package extract | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
) | ||
|
||
func SimpleUrl(input string) (string, bool, error) { | ||
parsed, err := url.Parse(input) | ||
if err != nil { | ||
return "", false, err | ||
} | ||
if parsed.Scheme != "" { | ||
tSimpleUrl := fmt.Sprintf("%s://%s", parsed.Scheme, parsed.Host) | ||
return tSimpleUrl, true, nil | ||
} else { | ||
// 拼接协议进行解析 | ||
modifiedHost := fmt.Sprintf("http://%s", input) | ||
parsed, err := url.Parse(modifiedHost) | ||
if err != nil { | ||
return "", false, err | ||
} else { | ||
tSimpleUrl := fmt.Sprintf("%s", parsed.Hostname()) | ||
return tSimpleUrl, false, nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package extract | ||
|
||
import "testing" | ||
|
||
// Test cases for SimpleUrl | ||
func TestSimpleUrl(t *testing.T) { | ||
testCases := []struct { | ||
input string | ||
expected string | ||
hasScheme bool | ||
}{ | ||
{"http://example.com:8080", "http://example.com:8080", true}, | ||
{"http://example.com", "http://example.com", true}, | ||
{"https://example.com/path", "https://example.com", true}, | ||
{"ftp://example.com", "ftp://example.com", true}, | ||
{"example.com/123.php?a=1", "example.com", false}, | ||
{"example.com", "example.com", false}, | ||
{"http://", "http://", true}, // Invalid URL | ||
{"", "", false}, // Empty string | ||
} | ||
|
||
for _, tc := range testCases { | ||
result, hasScheme, err := SimpleUrl(tc.input) | ||
if err != nil { | ||
t.Errorf("input: %s, unexpected error: %v", tc.input, err) | ||
} | ||
if result != tc.expected { | ||
t.Errorf("input: %s, expected: %s, got: %s", tc.input, tc.expected, result) | ||
} | ||
if hasScheme != tc.hasScheme { | ||
t.Errorf("input: %s, expected hasScheme: %v, got: %v", tc.input, tc.hasScheme, hasScheme) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package uniqueutil | ||
|
||
func IsKeyUniq(key string, found map[string]struct{}) bool { | ||
if _, ok := found[key]; !ok { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ package vars | |
|
||
// VERSION number | ||
// 版本号 | ||
const VERSION string = "1.5.6" | ||
const VERSION string = "1.5.7" |