currency

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package currency provides functionality for working with currency codes and metadata. It includes validation, formatting, and conversion utilities for ISO 4217 currency codes.

Deprecated: This package is deprecated and will be removed in a future release. Please use the money package instead: github.com/amirasaad/fintech/pkg/money

Index

Constants

View Source
const (
	// DefaultCode is the fallback currency code (USD)
	DefaultCode = "USD"
	// DefaultDecimals is the default number of decimal places for currencies
	DefaultDecimals = 2
	// MaxDecimals is the maximum number of decimal places allowed
	MaxDecimals = 18
	// MaxSymbolLength is the maximum length for currency symbols
	MaxSymbolLength = 10

	// Default is the default currency code (USD)
	// Deprecated: Use money.USD from the money package instead
	Default = USD
)
View Source
const (
	// USD represents US Dollar.
	//
	// Deprecated: Use money.USD from the money package instead.
	USD = money.USD

	// EUR represents Euro.
	//
	// Deprecated: Use money.EUR from the money package instead.
	EUR = money.EUR

	// JPY represents Japanese Yen.
	//
	// Deprecated: Use money.JPY from the money package instead.
	JPY = money.JPY

	// KWD represents Kuwaiti Dinar.
	//
	// Deprecated: Use money.KWD from the money package instead.
	KWD = money.KWD

	// GBP represents British Pound.
	//
	// Deprecated: Use money.GBP from the money package instead.
	GBP = money.GBP
)

Common currency codes for convenience. These are provided for backward compatibility.

Variables

View Source
var (
	// Deprecated: Use money.ErrInvalidCurrency instead
	ErrInvalidCode     = money.ErrInvalidCurrency
	ErrUnsupported     = errors.New("unsupported currency")
	ErrInvalidDecimals = errors.New("invalid decimals: must be between 0 and 8")
	ErrInvalidSymbol   = errors.New(
		"invalid symbol: must not be empty and max 10 characters")
	ErrCurrencyNotFound = errors.New("currency not found")
	ErrCurrencyExists   = errors.New("currency already exists")
)

Common errors

View Source
var Get = getCurrencyInternal

Get returns currency metadata for the given code

Functions

func Count

func Count() (int, error)

func CountActive

func CountActive() (int, error)

func CountLegacy

func CountLegacy() int

func InitializeGlobalRegistry

func InitializeGlobalRegistry(ctx context.Context, redisURL ...string) error

InitializeGlobalRegistry initializes the global currency registry with optional Redis configuration. If redisURL is provided, it will be used to configure Redis caching. If keyPrefix is provided, it will be used as the Redis key prefix. If redisURL is empty, an in-memory cache will be used.

This function should be called during application startup.

func IsSupported

func IsSupported(code string) bool

func IsSupportedLegacy

func IsSupportedLegacy(code string) bool

func IsValidFormat

func IsValidFormat(code string) bool

IsValidFormat returns true if the code is a well-formed ISO 4217 currency code (3 uppercase letters).

func Legacy

func Legacy(code string, meta Meta)

Legacy Backward compatibility functions (deprecated)

func ListSupported

func ListSupported() ([]string, error)

func ListSupportedLegacy

func ListSupportedLegacy() []string

func Register

func Register(meta Meta) error

Register Global convenience functions with error handling

func Unregister

func Unregister(code string) error

func UnregisterLegacy

func UnregisterLegacy(code string) bool

Types

type Amount deprecated added in v1.3.0

type Amount = money.Amount

Amount represents a monetary amount with its currency.

Deprecated: This type is deprecated and will be removed in a future release. Please use money.Amount from the money package instead.

type Code deprecated

type Code = money.Code

Code represents a currency code (e.g., "USD", "EUR").

Deprecated: This type is deprecated and will be removed in a future release. Please use money.Code from the money package instead.

type Currency deprecated added in v1.3.0

type Currency = money.Currency

Currency represents currency information.

Deprecated: This type is deprecated and will be removed in a future release. Please use money.Currency from the money package instead.

type CurrencyConverter deprecated added in v1.3.0

type CurrencyConverter = exchange.Exchange

CurrencyConverter is a type alias for backward compatibility.

Deprecated: This type is deprecated and will be removed in a future release. Please use exchange.Exchange from the provider package instead.

type Entity

type Entity struct {
	*registry.BaseEntity
	// contains filtered or unexported fields
}

Entity implements the registry.Entity interface Deprecated

func NewEntity

func NewEntity(meta Meta) *Entity

NewEntity creates a new currency entity

func (*Entity) Active

func (c *Entity) Active() bool

Active returns whether the currency is active

func (*Entity) Code

func (c *Entity) Code() string

Code returns the currency code

func (*Entity) CreatedAt

func (c *Entity) CreatedAt() time.Time

CreatedAt returns the creation timestamp

func (*Entity) Meta

func (c *Entity) Meta() Meta

Meta returns the currency metadata

func (*Entity) Metadata

func (c *Entity) Metadata() map[string]string

Metadata returns currency metadata

func (*Entity) Name

func (c *Entity) Name() string

Name returns the currency name

func (*Entity) UpdatedAt

func (c *Entity) UpdatedAt() time.Time

UpdatedAt returns the last update timestamp

type Meta

type Meta struct {
	Code     string            `json:"code"`
	Name     string            `json:"name"`
	Symbol   string            `json:"symbol"`
	Decimals int               `json:"decimals"`
	Country  string            `json:"country,omitempty"`
	Region   string            `json:"region,omitempty"`
	Active   bool              `json:"active"`
	Metadata map[string]string `json:"metadata,omitempty"`
	Created  time.Time         `json:"created"`
	Updated  time.Time         `json:"updated"`
}

Meta holds currency-specific metadata Deprecated:

func GetLegacy

func GetLegacy(code string) Meta

func ListAll

func ListAll() ([]Meta, error)
func Search(query string) ([]Meta, error)

func SearchByRegion

func SearchByRegion(region string) ([]Meta, error)

type Money deprecated added in v1.3.0

type Money = money.Money

Money represents an amount of money in a specific currency.

Deprecated: This type is deprecated and will be removed in a future release. Please use money.Money from the money package instead.

type Registry

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

Registry provides currency-specific operations using the registry system

func GetGlobalRegistry

func GetGlobalRegistry() *Registry

GetGlobalRegistry returns the global currency registry instance. Make sure to call InitializeGlobalRegistry first to initialize the registry. If the registry is nil, it will attempt lazy initialization with in-memory cache. Panics if initialization fails, as the application cannot function without a currency registry.

func New

func New(ctx context.Context, params ...string) (*Registry, error)

New creates a new currency registry with default currencies If redisURL is provided, it will use Redis for caching The function accepts optional parameters in this order: redisURL, keyPrefix

func NewRegistryWithPersistence

func NewRegistryWithPersistence(
	ctx context.Context, persistencePath string, redisURL ...string,
) (*Registry, error)

NewRegistryWithPersistence creates a currency registry with persistence If redisURL is provided, it will use Redis for caching

func (*Registry) Activate

func (cr *Registry) Activate(code string) error

Activate activates a currency

func (*Registry) Count

func (cr *Registry) Count() (int, error)

Count returns the total number of registered currencies

func (*Registry) CountActive

func (cr *Registry) CountActive() (int, error)

CountActive returns the number of active currencies

func (*Registry) Deactivate

func (cr *Registry) Deactivate(code string) error

Deactivate deactivates a currency

func (*Registry) Get

func (cr *Registry) Get(code string) (Meta, error)

Get returns currency metadata for the given code

func (*Registry) GetRegistry

func (cr *Registry) GetRegistry() registry.Provider

GetRegistry returns the underlying registry provider

func (*Registry) IsSupported

func (cr *Registry) IsSupported(code string) bool

IsSupported checks if a currency code is registered and active

func (*Registry) ListAll

func (cr *Registry) ListAll() ([]Meta, error)

ListAll returns all registered currencies (active and inactive)

func (*Registry) ListSupported

func (cr *Registry) ListSupported() ([]string, error)

ListSupported returns a list of all supported currency codes

func (*Registry) Register

func (cr *Registry) Register(meta Meta) error

Register adds or updates a currency in the registry

func (*Registry) Search

func (cr *Registry) Search(query string) ([]Meta, error)

Search searches for currencies by name

func (*Registry) SearchByRegion

func (cr *Registry) SearchByRegion(region string) ([]Meta, error)

SearchByRegion searches for currencies by region

func (*Registry) SetRegistry added in v1.2.0

func (cr *Registry) SetRegistry(reg registry.Provider)

func (*Registry) Unregister

func (cr *Registry) Unregister(code string) error

Unregister removes a currency from the registry

type Validator

type Validator struct{}

Validator implements registry.Validator for currency entities

func NewCurrencyValidator

func NewCurrencyValidator() *Validator

NewCurrencyValidator creates a new currency validator

func (*Validator) Validate

func (cv *Validator) Validate(ctx context.Context, entity registry.Entity) error

Validate validates a currency entity

func (*Validator) ValidateMetadata

func (cv *Validator) ValidateMetadata(ctx context.Context, metadata map[string]string) error

ValidateMetadata validates currency metadata

Jump to

Keyboard shortcuts

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