Help us shape the future of Go! Take the 2026 Go Developer Survey

connection

package
v1.6.4-upstream Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: GPL-3.0 Imports: 29 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cache = cmap.New[*Connection]()

Functions

func CloseAll added in v1.3.5

func CloseAll()

CloseAll closes all cached connections

func CopyDirect

func CopyDirect(conn database.Connection, tableFName string, srcFile Connection) (cnt uint64, ok bool, err error)

CopyDirect copies directly from cloud files (without passing through dbio)

func EnvVarNameOf added in v1.6.4

func EnvVarNameOf(connName, key string) string

EnvVarNameOf builds the canonical env var name for a connection field.

func EnvVarRef added in v1.6.0

func EnvVarRef(connName, key string) string

EnvVarRef builds ${<NAME>_<PROP>} for a connection field.

func FormatUnsetRefError added in v1.6.0

func FormatUnsetRefError(refs []UnsetEnvRef, loc env.ConnLocation) error

FormatUnsetRefError names each unset var and its env.yaml line.

func LoadAPISpec added in v1.4.5

func LoadAPISpec(specIdentifier string) (spec api.Spec, err error)

LoadAPISpec loads the spec from the spec location

func MergeConnProps added in v1.6.0

func MergeConnProps(existing, incoming map[string]any) map[string]any

MergeConnProps copies existing and applies incoming. Nested maps merge. Keys that incoming does not pass stay on the result.

func NormalizeConnProps added in v1.6.0

func NormalizeConnProps(kv map[string]any) error

NormalizeConnProps parses secrets/inputs YAML strings into maps.

func ParsePropsInput added in v1.6.0

func ParsePropsInput(raw string) (map[string]any, error)

ParsePropsInput reads a YAML or JSON property map from stdin/payload text.

func PreserveRefs added in v1.6.4

func PreserveRefs(existing, incoming map[string]any)

PreserveRefs keeps an on-disk ${VAR} ref for any incoming literal value that equals that ref's expansion. The GUI never receives expanded values, but the CLI contract does allow setting a props map with resolved values; keeping the ref keeps the file free of plaintext secrets.

func PromoteLiteralSecrets added in v1.6.4

func PromoteLiteralSecrets(connName string, props, existing map[string]any, envUpdates map[string]any) (promoted []string)

PromoteLiteralSecrets replaces literal secret values in props with ${VAR} refs, recording the values to write under `env:` in envUpdates. It returns the promoted prop paths, e.g. []string{"password", "secrets.client_id"}.

Secret fields are: top-level keys in env.SecretKeys, and every leaf under a nested `secrets` map (same rule RejectLiteralSecrets applies when refusing). Values that are already ${VAR} refs are left alone, and so are values that equal the expansion of the ref already on disk for that field (existing), so retyping an unchanged secret does not add a plaintext copy to env:.

func PurgeCache added in v1.5.16

func PurgeCache(hash string)

PurgeCache removes a connection from the cache by hash so the next AsDatabaseContext / AsFileContext / AsAPIContext call rebuilds it. Use this after detecting a dead underlying handle (e.g. "sql: database is closed").

func ReadConnections

func ReadConnections(env map[string]interface{}) (conns map[string]Connection, err error)

func ReadConnectionsEnv

func ReadConnectionsEnv(env map[string]interface{}) (conns map[string]Connection, err error)

ReadConnections loads the connections

func ReadConnectionsFromFile

func ReadConnectionsFromFile(path string) (conns map[string]Connection, err error)

func ReadDbtConnections

func ReadDbtConnections() (conns map[string]Connection, err error)

func RejectLiteralSecrets added in v1.6.0

func RejectLiteralSecrets(name string, kv map[string]any) error

RejectLiteralSecrets refuses secret fields (and nested secrets values) whose value is not a ${VAR} ref.

func SchemeType

func SchemeType(url string) dbio.Type

SchemeType returns the correct scheme of the url

func ScrubConnProps added in v1.6.0

func ScrubConnProps(kv map[string]any) []map[string]any

