{"id":20278543,"url":"https://github.com/wmentor/dsn","last_synced_at":"2026-06-04T02:31:23.260Z","repository":{"id":57518787,"uuid":"246128192","full_name":"wmentor/dsn","owner":"wmentor","description":null,"archived":false,"fork":false,"pushed_at":"2021-02-09T20:09:28.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-14T06:54:14.603Z","etag":null,"topics":["dsn","go","golang","key-value","parser"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wmentor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-09T19:50:28.000Z","updated_at":"2021-03-13T07:31:00.000Z","dependencies_parsed_at":"2022-09-26T18:00:42.819Z","dependency_job_id":null,"html_url":"https://github.com/wmentor/dsn","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wmentor%2Fdsn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wmentor%2Fdsn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wmentor%2Fdsn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wmentor%2Fdsn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wmentor","download_url":"https://codeload.github.com/wmentor/dsn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241768282,"owners_count":20017129,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dsn","go","golang","key-value","parser"],"created_at":"2024-11-14T13:23:58.138Z","updated_at":"2026-06-04T02:31:23.219Z","avatar_url":"https://github.com/wmentor.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dsn\n\n![test](https://github.com/wmentor/dsn/workflows/test/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/wmentor/dsn/badge.svg?branch=master\u0026v=1.0.4)](https://coveralls.io/github/wmentor/dsn?branch=master)\n[![https://goreportcard.com/report/github.com/wmentor/dsn](https://goreportcard.com/badge/github.com/wmentor/dsn)](https://goreportcard.com/report/github.com/wmentor/dsn)\n[![https://pkg.go.dev/github.com/wmentor/dsn](https://pkg.go.dev/badge/github.com/wmentor/dsn.svg)](https://pkg.go.dev/github.com/wmentor/dsn)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nWe often work with configuration strings like pq database connection:\n\n```\nuser=mylogin password=mypass database=mydb host=127.0.0.1 port=5432 sslmode=true\n```\n\nThe simplest way to parse them in Golang is regular expressions:\n\n```golang\npackage main\n\nimport (\n  \"fmt\"\n  \"regexp\"\n)\n\nvar rex = regexp.MustCompile(\"(\\\\w+)=(\\\\w+)\")\n\nfunc main() {\n  conn := `user=mylogin password=mypass database=mydb\n           host=127.0.0.1 port=5432 sslmode=true`\n\n  data := rex.FindAllStringSubmatch(conn, -1)\n\n  res := make(map[string]string)\n  for _, kv := range data {\n    k := kv[1]\n    v := kv[2]\n    res[k] = v\n  }\n\n  fmt.Println(res)\n}\n```\n\nBut if the value can contain a space character, then you need to add escaping support in a regular expression. And so on, each additional action makes processing harder. But there is an easier way (wmentor/dsn).\n\nInstall package:\n\n```\ngo get github.com/wmentor/dsn\n```\n\nUsage:\n\n```golang\npackage main\n\nimport (\n  \"fmt\"\n\n  \"github.com/wmentor/dsn\"\n)\n\nfunc main() {\n\n  str := `user=mylogin passwd=mypass database=mydb\n          port=5432 sslmode=true`\n\n  ds, err := dsn.New(str)\n  if err != nil {\n    panic(\"invalid string\")\n  }\n\n  // print user=mylogin\n  fmt.Printf( \"user=%s\\n\", ds.GetString(\"user\",\"unknown\") )\n\n  // print passwd=mypass\n  fmt.Printf( \"passwd=%s\\n\", ds.GetString(\"passwd\",\"nopass\") )\n\n  // host is not exists, print host=127.0.0.1\n  fmt.Printf( \"host=%s\\n\", ds.GetString(\"host\",\"127.0.0.1\") )\n\n  // get int value and print port=5432\n  fmt.Printf( \"port=%d\\n\", ds.GetInt(\"port\", 4321) )\n\n  // print sslmode=true\n  fmt.Printf( \"sslmode=%t\\n\", ds.GetBool(\"sslmode\", false) )\n\n  // print keepalive=false\n  fmt.Printf( \"keepalive=%t\\n\", ds.GetBool(\"keepalive\", false) )\n}\n```\n\n*dns.New* returns object *dsn.DSN* or an error. All get methods (GetString,GetBool,GetInt,GetInt64,GetFloat) take 2 arguments - key name and default value. The default value is used when the key is missing or contains a invalid value.\n\nMoreover, dsn support escape some characters in key name and value (\\s,\\t,\\r,\\n,\\=,\\\\,\\\",\\'). See example below:\n\n```golang\npackage main\n\nimport (\n  \"fmt\"\n\n  \"github.com/wmentor/dsn\"\n)\n\nfunc main() {\n\n  str := `message=Hello,\\sWorld! calc=1+1\\=2`\n\n  ds, err := dsn.New(str)\n  if err != nil {\n    panic(\"invalid string\")\n  }\n\n  // print message=Hello, World!\n  fmt.Printf( \"message=%s\\n\", ds.GetString(\"message\",\"\") )\n\n  // print calc=1+2=2\n  fmt.Printf( \"calc=%s\\n\", ds.GetString(\"calc\",\"\") )\n}\n```\n\nObject dsn.DSN support set methods (SetString,SetBool,SetInt,SetInt64,SetFloat). They take 2 arguments - key name and value:\n\n```golang\nds.SetString(\"host\", \"192.168.1.1\")\n```\n\nStringer interface implemention makes simple convert dsn.DSN to string:\n\n```golang\nstr := ds.String()\n// or\nfmt.Println(ds)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwmentor%2Fdsn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwmentor%2Fdsn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwmentor%2Fdsn/lists"}