From 78de94c9199e80464e4ae82048365e1d67f20338 Mon Sep 17 00:00:00 2001 From: Elijah Duffy Date: Wed, 28 Jan 2026 11:17:38 -0800 Subject: [PATCH] config: add Host and Port helpers --- config.go | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index df0e9da..25db35d 100644 --- a/config.go +++ b/config.go @@ -1,6 +1,10 @@ package dbx -import "fmt" +import ( + "fmt" + "strconv" + "strings" +) // DBConfig provides a template for database connection configuration compatible // with go-toolkit/config. If used as a field in a struct, remember to include @@ -29,3 +33,32 @@ func (cfg *DBConfig) ConnectionString(dialect Dialect) string { panic("unsupported dialect: " + string(dialect)) } } + +// Host returns the host portion of the database URI. +func (cfg *DBConfig) Host() (string, error) { + host, _, err := cfg.split() + return host, err +} + +// Port returns the port portion of the database URI. +func (cfg *DBConfig) Port() (int, error) { + _, port, err := cfg.split() + if err != nil { + return 0, err + } + + portInt, err := strconv.Atoi(port) + if err != nil { + return 0, fmt.Errorf("invalid port: %w", err) + } + return portInt, nil +} + +// splits the URI into host and port. +func (cfg *DBConfig) split() (string, string, error) { + split := strings.Split(cfg.URI, ":") + if len(split) != 2 { + return "", "", fmt.Errorf("invalid URI format") + } + return split[0], split[1], nil +}