ScrubConnProps returns key names and ref/status only. No secret values.

func ValidateConnProps added in v1.6.4

func ValidateConnProps(name string, props map[string]any) error

ValidateConnProps checks that props can be written as a connection entry: a `url` parses (and implies the type when absent), or a valid `type` is present. SetValidated applies it before the write; the GUI save path, which writes through its own env editor, calls it directly.

Types

type AsConnOptions added in v1.4.24

type AsConnOptions struct {
	UseCache bool
	Expire   int // in seconds
	Extra    map[string]any
}

type ConnEntries added in v1.2.16

type ConnEntries []ConnEntry

func GetLocalConns

func GetLocalConns(options ...any) ConnEntries

func (ConnEntries) Discover added in v1.2.19

func (ce ConnEntries) Discover(name string, opt *DiscoverOptions) (nodes filesys.FileNodes, schemata database.Schemata, endpoints api.Endpoints, err error)

func (ConnEntries) Get added in v1.2.16

func (ce ConnEntries) Get(name string) ConnEntry

func (ConnEntries) List added in v1.2.19

func (ce ConnEntries) List() (fields []string, rows [][]any)

func (ConnEntries) Names added in v1.4.7

func (ce ConnEntries) Names() (names []string)

func (ConnEntries) Test added in v1.2.19

func (ce ConnEntries) Test(name string) (ok bool, err error)

Test keeps its signature: it builds the options from the SLING_TEST_* env vars and calls TestWithOptions, so the CLI does not change.

func (ConnEntries) TestWithOptions added in v1.6.4

func (ce ConnEntries) TestWithOptions(ctx context.Context, name string, opts TestOptions) (ok bool, err error)

TestWithOptions tests the named connection with opts. When opts.SpecFile is set, it overlays that spec file on the connection's spec for this test only.

type ConnEntry

type ConnEntry struct {
	Name        string     `json:"name"`
	Description string     `json:"description"`
	Source      string     `json:"source"`
	Connection  Connection `json:"connection"`
}

func LocalFileConnEntry

func LocalFileConnEntry() ConnEntry

type Connection

type Connection struct {
	Name string                 `json:"name,omitempty"`
	Type dbio.Type              `json:"type,omitempty"`
	Data map[string]interface{} `json:"data,omitempty"`

	File     filesys.FileSysClient
	Database database.Connection
	API      *api.APIConnection
	// contains filtered or unexported fields
}

Connection is the base connection struct

func NewConnection

func NewConnection(Name string, t dbio.Type, Data map[string]interface{}) (conn Connection, err error)

NewConnection creates a new connection

func NewConnectionFromDbt

func NewConnectionFromDbt(name string) (c Connection, err error)

NewConnectionFromDbt loads a Connection from a DBT Profile

func NewConnectionFromEntry added in v1.6.4

func NewConnectionFromEntry(name string, props map[string]any) (Connection, error)

NewConnectionFromEntry builds a connection from the raw props of one env.yaml entry, the same way ReadConnectionsEnv does: refs expand with the env file rules, and the type stays in the data.

func NewConnectionFromMap

func NewConnectionFromMap(m map[string]interface{}) (c Connection, err error)

NewConnectionFromMap loads a Connection from a Map

func NewConnectionFromProfiles

func NewConnectionFromProfiles(name string) (c Connection, err error)

NewConnectionFromProfiles loads a Connection from YAML Profiles

func NewConnectionFromURL

func NewConnectionFromURL(Name, URL string) (conn Connection, err error)

NewConnectionFromURL creates a new connection from a url

func ParseLocation added in v1.4.5

func ParseLocation(location string) (conn Connection, objectExpr string, err error)

func (*Connection) AsAPI added in v1.4.5

func (c *Connection) AsAPI(options ...AsConnOptions) (ac *api.APIConnection, err error)

func (*Connection) AsAPIContext added in v1.4.5

func (c *Connection) AsAPIContext(ctx context.Context, options ...AsConnOptions) (ac *api.APIConnection, err error)

func (*Connection) AsDatabase

func (c *Connection) AsDatabase(options ...AsConnOptions) (dc database.Connection, err error)

func (*Connection) AsDatabaseContext added in v1.3.4

func (c *Connection) AsDatabaseContext(ctx context.Context, options ...AsConnOptions) (dc database.Connection, err error)

func (*Connection) AsFile

func (c *Connection) AsFile(options ...AsConnOptions) (fc filesys.FileSysClient, err error)

func (*Connection) AsFileContext added in v1.3.4

func (c *Connection) AsFileContext(ctx context.Context, options ...AsConnOptions) (fc filesys.FileSysClient, err error)

func (*Connection) Close

func (c *Connection) Close() error

Close closes the connection

func (*Connection) ConnSetDatabase

func (c *Connection) ConnSetDatabase(dbName string) *Connection

ConnSetDatabase returns a new connection with the specified database name

func (*Connection) Context

func (c *Connection) Context() *g.Context

Context returns the context

func (*Connection) Copy

func (c *Connection) Copy() *Connection

ToMap transforms DataConn to a Map

func (*Connection) DataS

func (c *Connection) DataS(lowerCase ...bool) map[string]string

DataS returns data as map[string]string

func (*Connection) DataSWithExtra added in v1.4.24

func (c *Connection) DataSWithExtra(extra map[string]any) map[string]string

func (*Connection) Discover added in v1.2.16

func (c *Connection) Discover(opt *DiscoverOptions) (ok bool, nodes filesys.FileNodes, schemata database.Schemata, endpoints api.Endpoints, err error)

func (*Connection) GetType added in v1.5.5

func (c *Connection) GetType() dbio.Type

GetType returns the more accurate type, especially for ODBC databases a bit gnarly...

func (*Connection) Hash

func (c *Connection) Hash(excludeKeys ...string) string

func (*Connection) Info

func (c *Connection) Info() Info

Info returns connection information

func (*Connection) IsADBC added in v1.5.2

func (c *Connection) IsADBC() bool

IsADBC returns whether connection originates from ADBC

func (*Connection) Set

func (c *Connection) Set(m map[string]interface{})

Set sets key/values from a map

func (*Connection) Test added in v1.2.16

func (c *Connection) Test() (ok bool, err error)

Test keeps its signature: it builds the options from the SLING_TEST_* env vars and calls TestWithOptions, so the CLI does not change.

func (*Connection) TestWithOptions added in v1.6.4

func (c *Connection) TestWithOptions(ctx context.Context, opts TestOptions) (ok bool, err error)

TestWithOptions tests a connection. For API connections it tests the requested (or all) endpoints, emitting a spec event per step to opts.OnEvent and stopping promptly when ctx is cancelled.

func (*Connection) ToMap

func (c *Connection) ToMap() map[string]interface{}

ToMap transforms DataConn to a Map

func (*Connection) URL

func (c *Connection) URL() string

URL returns the url string

type ConnectionInt

type ConnectionInt interface {
	// Self() Connection
	Close() error
	Context() g.Context
	Info() Info
	URL() string
	DataS(lowerCase ...bool) map[string]string
	ToMap() map[string]interface{}
	AsDatabase() (database.Connection, error)
	AsFile() (filesys.FileSysClient, error)
	Set(map[string]interface{})
}

ConnectionInt is a connection

type DiscoverOptions

type DiscoverOptions struct {
	Pattern   string                 `json:"pattern,omitempty"`
	Level     database.SchemataLevel `json:"level,omitempty"`
	Recursive bool                   `json:"recursive,omitempty"`
}

type EnvFileConns added in v1.2.19

type EnvFileConns struct {
	Name    string
	EnvFile *env.EnvFile
}

func (*EnvFileConns) ConnectionEntries added in v1.2.19

func (ec *EnvFileConns) ConnectionEntries() (entries ConnEntries, err error)

func (*EnvFileConns) Get added in v1.6.4

func (ec *EnvFileConns) Get(name string) (props map[string]any, found bool)

Get returns the raw (unexpanded) props of one connection from the env file, plus whether it exists in this specific file. Unlike ConnectionEntries, it does not go through LoadSlingEnvFile / ReadConnections, so ${VAR} refs stay refs and a resolved secret can never be surfaced.

func (*EnvFileConns) GetConnEntry added in v1.2.19

func (ec *EnvFileConns) GetConnEntry(name string) (conn ConnEntry, ok bool)

GetConnEntry get connection from envfile (may be provided via body)

func (*EnvFileConns) Keys added in v1.5.2

func (ec *EnvFileConns) Keys() []string

func (*EnvFileConns) RenameValidated added in v1.6.4

func (ec *EnvFileConns) RenameValidated(oldName, newName string) error

RenameValidated renames one connection entry of the env file: the entry keeps its position, its node and its comments, and the promoted `env:` keys keep their names, so the ${VAR} refs of the entry stay valid.

func (*EnvFileConns) Set added in v1.2.19

func (ec *EnvFileConns) Set(name string, kvMap map[string]any) (err error)

Set merges kvMap into one connection entry (the `sling conns set` and MCP path). It writes through SetValidated, so only the changed lines of the file change, and then refreshes the entry in ec.EnvFile.Connections.

func (*EnvFileConns) SetValidated added in v1.6.4

func (ec *EnvFileConns) SetValidated(name string, props map[string]any, opts SetOptions) (err error)

SetValidated is Set + validation/drop-guards, used by the GUI path and by Set. It merges over the raw on-disk entry (not the expanded struct), validates the name and type, and writes through env.EnvFileEditor: ${VAR} refs are never expanded onto disk, and lines that do not change stay as they are.

func (*EnvFileConns) Unset added in v1.2.19

func (ec *EnvFileConns) Unset(name string) (err error)

Unset removes one connection entry and its head comment from the env file. All other lines of the file stay as they are.

type Info

type Info struct {
	Name     string
	Type     dbio.Type
	LongType string
	Database string
	Data     map[string]interface{}
}

Info is the connection type

func (*Info) IsURL

func (i *Info) IsURL() bool

type LocalConnsExclude added in v1.4.11

type LocalConnsExclude string

type SetOptions added in v1.6.4

type SetOptions struct {
	// RejectLiteralSecrets refuses secret fields (and nested secrets values)
	// that are not ${VAR} refs. The GUI path promotes literals first
	// (PromoteLiteralSecrets) and then sets this as a backstop.
	RejectLiteralSecrets bool
	// AllowOverwrite permits replacing an existing connection entry.
	AllowOverwrite bool
	// RequireExisting makes a missing connection entry an error.
	RequireExisting bool
	// EnvUpdates, when non-empty, are written under `env:` in the same save
	// as the connection entry (one write).
	EnvUpdates map[string]any
	// AllowEnvOverwrite permits replacing existing env: values.
	AllowEnvOverwrite bool
	// Replace treats props as the full entry: keys that props does not pass
	// are removed from the stored entry. Without it the entry is merged, and
	// omitted keys are kept (the CLI contract). Secret refs of stored fields
	// are still preserved either way (PreserveRefs).
	Replace bool
}

SetOptions controls SetValidated.

type TestOptions added in v1.6.4

type TestOptions struct {
	Endpoints   []string       // endpoint names to test; empty means all
	Limit       int            // records per request (default 10)
	MaxRequests int            // requests per endpoint (default 2)
	Context     map[string]any // spec test context (store, range, mode)
	SpecFile    string         // overlay this spec file on the connection's spec
	Trace       bool           // trace-level logging
	OnEvent     func(api.SpecEvent)
}

TestOptions configures a connection test.

type UnsetEnvRef added in v1.6.0

type UnsetEnvRef struct {
	Key string
	Var string
}

UnsetEnvRef is a ${VAR} value that g.Rmd did not substitute (var not set).

func FindUnsetEnvRefs added in v1.6.0

func FindUnsetEnvRefs(data map[string]any) []UnsetEnvRef

FindUnsetEnvRefs walks connection data for whole-string ${VAR} values.

Jump to

Keyboard shortcuts

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