-
-
Save fipar/4271194 to your computer and use it in GitHub Desktop.
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
// returns the individual fields for a dsn, or an error | |
func ParseDSN(input string) (error error, host string, port string, user string, password string, database string) { | |
args := strings.Split(string(input), ",") | |
if len(args) < 1 { | |
return errors.New("Seems like " + string(input) + " is not a valid dsn"), "", "", "", "", "" | |
} | |
for i := 0; i < len(args); i++ { | |
tmp := strings.Split(args[i], "=") | |
if len(tmp) < 2 { | |
return errors.New("I can't parse " + args[i]), "", "", "", "", "" | |
} | |
switch tmp[0] { | |
case "h": | |
{ | |
host = tmp[1] | |
} | |
case "P": | |
{ | |
port = tmp[1] | |
} | |
case "p": | |
{ | |
password = tmp[1] | |
} | |
case "u": | |
{ | |
user = tmp[1] | |
} | |
case "D": | |
{ | |
database = tmp[1] | |
} | |
default: | |
{ | |
return errors.New("Bad option (" + tmp[0] + " while parsing DSN"), "", "", "", "", "" | |
} | |
} | |
} | |
return nil, host, port, user, password, database | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment