-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kos-v/new-feats
- Loading branch information
Showing
4 changed files
with
110 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,18 @@ go get github.com/kos-v/dsnparser | |
```go | ||
import "github.com/kos-v/dsnparser" | ||
|
||
dsn := dsnparser.Parse("mysql://user:[email protected]:3306/dbname?transport=tcp"); | ||
dsn.GetScheme() // string "mysql" | ||
dsn.GetUser() // string "user" | ||
dsn.GetPassword() // string "password" | ||
dsn.GetHost() // string "example.local" | ||
dsn.GetPort() // string "3306" | ||
dsn.GetPath() // string "dbname" | ||
dsn.GetParam("transport") // string "tcp" | ||
dsn.GetParams() // map[string]string [transport: tcp] | ||
dsn.HasParam("transport") // bool true | ||
dsn.GetRaw() // string "mysql://user:[email protected]:3306/dbname?transport=tcp" | ||
dsn := dsnparser.Parse("mysql://user:password@tcp(example.local:3306)/dbname?encoding=utf8mb4"); | ||
dsn.GetScheme() // string "mysql" | ||
dsn.GetUser() // string "user" | ||
dsn.GetPassword() // string "password" | ||
dsn.GetHost() // string "example.local" | ||
dsn.GetPort() // string "3306" | ||
dsn.GetPath() // string "dbname" | ||
dsn.GetParam("encoding") // string "utf8mb4" | ||
dsn.GetParams() // map[string]string [encoding: "utf8mb4"] | ||
dsn.GetTransport() // string "tcp" | ||
dsn.HasParam("encoding") // bool true | ||
dsn.GetRaw() // string "mysql://user:[email protected]:3306/dbname?encoding=utf8mb4" | ||
``` | ||
|
||
##### Examples: | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,6 +2,16 @@ package dsnparser | |
|
||
import "testing" | ||
|
||
func TestDSN_HasParam(t *testing.T) { | ||
result := Parse("mysql://user:[email protected]:3306/dbname?foo=foo val") | ||
if !result.HasParam("foo") { | ||
t.Errorf("Unexpected value. Must be true") | ||
} | ||
if result.HasParam("bar") { | ||
t.Errorf("Unexpected value. Must be false") | ||
} | ||
} | ||
|
||
func TestParse_Scheme(t *testing.T) { | ||
tests := []struct { | ||
dsn string | ||
|
@@ -11,7 +21,7 @@ func TestParse_Scheme(t *testing.T) { | |
{"mysql", ""}, | ||
{"://", ""}, | ||
{"mysql://", "mysql"}, | ||
{"mysql://user:[email protected]:3306/dbname?tblsprefix=fs_", "mysql"}, | ||
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "mysql"}, | ||
} | ||
|
||
for i, test := range tests { | ||
|
@@ -43,7 +53,7 @@ func TestParse_User(t *testing.T) { | |
{":password@", ""}, | ||
{"u@", "u"}, | ||
{":@", ""}, | ||
{"mysql://user:[email protected]:3306/dbname?tblsprefix=fs_", "user"}, | ||
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "user"}, | ||
{"mysql://[email protected]:3306/dbname?tblsprefix=fs_", "user"}, | ||
{"mysql://example.com:3306/dbname?tblsprefix=fs_", ""}, | ||
{"\\@@", "@"}, | ||
|
@@ -89,7 +99,7 @@ func TestParse_Password(t *testing.T) { | |
{":password@", "password"}, | ||
{":p@", "p"}, | ||
{":@", ""}, | ||
{"mysql://user:[email protected]:3306/dbname?tblsprefix=fs_", "password"}, | ||
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "password"}, | ||
{"mysql://[email protected]:3306/dbname?tblsprefix=fs_", ""}, | ||
{"mysql://example.com:3306/dbname?tblsprefix=fs_", ""}, | ||
{"\\@@", ""}, | ||
|
@@ -121,7 +131,9 @@ func TestParse_Host(t *testing.T) { | |
expected string | ||
}{ | ||
{"mysql://user:[email protected]:3306/dbname?tblsprefix=fs_", "example.com"}, | ||
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "example.com"}, | ||
{"mysql://user:[email protected]/dbname?tblsprefix=fs_", "example.com"}, | ||
{"mysql://user:password@tcp(example.com)/dbname?tblsprefix=fs_", "example.com"}, | ||
{"mysql://user:password@localhost/dbname?tblsprefix=fs_", "localhost"}, | ||
{"mysql://user:[email protected]/dbname?tblsprefix=fs_", "127.0.0.1"}, | ||
{"mysql://user:[email protected]", "example.com"}, | ||
|
@@ -163,11 +175,14 @@ func TestParse_Port(t *testing.T) { | |
expected string | ||
}{ | ||
{"mysql://user:[email protected]:3306/dbname?tblsprefix=fs_", "3306"}, | ||
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "3306"}, | ||
{"mysql://user:[email protected]:3306", "3306"}, | ||
{"mysql://user:password@tcp(example.com:3306)", "3306"}, | ||
{"mysql://user:[email protected]/dbname?tblsprefix=fs_", ""}, | ||
{"mysql://user:[email protected]:/dbname?tblsprefix=fs_", ""}, | ||
{"mysql://user:[email protected]:bad, but working/dbname?tblsprefix=fs_", "bad, but working"}, | ||
{"example.com:3306", "3306"}, | ||
{"tcp(example.com:3306)", "3306"}, | ||
{"example.com:", ""}, | ||
{"example.com", ""}, | ||
{"хост.лок:3306", "3306"}, | ||
|
@@ -187,6 +202,7 @@ func TestParse_Path(t *testing.T) { | |
expected string | ||
}{ | ||
{"mysql://user:[email protected]:3306/foo?tblsprefix=fs_", "foo"}, | ||
{"mysql://user:password@tcp(example.com:3306)/foo?tblsprefix=fs_", "foo"}, | ||
{"mysql://user:[email protected]:3306/foo/bar/baz?tblsprefix=fs_", "foo/bar/baz"}, | ||
{"mysql://user:[email protected]:3306//?tblsprefix=fs_", "/"}, | ||
{"mysql://user:[email protected]:3306/?tblsprefix=fs_", ""}, | ||
|
@@ -258,13 +274,35 @@ func TestParse_Params(t *testing.T) { | |
} | ||
|
||
for _, expected := range test.expected { | ||
if dsn.HasParam(expected.key) { | ||
if dsn.GetParam(expected.key) != expected.value { | ||
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", testId+1, expected.value, dsn.GetParam(expected.key)) | ||
} | ||
} else { | ||
t.Errorf("Expected key not found in returned result. Test: %v. Expected key: \"%s\".", testId+1, expected.key) | ||
if dsn.GetParam(expected.key) != expected.value { | ||
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", testId+1, expected.value, dsn.GetParam(expected.key)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestParse_Transport(t *testing.T) { | ||
tests := []struct { | ||
dsn string | ||
expected string | ||
}{ | ||
{"example.com", ""}, | ||
{"example.com:3306", ""}, | ||
{"(example.com)", ""}, | ||
{"tcp()", "tcp"}, | ||
{"x(example.com)", "x"}, | ||
{"tcp(example.com)", "tcp"}, | ||
{"tcp(example.com:3306)", "tcp"}, | ||
{"mysql://user:password@tcp(example.com)", "tcp"}, | ||
{"mysql://user:password@tcp(example.com:3306)", "tcp"}, | ||
{"mysql://user:password@tcp(example.com)/dbname?tblsprefix=fs_", "tcp"}, | ||
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "tcp"}, | ||
} | ||
|
||
for i, test := range tests { | ||
dsn := Parse(test.dsn) | ||
if dsn.GetTransport() != test.expected { | ||
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", i+1, test.expected, dsn.GetTransport()) | ||
} | ||
} | ||
} |