mt

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package mt provides generic multi-tenant tools.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneCtx

func CloneCtx(ctx context.Context) context.Context

CloneCtx creates a new context from parent with tenant ID and OpenTelemetry values preserved. Cancellation is not inherited; metadata is copied.

func ExpandConfigReader

func ExpandConfigReader(yamlPath string) (io.Reader, error)

ExpandConfigReader merges defaults into per-tenant configs and returns an io.Reader. YAML must have a "default" section. Other sections inherit from default and can override. Placeholders ${tenant} are replaced with the tenant ID.

func ExtractTID

func ExtractTID(ctx context.Context) string

ExtractTID returns tenant ID from context, or defaultTID if not found.

func InjectTID

func InjectTID(ctx context.Context, tenantID string) context.Context

InjectTID stores tenant ID in context, using defaultTID if empty.

func InjectTenantFromObj

func InjectTenantFromObj(ctx context.Context, v any) context.Context

InjectTenantFromObj extracts tenant ID from v if it implements tenantIDGetter, then stores it in a new context returned.

func SetDefTenant

func SetDefTenant(tenantID string)

SetDefTenant sets the default tenant ID. Must be called once.

Types

type ConfigMgr

type ConfigMgr[T any] struct {
	// contains filtered or unexported fields
}

ConfigMgr holds per-tenant configs with a fallback default.

func NewConfigMgr

func NewConfigMgr[T any](configs map[string]T, defCfg T) *ConfigMgr[T]

NewConfigMgr creates a new manager with copied tenant configs and a default.

func (*ConfigMgr[T]) Get

func (m *ConfigMgr[T]) Get(ctx context.Context) T

Get returns config for the tenant in ctx, or default if not found.

func (*ConfigMgr[T]) Map

func (m *ConfigMgr[T]) Map(f func(string, T))

Map calls f for each tenant config.

func (*ConfigMgr[T]) Tenants

func (m *ConfigMgr[T]) Tenants() []string

Tenants returns all configured tenant IDs.

type ISDK

type ISDK[T any] interface {
	Get(ctx context.Context) T
	Map(func(string, T))
}

ISDK is the interface for SDK managers returning T.

func NewSDKMgr

func NewSDKMgr[S any, C any](configs map[string]C, initFn InitFn[S, C]) ISDK[S]

NewSDKMgr creates a new SDK manager. configs maps tenant ID to SDK config. initFn is the factory function to create SDK instances.

func NewSDKMgrWMet

func NewSDKMgrWMet[S any, C any, M any](configs map[string]C, initFn InitFnWMet[S, C, M], metrics M) ISDK[S]

NewSDKMgrWMet creates a new SDK manager with metrics support.

type ISDKE

type ISDKE[T any] interface {
	Get(ctx context.Context) (T, error)
	IsCentralized() bool
}

ISDKE is the interface for SDK managers returning (T, error).

func NewSDKMgrE

func NewSDKMgrE[S any, C any](configs map[string]C, initFn InitFnE[S, C]) (ISDKE[S], error)

NewSDKMgrE creates a new SDK manager with error handling.

type InitFn

type InitFn[S any, C any] func(context.Context, C) S

type InitFnE

type InitFnE[S any, C any] func(context.Context, C) (S, error)

type InitFnWMet

type InitFnWMet[S any, C any, M any] func(context.Context, C, M) S

type MockISDK

type MockISDK[T any] struct {
	// contains filtered or unexported fields
}

MockISDK is a mock of ISDK interface.

func NewISDKMock

func NewISDKMock[T any](t testing.TB) (*gomock.Controller, *MockISDK[T])

NewISDKMock creates a mock for ISDK[T] (test helper).

func NewMockISDK

func NewMockISDK[T any](ctrl *gomock.Controller) *MockISDK[T]

NewMockISDK creates a new mock instance.

func (*MockISDK[T]) EXPECT

func (m *MockISDK[T]) EXPECT() *MockISDKMockRecorder[T]

EXPECT returns an object that allows expected use.

func (*MockISDK[T]) Get

func (m *MockISDK[T]) Get(ctx context.Context) T

Get mocks base method.

func (*MockISDK[T]) Map

func (m *MockISDK[T]) Map(f func(string, T))

Map mocks base method.

type MockISDKE

type MockISDKE[T any] struct {
	// contains filtered or unexported fields
}

MockISDKE is a mock of ISDKE interface.

func NewISDKEMock

func NewISDKEMock[T any](t testing.TB) (*gomock.Controller, *MockISDKE[T])

NewISDKEMock creates a mock for ISDKE[T] (test helper).

func NewMockISDKE

func NewMockISDKE[T any](ctrl *gomock.Controller) *MockISDKE[T]

NewMockISDKE creates a new mock instance.

func (*MockISDKE[T]) EXPECT

func (m *MockISDKE[T]) EXPECT() *MockISDKEMockRecorder[T]

EXPECT returns an object that allows expected use.

func (*MockISDKE[T]) Get

func (m *MockISDKE[T]) Get(ctx context.Context) (T, error)

Get mocks base method.

func (*MockISDKE[T]) IsCentralized

func (m *MockISDKE[T]) IsCentralized() bool

IsCentralized mocks base method.

type MockISDKEMockRecorder

type MockISDKEMockRecorder[T any] struct {
	// contains filtered or unexported fields
}

MockISDKEMockRecorder is the mock recorder for MockISDKE.

func (*MockISDKEMockRecorder[T]) Get

func (mr *MockISDKEMockRecorder[T]) Get(ctx any) *gomock.Call

Get indicates an expected call of Get.

func (*MockISDKEMockRecorder[T]) IsCentralized

func (mr *MockISDKEMockRecorder[T]) IsCentralized() *gomock.Call

IsCentralized indicates an expected call of IsCentralized.

type MockISDKMockRecorder

type MockISDKMockRecorder[T any] struct {
	// contains filtered or unexported fields
}

MockISDKMockRecorder is the mock recorder for MockISDK.

func (*MockISDKMockRecorder[T]) Get

func (mr *MockISDKMockRecorder[T]) Get(ctx any) *gomock.Call

Get indicates an expected call of Get.

func (*MockISDKMockRecorder[T]) Map

func (mr *MockISDKMockRecorder[T]) Map(f any) *gomock.Call

Map indicates an expected call of Map.

type SDKMgr

type SDKMgr[S any, C any] struct {
	// contains filtered or unexported fields
}

SDKMgr manages per-tenant SDK instances.

func (*SDKMgr[S, C]) Get

func (s *SDKMgr[S, C]) Get(ctx context.Context) S

func (*SDKMgr[S, C]) Map

func (m *SDKMgr[S, C]) Map(f func(string, S))

type SDKMgrE

type SDKMgrE[S any, C any] struct {
	// contains filtered or unexported fields
}

SDKMgrE manages per-tenant SDK instances with error handling.

func (*SDKMgrE[S, C]) Get

func (s *SDKMgrE[S, C]) Get(ctx context.Context) (S, error)

func (*SDKMgrE[S, C]) IsCentralized

func (s *SDKMgrE[S, C]) IsCentralized() bool

func (*SDKMgrE[S, C]) Map

func (m *SDKMgrE[S, C]) Map(f func(string, S))

type SDKMgrWMet

type SDKMgrWMet[S any, C any, M any] struct {
	// contains filtered or unexported fields
}

SDKMgrWMet manages per-tenant SDK instances with metrics.

func (*SDKMgrWMet[S, C, M]) Get

func (s *SDKMgrWMet[S, C, M]) Get(ctx context.Context) S

func (*SDKMgrWMet[S, C, M]) Map

func (m *SDKMgrWMet[S, C, M]) Map(f func(string, S))

type TenantRepl

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

TenantRepl is a regex-based tenant ID replacer.

func NewTenantRepl

func NewTenantRepl(pairs map[string]string) (*TenantRepl, error)

NewTenantRepl creates a replacer from pattern→replacement pairs.

func (*TenantRepl) Replace

func (r *TenantRepl) Replace(input string) string

Replace returns the first matching replacement for input, or input if no match.

type TplRenderer

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

TplRenderer implements echo template rendering with per-tenant templates.

func NewTplRenderer

func NewTplRenderer(basePath string, tenantReplacements map[string]string) (*TplRenderer, error)

NewTplRenderer loads templates from tenant subdirectories of basePath. tenantReplacements are used to map tenant IDs to template sets.

func (*TplRenderer) Render

func (r *TplRenderer) Render(w io.Writer, name string, data any, c echo.Context) error

Render renders a template for the current tenant.

Jump to

Keyboard shortcuts

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