helper

package
v1.63.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckValidTask

func CheckValidTask(task interface{}) error

CheckValidTask checks if the provided task is a valid function. It returns an error if the task is nil, not a function, or if its value is nil.

func CheckValidTaskWithParameters

func CheckValidTaskWithParameters(task interface{}, parameters ...interface{}) error

CheckValidTaskWithParameters checks if the provided task and parameters are valid. It checks if the task is a valid function and if the parameters match the task's input types.

func GetInputParametersFromTask

func GetInputParametersFromTask(task interface{}) ([]reflect.Type, error)

GetInputParametersFromTask retrieves the input parameters of the provided task. It checks if the task is a valid function and returns its input parameter types.

func GetOutputParametersFromTask

func GetOutputParametersFromTask(task interface{}) ([]reflect.Type, error)

GetOutputParametersFromTask retrieves the output parameters of the provided task. It checks if the task is a valid function and returns its output parameter types.

func GetTaskNameFromFunction

func GetTaskNameFromFunction(f interface{}) (string, error)

GetTaskNameFromFunction retrieves the name of the function from the provided task. It checks if the task is a valid function and returns its name.

func GetTaskNameFromInterface

func GetTaskNameFromInterface(task interface{}) (string, error)

GetTaskNameFromInterface retrieves the name of the task from the provided interface. It checks if the task is a string or a function and returns its name accordingly.

func LenSyncMap added in v1.46.0

func LenSyncMap(m *sync.Map) int

LenSyncMap returns the number of entries in a sync.Map.

func MustStartPostgresContainer

func MustStartPostgresContainer() (func(ctx context.Context, opts ...testcontainers.TerminateOption) error, string, error)

MustStartPostgresContainer starts a PostgreSQL container for testing purposes. It uses the timescale/timescaledb image with PostgreSQL 17. It returns a function to terminate the container, the port on which the database is accessible, and an error if the container could not be started.

func SetTestDatabaseConfigEnvs

func SetTestDatabaseConfigEnvs(t *testing.T, port string)

SetTestDatabaseConfigEnvs sets the environment variables for the test database configuration. It sets the host, port, database name, username, password, schema, and table drop options for the test database.

Types

type Database

type Database struct {
	Name     string
	Logger   *slog.Logger
	Instance *sql.DB
}

Database represents a service that interacts with a database.

func NewDatabase

func NewDatabase(name string, dbConfig *DatabaseConfiguration, logger *slog.Logger) *Database

func NewDatabaseWithDB

func NewDatabaseWithDB(name string, dbConnnection *sql.DB, logger *slog.Logger) *Database

func NewTestDatabase

func NewTestDatabase(config *DatabaseConfiguration) *Database

NewTestDatabase creates a new Database instance for testing purposes. It initializes the database with the provided configuration and the name "test_db". It returns a pointer to the new Database instance.

func (*Database) CheckTableExistance

func (d *Database) CheckTableExistance(tableName string) (bool, error)

CheckTableExistance checks if a table with the specified name exists in the database. It queries the information_schema.tables to check for the existence of the table. It returns true if the table exists, false otherwise, and an error if the query fails.

func (*Database) Close

func (d *Database) Close() error

Close closes the database connection. It logs a message indicating the disconnection from the specific database. If the connection is successfully closed, it returns nil. If an error occurs while closing the connection, it returns the error.

func (*Database) ConnectToDatabase

func (d *Database) ConnectToDatabase(dbConfig *DatabaseConfiguration, logger *slog.Logger)

Internal function for the service creation to connect to a database. DatabaseConfiguration must contain uri, username and password. It initializes the database connection and sets the Instance field of the Database struct.

func (*Database) CreateCombinedIndex

func (d *Database) CreateCombinedIndex(tableName string, columnName1 string, columnName2 string) error

CreateCombinedIndex creates a combined index on the specified columns of the specified table. It uses the PostgreSQL CREATE INDEX statement to create the index. If the index already exists, it will not create a new one. It returns an error if the index creation fails.

func (*Database) CreateIndex

func (d *Database) CreateIndex(tableName string, columnName string) error

CreateIndex creates an index on the specified column of the specified table. It uses the PostgreSQL CREATE INDEX statement to create the index. If the index already exists, it will not create a new one. It returns an error if the index creation fails.

func (*Database) CreateIndexes

func (d *Database) CreateIndexes(tableName string, columnNames ...string) error

CreateIndexes creates indexes on the specified columns of the specified table. It iterates over the column names and calls CreateIndex for each one. It returns an error if any of the index creations fail.

func (*Database) CreateUniqueCombinedIndex

func (d *Database) CreateUniqueCombinedIndex(tableName string, columnName1 string, columnName2 string) error

CreateUniqueCombinedIndex creates a unique combined index on the specified columns of the specified table. It uses the PostgreSQL CREATE UNIQUE INDEX statement to create the index. If the index already exists, it will not create a new one. It returns an error if the index creation fails.

func (*Database) DropIndex

func (d *Database) DropIndex(tableName string, jsonMapKey string) error

DropIndex drops the index on the specified table and column. It uses the PostgreSQL DROP INDEX statement to drop the index. If the index does not exist, it will not return an error. It returns an error if the index dropping fails.

func (*Database) Health

func (d *Database) Health() map[string]string

Health checks the health of the database connection by pinging the database. It returns a map with keys indicating various health statistics.

type DatabaseConfiguration

type DatabaseConfiguration struct {
	Host          string
	Port          string
	Database      string
	Username      string
	Password      string
	Schema        string
	SSLMode       string
	WithTableDrop bool
}

func NewDatabaseConfiguration

func NewDatabaseConfiguration() (*DatabaseConfiguration, error)

NewDatabaseConfiguration creates a new DatabaseConfiguration instance. It reads the database configuration from environment variables. It returns a pointer to the new DatabaseConfiguration instance or an error if any required environment variable is not set.

func (*DatabaseConfiguration) DatabaseConnectionString

func (d *DatabaseConfiguration) DatabaseConnectionString() string

type Error added in v1.9.0

type Error struct {
	Original error
	Trace    []string
}

func NewError added in v1.9.0

func NewError(trace string, original error) Error

func (Error) Error added in v1.9.0

func (e Error) Error() string

type PrettyHandler added in v1.9.0

type PrettyHandler struct {
	slog.Handler
	// contains filtered or unexported fields
}

func NewPrettyHandler added in v1.9.0

func NewPrettyHandler(
	out io.Writer,
	opts PrettyHandlerOptions,
) *PrettyHandler

func (*PrettyHandler) Handle added in v1.9.0

func (h *PrettyHandler) Handle(ctx context.Context, r slog.Record) error

type PrettyHandlerOptions added in v1.9.0

type PrettyHandlerOptions struct {
	SlogOpts slog.HandlerOptions
}

Jump to

Keyboard shortcuts

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