dsnparser

package module
v0.0.0-...-8858a2c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2025 License: MIT Imports: 0 Imported by: 3

README

DSN Parser

Build Status Coverage Status Go Report Card

This library parses a DSN of the form:

Each of the sections is optional, so, for example, a DSN can be without schema or credentials.

Installation
go get github.com/kos-v/dsnparser
Using
import "github.com/kos-v/dsnparser"

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:password@example.local:3306/dbname?encoding=utf8mb4"
Examples:
  • user:password@example.local
  • example.local:65535
  • user:password@
  • socket:///foo/bar.sock
  • mysql://user:password@example.local/dbname
  • mysql://example.local/?db=dbname&user=user&password=password
Credentials

user:password@ - user and password.
user@ or user:@ - only user, without password.
:password@ - only password, without user.

Escaping

You can escape the ":" and "@" characters in credentials, as well as the "=" and "&" characters in the extra options. To do this, specify "\" before the desired character.
Examples:
us\:e\@r:p\@ssw\:ord@ -> us:e@r:p@ssw:ord
us\:e\@r:p\@ssw\:ord@ -> us:e@r:p@ssw:ord
?key1=foo \& bar&key2=baz \= quux -> key1=foo & bar&key2=baz = quux
?foo\&key=fool val&bar\=key=bar val -> foo&key=fool val&bar=key=bar val

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DSN

type DSN struct {
	// contains filtered or unexported fields
}

DSN container for data after dsn parsing.

func Parse

func Parse(raw string) *DSN

Parse receives a raw dsn as argument, parses it and returns it in the DSN structure.

func (*DSN) GetHost

func (d *DSN) GetHost() string

GetHost returns a host as the string.

func (*DSN) GetHostPort

func (d *DSN) GetHostPort() string

GetHostPort returns a hostport as the string.

func (*DSN) GetParam

func (d *DSN) GetParam(key string) string

GetParam returns an additional parameter by key as the string.

func (*DSN) GetParams

func (d *DSN) GetParams() map[string]string

GetParams returns additional parameters as key-value map.

func (*DSN) GetPassword

func (d *DSN) GetPassword() string

GetPassword returns a credential password as the string.

func (*DSN) GetPath

func (d *DSN) GetPath() string

GetPath returns a path as the string.

func (*DSN) GetPort

func (d *DSN) GetPort() string

GetPort returns a port as the string.

func (*DSN) GetRaw

func (d *DSN) GetRaw() string

GetRaw returns the dsn in its raw form, as it was passed to the Parse function.

func (*DSN) GetScheme

func (d *DSN) GetScheme() string

GetScheme returns a scheme as the string.

func (*DSN) GetSource

func (d *DSN) GetSource() string

GetSource returns a dsn source user as the string.

func (*DSN) GetTransport

func (d *DSN) GetTransport() string

GetTransport returns a transport as the string.

func (*DSN) GetUser

func (d *DSN) GetUser() string

GetUser returns a credential user as the string.

func (*DSN) HasParam

func (d *DSN) HasParam(key string) bool

HasParam checks for the existence of an additional parameter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL