Compare commits

..

5 Commits

36 changed files with 4313 additions and 65 deletions

View File

@ -1,16 +1,24 @@
package entity
import "time"
import (
"time"
"git.gocasts.ir/ebhomengo/niki/pkg/types"
"github.com/shopspring/decimal"
)
type Transaction struct {
ID uint64
UserID uint64
Amount float64
Currency Currency
Amount decimal.Decimal
Currency types.Currency
ActionType TransactionType
Timestamp time.Time
CreatedAt time.Time
}
func (T Transaction) UnimplementedAllowUseDBGenericFunc() {}
type TransactionType string
const (
@ -19,10 +27,3 @@ const (
TransactionTypeRefund TransactionType = "refund"
TransactionTypeDonate TransactionType = "donate"
)
type Currency string
const (
IRR Currency = "IRR"
USD Currency = "USD"
)

View File

@ -1,23 +1,28 @@
package entity
import "time"
import (
"time"
"git.gocasts.ir/ebhomengo/niki/pkg/types"
"github.com/shopspring/decimal"
)
type Wallet struct {
ID uint64
UserID uint64 // user unique ID
Balance float64
Currency Currency
UpdatedAt time.Time
Balance decimal.Decimal
Currency types.Currency
Status WalletStatus // "active", "frozen", "closed"
UpdatedAt time.Time
CreatedAt time.Time
}
func (w Wallet) UnimplementedAllowUseDBGenericFunc() {}
type WalletStatus string
const (
Frozen WalletStatus = "frozen" // when need to check , approve ,validate , solve sth (but deposit is possible)
Active WalletStatus = "active" // when everything is ok
// ??
// Closed WalletStatus = "closed" // when need to check , approve ,validate , solve sth (exp : security problem)
)

View File

@ -1,15 +1,22 @@
package param
import (
"time"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
"git.gocasts.ir/ebhomengo/niki/pkg/types"
"github.com/shopspring/decimal"
)
type CreateTransactionRequest struct {
UserID uint64 `json:"user_id"`
Amount float64 `json:"amount"`
Currency entity.Currency `json:"currency"`
Amount string `json:"amount"`
Currency types.Currency `json:"currency"`
ActionType entity.TransactionType `json:"action_type"`
Timestamp time.Time `json:"timestamp"`
}
type InsertTransactionResponse struct {
Balance decimal.Decimal `json:"balance"`
Currency types.Currency `json:"currency"`
}

View File

@ -4,21 +4,26 @@ import (
"time"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
"git.gocasts.ir/ebhomengo/niki/pkg/database/postgres"
"git.gocasts.ir/ebhomengo/niki/pkg/types"
"github.com/shopspring/decimal"
)
type TransactionRequest struct {
UserID uint64 `json:"user_id"`
Pagination postgres.RequestPagination `json:"pagination"`
}
type TransactionResponse struct {
Transaction []TransactionInfo `json:"transaction"`
Transaction []TransactionInfo `json:"transactions"`
Pagination postgres.ResponsePagination `json:"pagination"`
}
type TransactionInfo struct {
ID uint64 `json:"id"`
UserID uint64 `json:"user_id"`
Amount float64 `json:"amount"`
Currency entity.Currency `json:"currency"`
Amount decimal.Decimal `json:"amount,string"`
Currency types.Currency `json:"currency"`
ActionType entity.TransactionType `json:"action_type"`
Timestamp time.Time `json:"timestamp"`
}

View File

@ -4,6 +4,8 @@ import (
"time"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
"git.gocasts.ir/ebhomengo/niki/pkg/types"
"github.com/shopspring/decimal"
)
type WalletRequest struct {
@ -13,9 +15,9 @@ type WalletRequest struct {
type WalletResponse struct {
Wallet WalletInfo `json:"wallet"`
}
type WalletInfo struct {
Balance float64 `json:"balance"`
Balance decimal.Decimal `json:"balance,string"`
UpdatedAt time.Time `json:"updated_at"`
Status entity.WalletStatus `json:"status"`
Currency types.Currency `json:"currency"`
}

View File

@ -2,8 +2,12 @@ package postgres
import "git.gocasts.ir/ebhomengo/niki/pkg/database/postgres"
type Config struct {
}
type DB struct {
conn *postgres.DB
Config Config
}
func New(conn *postgres.DB) *DB {

View File

@ -0,0 +1,109 @@
package postgres
import (
"context"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
"git.gocasts.ir/ebhomengo/niki/pkg/database/postgres"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (db *DB) GetTransactionListByUserID(ctx context.Context, userID uint64, dbPagination postgres.DBPagination) ([]entity.Transaction, int64, error) {
const op = richerror.Op("Wallet.repo.GetTransactionListByUserID")
countQuery := "SELECT COUNT(*) FROM transactions WHERE user_id = $1"
fetchQuery := `SELECT * FROM Transactions WHERE user_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3`
countQueryStmt := postgres.StatementKeyAWalletGetTotalCountTransactionHistory
fetchQueryStmt := postgres.StatementKeyAWalletGetTransactionHistory
PageOffset := (dbPagination.PageNumber - 1) * dbPagination.PageSize // offset
PageSize := dbPagination.PageSize //limit
countParams := []any{userID} // $1
fetchParams := []any{userID, PageSize, PageOffset} // $1 , $2 , $3
//
//
/////////////////////////// with generic pagination package
transactionsList, totalItemsCount, err := postgres.PageNumberPagination[entity.Transaction](ctx, countQuery, fetchQuery,
db.conn, countQueryStmt, fetchQueryStmt, op,
scanTransaction, countParams, fetchParams,
)
if err != nil {
return nil, 0, richerror.New(op).WithErr(err)
}
return transactionsList, totalItemsCount, nil
/////////////////////////// normal
//var totalCount int64
//
//countStmt, CountStErr := db.conn.PrepareStatement(ctx, countQueryStmt, countQuery)
//
//if CountStErr != nil {
//
// return nil, 0, richerror.New(op).WithErr(CountStErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
//
//}
//countErr := countStmt.QueryRowContext(ctx, userID).Scan(&totalCount)
//
//if countErr != nil {
// return nil, 0, richerror.New(op).WithErr(CountStErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
//
//}
//
////// get records
//
//stmt, StErr := db.conn.PrepareStatement(ctx, fetchQueryStmt, fetchQuery)
//
//if StErr != nil {
//
// return nil, 0, richerror.New(op).WithErr(StErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
//
//}
//
//offset := (dbPagination.PageNumber - 1) * dbPagination.PageSize
//
//limit := dbPagination.PageSize
//
//queryRows, qrErr := stmt.QueryContext(ctx, userID, limit, offset)
//
//if qrErr != nil {
// return nil, 0, richerror.New(op).WithErr(qrErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
//}
//
//defer queryRows.Close()
//
//var transactions []entity.Transaction
//
//for queryRows.Next() {
//
// transaction, err := scanTransaction(queryRows)
//
// if err != nil {
// return nil, 0, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgCantScanQueryResult)
// }
//
// transactions = append(transactions, transaction)
//
//}
//
//if qErr := queryRows.Err(); qErr != nil {
//
// return nil, 0, richerror.New(op).WithErr(qErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgCantScanQueryResult)
//}
//
//return transactions, totalCount, nil
}
func scanTransaction(scanner postgres.Scanner) (transaction entity.Transaction, err error) {
err = scanner.Scan(&transaction.ID, &transaction.UserID, &transaction.Amount, &transaction.Currency, &transaction.ActionType, &transaction.Timestamp, &transaction.CreatedAt)
if err != nil {
return
}
return
}

View File

@ -0,0 +1,54 @@
package postgres
import (
"context"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
"git.gocasts.ir/ebhomengo/niki/pkg/database/postgres"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (db *DB) GetWalletByUserID(ctx context.Context, UserID uint64) (entity.Wallet, error) {
const op = richerror.Op("Wallet.repo.GetWalletByUserID")
query := `SELECT * FROM wallets WHERE user_id = $1`
/////////////// use instant query
wallet, err := postgres.InstantQueryRowContext[entity.Wallet](ctx, postgres.StatementKeyWalletGetUserWallet, query, db.conn, scanWallet, UserID)
if err != nil {
return entity.Wallet{}, richerror.New(op).WithErr(err)
}
return wallet, nil
//////////////////////normal
//stmt, stErr := db.conn.PrepareStatement(ctx, postgres.StatementKeyWalletGetUserWallet, query)
//if stErr != nil {
// return entity.Wallet{}, richerror.New(op).WithErr(stErr)
//}
//walletRow := db.conn.StmtQueryRowContext(ctx, stmt, UserID)
//
//wallet, sErr := scanWallet(walletRow)
//
//if sErr != nil {
// return entity.Wallet{}, richerror.New(op).WithErr(sErr)
//}
//
//return wallet, nil
}
func scanWallet(scanner postgres.Scanner) (entity.Wallet, error) {
var wallet entity.Wallet
err := scanner.Scan(&wallet.ID, &wallet.UserID, &wallet.Balance, &wallet.Currency, &wallet.Status, &wallet.UpdatedAt, &wallet.CreatedAt, &wallet.CreatedAt)
if err != nil {
return entity.Wallet{}, err
}
return wallet, nil
}

View File

@ -0,0 +1,105 @@
package postgres
import (
"context"
"time"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
"git.gocasts.ir/ebhomengo/niki/pkg/database/postgres"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/pkg/types"
"github.com/shopspring/decimal"
)
func (db *DB) InsertTransaction(ctx context.Context, transaction entity.Transaction, currencyRate decimal.Decimal) (balance decimal.Decimal, currency types.Currency, err error) {
const op = richerror.Op("wallet.repo.InsertTransaction")
// TODO : USE TX INSTANT QUERY
query := `INSERT INTO Transactions (user_id, amount ,currency ,action_type, timestamp) values ($1, $2, $3, $4, $5)`
stmt, stErr := db.conn.PrepareStatement(ctx, postgres.StatementKeyWalletInsertTransaction, query)
if stErr != nil {
err = richerror.New(op).WithErr(stErr).WithKind(richerror.KindUnexpected)
return
}
txHolder, newCtx := postgres.GetDBTxHolderFromContextOrNew(ctx)
tx, txErr := txHolder.BeginTx(newCtx, db.conn.Conn())
if txErr != nil {
err = richerror.New(op).WithErr(txErr).WithKind(richerror.KindUnexpected)
return
}
defer func() {
if tx != nil {
if err != nil {
if rbErr := tx.Rollback(); rbErr != nil {
// log rbErr
}
} else if cErr := tx.Commit(); cErr != nil {
// log cErr
}
}
}()
params := []any{transaction.UserID, transaction.Amount, transaction.Currency, transaction.ActionType, transaction.Timestamp}
_, execErr := tx.StmtExecContext(newCtx, stmt, params...)
if execErr != nil {
err = richerror.New(op).WithErr(execErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
return
}
newBalance, walletCurrency, balanceErr := db.UpsertBalance(newCtx, transaction, currencyRate)
if balanceErr != nil {
err = richerror.New(op).WithErr(balanceErr)
return
}
balance = newBalance
currency = walletCurrency
return
}
func (db *DB) UpsertBalance(newCtx context.Context, transaction entity.Transaction, currencyRate decimal.Decimal) (balance decimal.Decimal, currency types.Currency, err error) {
const op = richerror.Op("wallet.repo.UpdateBalance")
txHolder, _ := postgres.GetDBTxHolderFromContextOrNew(newCtx)
tx, txErr := txHolder.Conn()
if txErr != nil {
err = richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery).WithKind(richerror.KindUnexpected)
return
}
if tx == nil {
err = richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery).WithKind(richerror.KindUnexpected)
return
}
upsertQuery := `INSERT INTO wallets (user_id, balance , updated_at) VALUES ($1,$2,$3 )
ON CONFLICT (user_id) DO UPDATE SET balance = wallets.balance + $2
RETURNING balance , currency`
upsertStmt, stErr := db.conn.PrepareStatement(newCtx, postgres.StatementKeyWalletUpsertBalance, upsertQuery)
if stErr != nil {
err = richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery).WithKind(richerror.KindUnexpected)
return
}
amount := transaction.Amount.Mul(currencyRate)
row := tx.StmtQueryRowContext(newCtx, upsertStmt, transaction.UserID, amount, time.Now())
sErr := row.Scan(&balance, &currency)
if sErr != nil {
err = richerror.New(op).WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
return
}
return
}

View File

@ -0,0 +1,17 @@
-- +migrate Up
CREATE TABLE "transaction" (
"id" BIGSERIAL PRIMARY KEY,
"user_id" BIGINT NOT NULL,
"amount" NUMERIC(20, 2) NOT NULL,
"currency" VARCHAR(100) NOT NULL,
"action_type" VARCHAR(100) NOT NULL,
"timestamp" TIMESTAMP NOT NULL,
"created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- +migrate Down
DROP TABLE IF EXISTS "transaction";

View File

@ -0,0 +1,18 @@
-- +migrate Up
CREATE TABLE "wallets" (
"id" BIGSERIAL PRIMARY KEY,
"user_id" BIGINT NOT NULL,
"balance" NUMERIC(20, 2) NOT NULL,
"currency" VARCHAR(100) NOT NULL DEFAULT 'IRR',
"status" VARCHAR(100) NOT NULL DEFAULT 'active',
"updated_at" TIMESTAMP,
"created_at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- +migrate Down
DROP TABLE IF EXISTS "wallets";

View File

@ -7,25 +7,51 @@ import (
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/param"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"git.gocasts.ir/ebhomengo/niki/pkg/types"
//"git.gocasts.ir/ebhomengo/niki/pkg/types"
"github.com/shopspring/decimal"
)
func (s Service) CreateTransaction(ctx context.Context, request param.CreateTransactionRequest) (param.InsertTransactionResponse, error) {
const op = richerror.Op("wallet.service.CreateTransaction")
currencyRate := s.convertCurrency(request.Currency)
convertedAmount, _ := decimal.NewFromString(request.Amount)
transaction := entity.Transaction{
ID: 0,
UserID: request.UserID,
Amount: request.Amount,
Amount: convertedAmount,
Currency: request.Currency,
ActionType: request.ActionType,
Timestamp: time.Now(),
}
err := s.repo.InsertTransaction(ctx, transaction)
if err != nil {
return param.InsertTransactionResponse{}, err
Timestamp: request.Timestamp,
CreatedAt: time.Now(),
}
return param.InsertTransactionResponse{}, nil
balance, walletCurrency, inErr := s.repo.InsertTransaction(ctx, transaction, currencyRate)
if inErr != nil {
return param.InsertTransactionResponse{}, richerror.New(op).WithErr(inErr)
}
return param.InsertTransactionResponse{Balance: balance, Currency: walletCurrency}, nil
}
func (s Service) convertCurrency(currency types.Currency) decimal.Decimal {
if currency != types.IRR {
currencyRate, cErr := s.currencyRateProvider.GetCurrencyPriceRateInIRR(currency)
if cErr != nil {
// log // fallback or change provider
return decimal.Zero // if 0 => transaction commited with currency but wallet doesn't update or add 0 to wallet
}
return currencyRate
}
return decimal.NewFromInt(1)
}

View File

@ -13,12 +13,13 @@ func (s Service) GetUserWallet(ctx context.Context, request param.WalletRequest)
wallet, err := s.repo.GetWalletByUserID(ctx, request.UserID)
if err != nil {
return param.WalletResponse{}, err
return param.WalletResponse{}, richerror.New(op).WithErr(err)
}
return param.WalletResponse{
Wallet: param.WalletInfo{
Balance: wallet.Balance,
Currency: wallet.Currency,
UpdatedAt: wallet.UpdatedAt,
Status: wallet.Status,
},

View File

@ -4,20 +4,29 @@ import (
"context"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
"git.gocasts.ir/ebhomengo/niki/pkg/database/postgres"
"git.gocasts.ir/ebhomengo/niki/pkg/types"
"github.com/shopspring/decimal"
)
type CurrencyRateProvider interface {
GetCurrencyPriceRateInIRR(currency types.Currency) (decimal.Decimal, error)
}
type Repository interface {
GetTransactionListByUserID(ctx context.Context, UserID uint64) ([]entity.Transaction, error)
GetTransactionListByUserID(ctx context.Context, UserID uint64, DBPagination postgres.DBPagination) ([]entity.Transaction, int64, error)
GetWalletByUserID(ctx context.Context, UserID uint64) (entity.Wallet, error)
InsertTransaction(ctx context.Context, transaction entity.Transaction) error
InsertTransaction(ctx context.Context, transaction entity.Transaction, currencyRate decimal.Decimal) (decimal.Decimal, types.Currency, error)
}
type Config struct {
PageSize int64 `koanf:"page_size"`
}
type Service struct {
repo Repository
cfg Config
currencyRateProvider CurrencyRateProvider
}
func New(repo Repository, cfg Config) Service {

View File

@ -5,19 +5,36 @@ import (
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/param"
"git.gocasts.ir/ebhomengo/niki/pkg/database/postgres"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
func (s Service) GetUserTransactionHistory(ctx context.Context, request param.TransactionRequest) (param.TransactionResponse, error) {
const op = richerror.Op("wallet.service.GetUserTransactionHistory")
transactionList, err := s.repo.GetTransactionListByUserID(ctx, request.UserID)
if err != nil {
return param.TransactionResponse{}, err
dbPagination := postgres.DBPagination{
PageNumber: request.Pagination.PageNumber,
PageSize: s.cfg.PageSize,
}
return param.TransactionResponse{Transaction: transactionEntityToTransactionInfo(transactionList)}, nil
transactionList, totalCount, err := s.repo.GetTransactionListByUserID(ctx, request.UserID, dbPagination)
if err != nil {
return param.TransactionResponse{}, richerror.New(op).WithErr(err)
}
totalPages := (totalCount + s.cfg.PageSize - 1) / s.cfg.PageSize
paginationInfo := postgres.ResponsePagination{
PageNumber: request.Pagination.PageNumber,
PageSize: s.cfg.PageSize,
TotalPages: totalPages,
}
return param.TransactionResponse{
Transaction: transactionEntityToTransactionInfo(transactionList),
Pagination: paginationInfo,
}, nil
}

1
go.mod
View File

@ -21,6 +21,7 @@ require (
github.com/ory/dockertest/v3 v3.12.0
github.com/redis/go-redis/v9 v9.18.0
github.com/rubenv/sql-migrate v1.8.1
github.com/shopspring/decimal v1.4.0
github.com/spf13/cobra v1.10.2
github.com/stretchr/testify v1.11.1
github.com/swaggo/echo-swagger v1.5.2

2
go.sum
View File

@ -366,6 +366,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=

View File

@ -3,11 +3,13 @@ package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"sync"
"time"
querier "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
_ "github.com/jackc/pgx/v5/stdlib"
)
@ -21,17 +23,17 @@ type Config struct {
MaxIdleConn int `koanf:"maxIdleConns"`
MaxOpenConn int `koanf:"maxOpenConns"`
ConnMaxLifetime int `koanf:"connMaxLifetime"`
PathOfMigrations string `koanf:"pathOfMigrations"`
}
type DB struct {
config Config
db *querier.SQLDB
db *sql.DB
mu sync.Mutex
statements map[statementKey]*sql.Stmt
}
func (db *DB) Conn() *querier.SQLDB {
func (db *DB) Conn() *sql.DB {
return db.db
}
@ -56,7 +58,7 @@ func New(config Config) *DB {
return &DB{
config: config,
db: &querier.SQLDB{DB: db},
db: db,
statements: make(map[statementKey]*sql.Stmt),
}
}
@ -93,5 +95,118 @@ func (db *DB) CloseStatements() error {
}
func (db *DB) Close() error {
return db.db.DB.Close()
return db.db.Close()
}
func (db *DB) StmtQueryContext(ctx context.Context, stmt *sql.Stmt, args ...any) (*sql.Rows, error) {
return stmt.QueryContext(ctx, args...)
}
func (db *DB) StmtQueryRowContext(ctx context.Context, stmt *sql.Stmt, args ...any) *sql.Row {
return stmt.QueryRowContext(ctx, args...)
}
func (db *DB) StmtExecContext(ctx context.Context, stmt *sql.Stmt, args ...any) (sql.Result, error) {
result, err := stmt.ExecContext(ctx, args...)
if err != nil {
return nil, err
}
return result, nil
}
// /////////////////////// generic query
type AllowUseDBGenericFunc interface {
UnimplementedAllowUseDBGenericFunc()
}
func InstantQueryContext[T AllowUseDBGenericFunc](ctx context.Context, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) ([]T, error) {
const op = richerror.Op("postgres.InstantQueryContext")
readyStmt, err := conn.PrepareStatement(ctx, stmtKey, query)
if err != nil {
return nil, richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery)
}
rows, qErr := readyStmt.QueryContext(ctx, args...)
if qErr != nil {
return nil, richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery)
}
defer rows.Close()
var itemsList []T
for rows.Next() {
item, sErr := scanner(rows)
if sErr != nil {
return nil, richerror.New(op).WithErr(sErr).WithMessage(errmsg.ErrorMsgCantScanQueryResult)
}
itemsList = append(itemsList, item)
}
if rErr := rows.Err(); rErr != nil {
return nil, richerror.New(op).WithErr(rErr).WithMessage(errmsg.ErrorMsgFailedQuery)
}
return itemsList, nil
}
func InstantQueryRowContext[T AllowUseDBGenericFunc](ctx context.Context, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) (item T, err error) {
const op = richerror.Op("postgres.InstantQueryRowContext")
readyStmt, sErr := conn.PrepareStatement(ctx, stmtKey, query)
if sErr != nil {
err = richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery)
return
}
row := readyStmt.QueryRowContext(ctx, args...)
item, scErr := scanner(row)
if scErr != nil {
if errors.Is(scErr, sql.ErrNoRows) {
err = richerror.New(op).WithErr(scErr).WithKind(richerror.KindNotFound).WithMessage(errmsg.ErrorMsgCantScanQueryResult)
return
}
err = richerror.New(op).WithErr(scErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgCantScanQueryResult)
return
}
if rErr := row.Err(); rErr != nil {
err = richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
return
}
return
}
func InstantExecContext[T AllowUseDBGenericFunc](ctx context.Context, stmtKey statementKey, query string, conn *DB, args ...any) (sql.Result, error) {
const op = richerror.Op("postgres.InstantExecContext")
readyStmt, err := conn.PrepareStatement(ctx, stmtKey, query)
if err != nil {
return nil, richerror.New(op)
}
result, err := readyStmt.ExecContext(ctx, args...)
if err != nil {
return nil, richerror.New(op)
}
return result, nil
}
type ScannerFunc[T any] func(scanner Scanner) (T, error)

View File

@ -2,4 +2,4 @@ production:
dialect: postgres
datasource: "host=127.0.0.1 port=5432 user=wallet password=wallet2123 dbname=wallet_db sslmode=disable"
dir: domain/wallet/repository/postgres/migrations
table: wallet_migrationsns
table: gorp_migrationsns

View File

@ -1 +1,113 @@
package migrator
import (
"database/sql"
"fmt"
"os"
"text/tabwriter"
"git.gocasts.ir/ebhomengo/niki/pkg/database/postgres"
_ "github.com/jackc/pgx/v5/stdlib"
migrate "github.com/rubenv/sql-migrate"
)
type Config struct {
migrationDBName string `koanf:"migration_db_name"`
pathOfMigrations string `koanf:"pathOfMigrations"`
dbConfig postgres.Config
}
type Migrator struct {
cfg Config
dialect string
migrations *migrate.FileMigrationSource
}
func New(cfg Config) *Migrator {
return &Migrator{
cfg: cfg,
dialect: "psx",
migrations: &migrate.FileMigrationSource{Dir: cfg.pathOfMigrations},
}
}
func (m *Migrator) getDsn() string {
return fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
m.cfg.dbConfig.Host, m.cfg.dbConfig.User, m.cfg.dbConfig.Password, m.cfg.dbConfig.DbName)
}
func (m *Migrator) Up() {
db, err := sql.Open(m.dialect, m.getDsn())
if err != nil {
panic(fmt.Errorf("can't open postgres db: %v", err))
}
defer db.Close()
if err != nil {
return
}
n, err := migrate.Exec(db, m.dialect, m.migrations, migrate.Up)
if err != nil {
panic(fmt.Errorf("cant apply migrations : %v ", err))
}
fmt.Printf("Applied %d migrations\n", n)
}
func (m *Migrator) Down() {
migrate.SetTable(m.cfg.migrationDBName)
db, err := sql.Open(m.dialect, m.getDsn())
if err != nil {
panic(fmt.Errorf("can't open postgres db: %v", err))
}
defer db.Close()
n, err := migrate.Exec(db, m.dialect, m.migrations, migrate.Down)
if err != nil {
panic(fmt.Errorf("cant rollback migrations : %v ", err))
}
fmt.Printf("Applied %d migrations\n", n)
}
func (m *Migrator) Status() {
migrate.SetTable(m.cfg.migrationDBName)
db, err := sql.Open(m.dialect, m.getDsn())
if err != nil {
panic(fmt.Errorf("can't open postgres db: %v", err))
}
defer db.Close()
migrations, _, err := migrate.PlanMigration(db, m.dialect, m.migrations, migrate.Up, 0)
if err != nil {
panic(fmt.Errorf("can't plan migrations: %v", err))
}
if len(migrations) == 0 {
fmt.Println("✅ No pending migrations.")
return
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "PENDING MIGRATIONS")
fmt.Fprintln(w, "ID\tSTATUS")
for _, migration := range migrations {
fmt.Fprintf(w, "%s\tPending\n", migration.Id)
}
w.Flush()
}

View File

@ -0,0 +1,83 @@
package postgres
import (
"context"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)
// page number pagination
type RequestPagination struct {
PageNumber int64 `json:"page_number"`
}
type ResponsePagination struct {
PageNumber int64 `json:"page_number"`
PageSize int64 `json:"page_size"`
TotalPages int64 `json:"total_pages"`
}
type DBPagination struct {
PageNumber int64
PageSize int64
}
func PageNumberPagination[T AllowUseDBGenericFunc](ctx context.Context, countQuery string, fetchQuery string, conn *DB, countQueryStmt statementKey, fetchQueryStmt statementKey, op richerror.Op, scanner ScannerFunc[T], countParams []any, fetchParams []any) ([]T, int64, error) {
var totalCount int64
countStmt, CountStErr := conn.PrepareStatement(ctx, countQueryStmt, countQuery)
if CountStErr != nil {
return nil, 0, richerror.New(op).WithErr(CountStErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
}
countErr := countStmt.QueryRowContext(ctx, countParams...).Scan(&totalCount)
if countErr != nil {
return nil, 0, richerror.New(op).WithErr(countErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
}
//// get records
stmt, StErr := conn.PrepareStatement(ctx, fetchQueryStmt, fetchQuery)
if StErr != nil {
return nil, 0, richerror.New(op).WithErr(StErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
}
queryRows, qrErr := stmt.QueryContext(ctx, fetchParams...)
if qrErr != nil {
return nil, 0, richerror.New(op).WithErr(qrErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
}
defer queryRows.Close()
var itemsList []T
for queryRows.Next() {
item, err := scanner(queryRows)
if err != nil {
return nil, 0, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgCantScanQueryResult)
}
itemsList = append(itemsList, item)
}
if qErr := queryRows.Err(); qErr != nil {
return nil, 0, richerror.New(op).WithErr(qErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
}
return itemsList, totalCount, nil
}

View File

@ -3,7 +3,9 @@ package postgres
type statementKey uint
const (
StatementKeyAWalletGetTransactionHistory statementKey = iota + 1
StatementKeyWalletInsertTransaction
StatementKeyWalletGetUserWallet
StatementKeyAWalletGetTransactionHistory statementKey = iota + 1 //wallet
StatementKeyAWalletGetTotalCountTransactionHistory //wallet
StatementKeyWalletInsertTransaction //wallet
StatementKeyWalletGetUserWallet //wallet
StatementKeyWalletUpsertBalance //wallet
)

View File

@ -0,0 +1,5 @@
package postgres
type Scanner interface {
Scan(dest ...any) error
}

View File

@ -0,0 +1,277 @@
package postgres
import (
"context"
"database/sql"
"errors"
"fmt"
"sync"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"github.com/lib/pq"
)
type contextKey string
const DBTxHolderContextKey contextKey = "txholder"
type conn interface {
Commit() error
Rollback() error
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
StmtQueryContext(ctx context.Context, stmt *sql.Stmt, args ...any) (*sql.Rows, error)
StmtQueryRowContext(ctx context.Context, stmt *sql.Stmt, args ...any) *sql.Row
StmtExecContext(ctx context.Context, stmt *sql.Stmt, args ...any) (sql.Result, error)
}
type TxConn struct {
*sql.Tx
}
type DBTxHolder struct {
conn conn
mu sync.Mutex
}
func (db *DBTxHolder) Conn() (conn, error) {
if db.conn == nil {
return nil, fmt.Errorf("Conn() called before BeginTx()")
}
return db.conn, nil
}
func GetDBTxHolderFromContextOrNew(ctx context.Context) (*DBTxHolder, context.Context) {
db, ok := ctx.Value(DBTxHolderContextKey).(*DBTxHolder)
if !ok {
db = &DBTxHolder{
conn: nil,
}
ctx = context.WithValue(ctx, DBTxHolderContextKey, db)
}
return db, ctx
}
func (db *DBTxHolder) BeginTx(ctx context.Context, conn *sql.DB) (conn, error) {
dbTransactionConn, err := db.Continue(ctx, conn)
if err != nil {
return nil, err
}
return dbTransactionConn, nil
}
func (db *DBTxHolder) Continue(ctx context.Context, conn *sql.DB) (conn, error) {
//db.mu.Lock()
//defer db.mu.Unlock()
var err error
if db.conn == nil {
db.conn, err = db.txFactory(ctx, conn)
if err != nil {
return nil, err
}
}
return db.conn, err
}
func (db *DBTxHolder) txFactory(ctx context.Context, rConn *sql.DB) (*TxConn, error) {
tx, bErr := rConn.BeginTx(ctx, nil)
if bErr != nil {
return nil, bErr
}
return &TxConn{Tx: tx}, nil
}
func (db *DBTxHolder) Commit() error {
//db.mu.Lock()
//defer db.mu.Unlock()
if db.conn == nil {
return fmt.Errorf("no active transaction")
}
err := db.conn.Commit()
db.conn = nil
return err
}
func (db *DBTxHolder) Rollback() error {
//db.mu.Lock()
//defer db.mu.Unlock()
if db.conn == nil {
return fmt.Errorf("no active transaction")
}
err := db.conn.Rollback()
db.conn = nil
return err
}
func (db *DBTxHolder) SavePoint(ctx context.Context, key string) error {
//db.mu.Lock()
//defer db.mu.Unlock()
if db.conn == nil {
return fmt.Errorf("no active transaction")
}
quoted := pq.QuoteIdentifier(key)
_, err := db.conn.ExecContext(ctx, "SAVEPOINT "+quoted)
return err
}
func (db *DBTxHolder) RollbackSavePoint(ctx context.Context, key string) error {
//db.mu.Lock()
//defer db.mu.Unlock()
if db.conn == nil {
return fmt.Errorf("no active transaction")
}
quoted := pq.QuoteIdentifier(key)
_, err := db.conn.ExecContext(ctx, "ROLLBACK TO SAVEPOINT "+quoted)
if err != nil {
return err
}
return nil
}
func (db *DBTxHolder) ReleaseSavePoint(ctx context.Context, key string) error {
//db.mu.Lock()
//defer db.mu.Unlock()
if db.conn == nil {
return fmt.Errorf("no active transaction")
}
quoted := pq.QuoteIdentifier(key)
_, err := db.conn.ExecContext(ctx, "RELEASE SAVEPOINT "+quoted)
if err != nil {
return err
}
return nil
}
func (tx *TxConn) StmtQueryContext(ctx context.Context, stmt *sql.Stmt, args ...any) (*sql.Rows, error) {
txStmt := tx.StmtContext(ctx, stmt)
return txStmt.QueryContext(ctx, args...)
}
func (tx *TxConn) StmtQueryRowContext(ctx context.Context, stmt *sql.Stmt, args ...any) *sql.Row {
txStmt := tx.StmtContext(ctx, stmt)
return txStmt.QueryRowContext(ctx, args...)
}
func (tx *TxConn) StmtExecContext(ctx context.Context, stmt *sql.Stmt, args ...any) (sql.Result, error) {
txStmt := tx.StmtContext(ctx, stmt)
result, err := txStmt.ExecContext(ctx, args...)
if err != nil {
return nil, err
}
return result, nil
}
///////////////////////// generic query
func TXInstantQueryContext[T AllowUseDBGenericFunc](ctx context.Context, txConn *sql.Tx, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) ([]T, error) {
const op = richerror.Op("postgres.TXInstantQueryContext")
stmt, err := conn.PrepareStatement(ctx, stmtKey, query)
if err != nil {
return nil, richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery)
}
txStmt := txConn.StmtContext(ctx, stmt)
rows, qErr := txStmt.QueryContext(ctx, args...)
if qErr != nil {
return nil, richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery)
}
defer rows.Close()
var itemsList []T
for rows.Next() {
item, sErr := scanner(rows)
if sErr != nil {
return nil, richerror.New(op).WithErr(sErr).WithMessage(errmsg.ErrorMsgCantScanQueryResult)
}
itemsList = append(itemsList, item)
}
if rErr := rows.Err(); rErr != nil {
return nil, richerror.New(op).WithErr(rErr).WithMessage(errmsg.ErrorMsgFailedQuery)
}
return itemsList, nil
}
func TXInstantQueryRowContext[T AllowUseDBGenericFunc](ctx context.Context, txConn *sql.Tx, stmtKey statementKey, query string, conn *DB, scanner ScannerFunc[T], args ...any) (item T, err error) {
const op = richerror.Op("postgres.TXInstantQueryRowContext")
stmt, sErr := conn.PrepareStatement(ctx, stmtKey, query)
if sErr != nil {
err = richerror.New(op).WithMessage(errmsg.ErrorMsgFailedQuery)
return
}
txStmt := txConn.StmtContext(ctx, stmt)
row := txStmt.QueryRowContext(ctx, args...)
item, scErr := scanner(row)
if scErr != nil {
if errors.Is(scErr, sql.ErrNoRows) {
err = richerror.New(op).WithErr(scErr).WithKind(richerror.KindNotFound).WithMessage(errmsg.ErrorMsgCantScanQueryResult)
return
}
err = richerror.New(op).WithErr(scErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgCantScanQueryResult)
return
}
if rErr := row.Err(); rErr != nil {
err = richerror.New(op).WithErr(rErr).WithKind(richerror.KindUnexpected).WithMessage(errmsg.ErrorMsgFailedQuery)
return
}
return
}
func TXInstantExecContext[T AllowUseDBGenericFunc](ctx context.Context, txConn *sql.Tx, stmtKey statementKey, query string, conn *DB, args ...any) (sql.Result, error) {
const op = richerror.Op("postgres.TXInstantExecContext")
stmt, err := conn.PrepareStatement(ctx, stmtKey, query)
if err != nil {
return nil, richerror.New(op)
}
txStmt := txConn.StmtContext(ctx, stmt)
result, err := txStmt.ExecContext(ctx, args...)
if err != nil {
return nil, richerror.New(op)
}
return result, nil
}

View File

@ -58,4 +58,5 @@ const (
ErrorMsgInvalidRefreshToken = "invalid refresh token"
ErrorMsgInvalidBenefactorStatus = "invalid benefactor status"
ErrorMsgInvalidAction = "action invalid"
ErrorMsgFailedQuery = "query failed" // wallet
)

21
pkg/types/currency.go Normal file
View File

@ -0,0 +1,21 @@
package types
import "errors"
type Currency string
const (
IRR Currency = "IRR"
USD Currency = "USD"
)
func StringCastToCurrency(s string) (Currency, error) {
switch s {
case "IRR":
return IRR, nil
case "USD":
return USD, nil
default:
return IRR, errors.New("not a valid currency")
}
}

9
vendor/github.com/shopspring/decimal/.gitignore generated vendored Normal file
View File

@ -0,0 +1,9 @@
.git
*.swp
# IntelliJ
.idea/
*.iml
# VS code
*.code-workspace

76
vendor/github.com/shopspring/decimal/CHANGELOG.md generated vendored Normal file
View File

@ -0,0 +1,76 @@
## Decimal v1.4.0
#### BREAKING
- Drop support for Go version older than 1.10 [#361](https://github.com/shopspring/decimal/pull/361)
#### FEATURES
- Add implementation of natural logarithm [#339](https://github.com/shopspring/decimal/pull/339) [#357](https://github.com/shopspring/decimal/pull/357)
- Add improved implementation of power operation [#358](https://github.com/shopspring/decimal/pull/358)
- Add Compare method which forwards calls to Cmp [#346](https://github.com/shopspring/decimal/pull/346)
- Add NewFromBigRat constructor [#288](https://github.com/shopspring/decimal/pull/288)
- Add NewFromUint64 constructor [#352](https://github.com/shopspring/decimal/pull/352)
#### ENHANCEMENTS
- Migrate to Github Actions [#245](https://github.com/shopspring/decimal/pull/245) [#340](https://github.com/shopspring/decimal/pull/340)
- Fix examples for RoundDown, RoundFloor, RoundUp, and RoundCeil [#285](https://github.com/shopspring/decimal/pull/285) [#328](https://github.com/shopspring/decimal/pull/328) [#341](https://github.com/shopspring/decimal/pull/341)
- Use Godoc standard to mark deprecated Equals and StringScaled methods [#342](https://github.com/shopspring/decimal/pull/342)
- Removed unnecessary min function for RescalePair method [#265](https://github.com/shopspring/decimal/pull/265)
- Avoid reallocation of initial slice in MarshalBinary (GobEncode) [#355](https://github.com/shopspring/decimal/pull/355)
- Optimize NumDigits method [#301](https://github.com/shopspring/decimal/pull/301) [#356](https://github.com/shopspring/decimal/pull/356)
- Optimize BigInt method [#359](https://github.com/shopspring/decimal/pull/359)
- Support scanning uint64 [#131](https://github.com/shopspring/decimal/pull/131) [#364](https://github.com/shopspring/decimal/pull/364)
- Add docs section with alternative libraries [#363](https://github.com/shopspring/decimal/pull/363)
#### BUGFIXES
- Fix incorrect calculation of decimal modulo [#258](https://github.com/shopspring/decimal/pull/258) [#317](https://github.com/shopspring/decimal/pull/317)
- Allocate new(big.Int) in Copy method to deeply clone it [#278](https://github.com/shopspring/decimal/pull/278)
- Fix overflow edge case in QuoRem method [#322](https://github.com/shopspring/decimal/pull/322)
## Decimal v1.3.1
#### ENHANCEMENTS
- Reduce memory allocation in case of initialization from big.Int [#252](https://github.com/shopspring/decimal/pull/252)
#### BUGFIXES
- Fix binary marshalling of decimal zero value [#253](https://github.com/shopspring/decimal/pull/253)
## Decimal v1.3.0
#### FEATURES
- Add NewFromFormattedString initializer [#184](https://github.com/shopspring/decimal/pull/184)
- Add NewNullDecimal initializer [#234](https://github.com/shopspring/decimal/pull/234)
- Add implementation of natural exponent function (Taylor, Hull-Abraham) [#229](https://github.com/shopspring/decimal/pull/229)
- Add RoundUp, RoundDown, RoundCeil, RoundFloor methods [#196](https://github.com/shopspring/decimal/pull/196) [#202](https://github.com/shopspring/decimal/pull/202) [#220](https://github.com/shopspring/decimal/pull/220)
- Add XML support for NullDecimal [#192](https://github.com/shopspring/decimal/pull/192)
- Add IsInteger method [#179](https://github.com/shopspring/decimal/pull/179)
- Add Copy helper method [#123](https://github.com/shopspring/decimal/pull/123)
- Add InexactFloat64 helper method [#205](https://github.com/shopspring/decimal/pull/205)
- Add CoefficientInt64 helper method [#244](https://github.com/shopspring/decimal/pull/244)
#### ENHANCEMENTS
- Performance optimization of NewFromString init method [#198](https://github.com/shopspring/decimal/pull/198)
- Performance optimization of Abs and Round methods [#240](https://github.com/shopspring/decimal/pull/240)
- Additional tests (CI) for ppc64le architecture [#188](https://github.com/shopspring/decimal/pull/188)
#### BUGFIXES
- Fix rounding in FormatFloat fallback path (roundShortest method, fix taken from Go main repository) [#161](https://github.com/shopspring/decimal/pull/161)
- Add slice range checks to UnmarshalBinary method [#232](https://github.com/shopspring/decimal/pull/232)
## Decimal v1.2.0
#### BREAKING
- Drop support for Go version older than 1.7 [#172](https://github.com/shopspring/decimal/pull/172)
#### FEATURES
- Add NewFromInt and NewFromInt32 initializers [#72](https://github.com/shopspring/decimal/pull/72)
- Add support for Go modules [#157](https://github.com/shopspring/decimal/pull/157)
- Add BigInt, BigFloat helper methods [#171](https://github.com/shopspring/decimal/pull/171)
#### ENHANCEMENTS
- Memory usage optimization [#160](https://github.com/shopspring/decimal/pull/160)
- Updated travis CI golang versions [#156](https://github.com/shopspring/decimal/pull/156)
- Update documentation [#173](https://github.com/shopspring/decimal/pull/173)
- Improve code quality [#174](https://github.com/shopspring/decimal/pull/174)
#### BUGFIXES
- Revert remove insignificant digits [#159](https://github.com/shopspring/decimal/pull/159)
- Remove 15 interval for RoundCash [#166](https://github.com/shopspring/decimal/pull/166)

45
vendor/github.com/shopspring/decimal/LICENSE generated vendored Normal file
View File

@ -0,0 +1,45 @@
The MIT License (MIT)
Copyright (c) 2015 Spring, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
- Based on https://github.com/oguzbilgic/fpd, which has the following license:
"""
The MIT License (MIT)
Copyright (c) 2013 Oguz Bilgic
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

139
vendor/github.com/shopspring/decimal/README.md generated vendored Normal file
View File

@ -0,0 +1,139 @@
# decimal
[![ci](https://github.com/shopspring/decimal/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/shopspring/decimal/actions/workflows/ci.yml)
[![GoDoc](https://godoc.org/github.com/shopspring/decimal?status.svg)](https://godoc.org/github.com/shopspring/decimal)
[![Go Report Card](https://goreportcard.com/badge/github.com/shopspring/decimal)](https://goreportcard.com/report/github.com/shopspring/decimal)
Arbitrary-precision fixed-point decimal numbers in go.
_Note:_ Decimal library can "only" represent numbers with a maximum of 2^31 digits after the decimal point.
## Features
* The zero-value is 0, and is safe to use without initialization
* Addition, subtraction, multiplication with no loss of precision
* Division with specified precision
* Database/sql serialization/deserialization
* JSON and XML serialization/deserialization
## Install
Run `go get github.com/shopspring/decimal`
## Requirements
Decimal library requires Go version `>=1.10`
## Documentation
http://godoc.org/github.com/shopspring/decimal
## Usage
```go
package main
import (
"fmt"
"github.com/shopspring/decimal"
)
func main() {
price, err := decimal.NewFromString("136.02")
if err != nil {
panic(err)
}
quantity := decimal.NewFromInt(3)
fee, _ := decimal.NewFromString(".035")
taxRate, _ := decimal.NewFromString(".08875")
subtotal := price.Mul(quantity)
preTax := subtotal.Mul(fee.Add(decimal.NewFromFloat(1)))
total := preTax.Mul(taxRate.Add(decimal.NewFromFloat(1)))
fmt.Println("Subtotal:", subtotal) // Subtotal: 408.06
fmt.Println("Pre-tax:", preTax) // Pre-tax: 422.3421
fmt.Println("Taxes:", total.Sub(preTax)) // Taxes: 37.482861375
fmt.Println("Total:", total) // Total: 459.824961375
fmt.Println("Tax rate:", total.Sub(preTax).Div(preTax)) // Tax rate: 0.08875
}
```
## Alternative libraries
When working with decimal numbers, you might face problems this library is not perfectly suited for.
Fortunately, thanks to the wonderful community we have a dozen other libraries that you can choose from.
Explore other alternatives to find the one that best fits your needs :)
* [cockroachdb/apd](https://github.com/cockroachdb/apd) - arbitrary precision, mutable and rich API similar to `big.Int`, more performant than this library
* [alpacahq/alpacadecimal](https://github.com/alpacahq/alpacadecimal) - high performance, low precision (12 digits), fully compatible API with this library
* [govalues/decimal](https://github.com/govalues/decimal) - high performance, zero-allocation, low precision (19 digits)
* [greatcloak/decimal](https://github.com/greatcloak/decimal) - fork focusing on billing and e-commerce web application related use cases, includes out-of-the-box BSON marshaling support
## FAQ
#### Why don't you just use float64?
Because float64 (or any binary floating point type, actually) can't represent
numbers such as `0.1` exactly.
Consider this code: http://play.golang.org/p/TQBd4yJe6B You might expect that
it prints out `10`, but it actually prints `9.999999999999831`. Over time,
these small errors can really add up!
#### Why don't you just use big.Rat?
big.Rat is fine for representing rational numbers, but Decimal is better for
representing money. Why? Here's a (contrived) example:
Let's say you use big.Rat, and you have two numbers, x and y, both
representing 1/3, and you have `z = 1 - x - y = 1/3`. If you print each one
out, the string output has to stop somewhere (let's say it stops at 3 decimal
digits, for simplicity), so you'll get 0.333, 0.333, and 0.333. But where did
the other 0.001 go?
Here's the above example as code: http://play.golang.org/p/lCZZs0w9KE
With Decimal, the strings being printed out represent the number exactly. So,
if you have `x = y = 1/3` (with precision 3), they will actually be equal to
0.333, and when you do `z = 1 - x - y`, `z` will be equal to .334. No money is
unaccounted for!
You still have to be careful. If you want to split a number `N` 3 ways, you
can't just send `N/3` to three different people. You have to pick one to send
`N - (2/3*N)` to. That person will receive the fraction of a penny remainder.
But, it is much easier to be careful with Decimal than with big.Rat.
#### Why isn't the API similar to big.Int's?
big.Int's API is built to reduce the number of memory allocations for maximal
performance. This makes sense for its use-case, but the trade-off is that the
API is awkward and easy to misuse.
For example, to add two big.Ints, you do: `z := new(big.Int).Add(x, y)`. A
developer unfamiliar with this API might try to do `z := a.Add(a, b)`. This
modifies `a` and sets `z` as an alias for `a`, which they might not expect. It
also modifies any other aliases to `a`.
Here's an example of the subtle bugs you can introduce with big.Int's API:
https://play.golang.org/p/x2R_78pa8r
In contrast, it's difficult to make such mistakes with decimal. Decimals
behave like other go numbers types: even though `a = b` will not deep copy
`b` into `a`, it is impossible to modify a Decimal, since all Decimal methods
return new Decimals and do not modify the originals. The downside is that
this causes extra allocations, so Decimal is less performant. My assumption
is that if you're using Decimals, you probably care more about correctness
than performance.
## License
The MIT License (MIT)
This is a heavily modified fork of [fpd.Decimal](https://github.com/oguzbilgic/fpd), which was also released under the MIT License.

63
vendor/github.com/shopspring/decimal/const.go generated vendored Normal file
View File

@ -0,0 +1,63 @@
package decimal
import (
"strings"
)
const (
strLn10 = "2.302585092994045684017991454684364207601101488628772976033327900967572609677352480235997205089598298341967784042286248633409525465082806756666287369098781689482907208325554680843799894826233198528393505308965377732628846163366222287698219886746543667474404243274365155048934314939391479619404400222105101714174800368808401264708068556774321622835522011480466371565912137345074785694768346361679210180644507064800027750268491674655058685693567342067058113642922455440575892572420824131469568901675894025677631135691929203337658714166023010570308963457207544037084746994016826928280848118428931484852494864487192780967627127577539702766860595249671667418348570442250719796500471495105049221477656763693866297697952211071826454973477266242570942932258279850258550978526538320760672631716430950599508780752371033310119785754733154142180842754386359177811705430982748238504564801909561029929182431823752535770975053956518769751037497088869218020518933950723853920514463419726528728696511086257149219884997874887377134568620916705849807828059751193854445009978131146915934666241071846692310107598438319191292230792503747298650929009880391941702654416816335727555703151596113564846546190897042819763365836983716328982174407366009162177850541779276367731145041782137660111010731042397832521894898817597921798666394319523936855916447118246753245630912528778330963604262982153040874560927760726641354787576616262926568298704957954913954918049209069438580790032763017941503117866862092408537949861264933479354871737451675809537088281067452440105892444976479686075120275724181874989395971643105518848195288330746699317814634930000321200327765654130472621883970596794457943468343218395304414844803701305753674262153675579814770458031413637793236291560128185336498466942261465206459942072917119370602444929358037007718981097362533224548366988505528285966192805098447175198503666680874970496982273220244823343097169111136813588418696549323714996941979687803008850408979618598756579894836445212043698216415292987811742973332588607915912510967187510929248475023930572665446276200923068791518135803477701295593646298412366497023355174586195564772461857717369368404676577047874319780573853271810933883496338813069945569399346101090745616033312247949360455361849123333063704751724871276379140924398331810164737823379692265637682071706935846394531616949411701841938119405416449466111274712819705817783293841742231409930022911502362192186723337268385688273533371925103412930705632544426611429765388301822384091026198582888433587455960453004548370789052578473166283701953392231047527564998119228742789713715713228319641003422124210082180679525276689858180956119208391760721080919923461516952599099473782780648128058792731993893453415320185969711021407542282796298237068941764740642225757212455392526179373652434440560595336591539160312524480149313234572453879524389036839236450507881731359711238145323701508413491122324390927681724749607955799151363982881058285740538000653371655553014196332241918087621018204919492651483892"
)
var (
ln10 = newConstApproximation(strLn10)
)
type constApproximation struct {
exact Decimal
approximations []Decimal
}
func newConstApproximation(value string) constApproximation {
parts := strings.Split(value, ".")
coeff, fractional := parts[0], parts[1]
coeffLen := len(coeff)
maxPrecision := len(fractional)
var approximations []Decimal
for p := 1; p < maxPrecision; p *= 2 {
r := RequireFromString(value[:coeffLen+p])
approximations = append(approximations, r)
}
return constApproximation{
RequireFromString(value),
approximations,
}
}
// Returns the smallest approximation available that's at least as precise
// as the passed precision (places after decimal point), i.e. Floor[ log2(precision) ] + 1
func (c constApproximation) withPrecision(precision int32) Decimal {
i := 0
if precision >= 1 {
i++
}
for precision >= 16 {
precision /= 16
i += 4
}
for precision >= 2 {
precision /= 2
i++
}
if i >= len(c.approximations) {
return c.exact
}
return c.approximations[i]
}

415
vendor/github.com/shopspring/decimal/decimal-go.go generated vendored Normal file
View File

@ -0,0 +1,415 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Multiprecision decimal numbers.
// For floating-point formatting only; not general purpose.
// Only operations are assign and (binary) left/right shift.
// Can do binary floating point in multiprecision decimal precisely
// because 2 divides 10; cannot do decimal floating point
// in multiprecision binary precisely.
package decimal
type decimal struct {
d [800]byte // digits, big-endian representation
nd int // number of digits used
dp int // decimal point
neg bool // negative flag
trunc bool // discarded nonzero digits beyond d[:nd]
}
func (a *decimal) String() string {
n := 10 + a.nd
if a.dp > 0 {
n += a.dp
}
if a.dp < 0 {
n += -a.dp
}
buf := make([]byte, n)
w := 0
switch {
case a.nd == 0:
return "0"
case a.dp <= 0:
// zeros fill space between decimal point and digits
buf[w] = '0'
w++
buf[w] = '.'
w++
w += digitZero(buf[w : w+-a.dp])
w += copy(buf[w:], a.d[0:a.nd])
case a.dp < a.nd:
// decimal point in middle of digits
w += copy(buf[w:], a.d[0:a.dp])
buf[w] = '.'
w++
w += copy(buf[w:], a.d[a.dp:a.nd])
default:
// zeros fill space between digits and decimal point
w += copy(buf[w:], a.d[0:a.nd])
w += digitZero(buf[w : w+a.dp-a.nd])
}
return string(buf[0:w])
}
func digitZero(dst []byte) int {
for i := range dst {
dst[i] = '0'
}
return len(dst)
}
// trim trailing zeros from number.
// (They are meaningless; the decimal point is tracked
// independent of the number of digits.)
func trim(a *decimal) {
for a.nd > 0 && a.d[a.nd-1] == '0' {
a.nd--
}
if a.nd == 0 {
a.dp = 0
}
}
// Assign v to a.
func (a *decimal) Assign(v uint64) {
var buf [24]byte
// Write reversed decimal in buf.
n := 0
for v > 0 {
v1 := v / 10
v -= 10 * v1
buf[n] = byte(v + '0')
n++
v = v1
}
// Reverse again to produce forward decimal in a.d.
a.nd = 0
for n--; n >= 0; n-- {
a.d[a.nd] = buf[n]
a.nd++
}
a.dp = a.nd
trim(a)
}
// Maximum shift that we can do in one pass without overflow.
// A uint has 32 or 64 bits, and we have to be able to accommodate 9<<k.
const uintSize = 32 << (^uint(0) >> 63)
const maxShift = uintSize - 4
// Binary shift right (/ 2) by k bits. k <= maxShift to avoid overflow.
func rightShift(a *decimal, k uint) {
r := 0 // read pointer
w := 0 // write pointer
// Pick up enough leading digits to cover first shift.
var n uint
for ; n>>k == 0; r++ {
if r >= a.nd {
if n == 0 {
// a == 0; shouldn't get here, but handle anyway.
a.nd = 0
return
}
for n>>k == 0 {
n = n * 10
r++
}
break
}
c := uint(a.d[r])
n = n*10 + c - '0'
}
a.dp -= r - 1
var mask uint = (1 << k) - 1
// Pick up a digit, put down a digit.
for ; r < a.nd; r++ {
c := uint(a.d[r])
dig := n >> k
n &= mask
a.d[w] = byte(dig + '0')
w++
n = n*10 + c - '0'
}
// Put down extra digits.
for n > 0 {
dig := n >> k
n &= mask
if w < len(a.d) {
a.d[w] = byte(dig + '0')
w++
} else if dig > 0 {
a.trunc = true
}
n = n * 10
}
a.nd = w
trim(a)
}
// Cheat sheet for left shift: table indexed by shift count giving
// number of new digits that will be introduced by that shift.
//
// For example, leftcheats[4] = {2, "625"}. That means that
// if we are shifting by 4 (multiplying by 16), it will add 2 digits
// when the string prefix is "625" through "999", and one fewer digit
// if the string prefix is "000" through "624".
//
// Credit for this trick goes to Ken.
type leftCheat struct {
delta int // number of new digits
cutoff string // minus one digit if original < a.
}
var leftcheats = []leftCheat{
// Leading digits of 1/2^i = 5^i.
// 5^23 is not an exact 64-bit floating point number,
// so have to use bc for the math.
// Go up to 60 to be large enough for 32bit and 64bit platforms.
/*
seq 60 | sed 's/^/5^/' | bc |
awk 'BEGIN{ print "\t{ 0, \"\" }," }
{
log2 = log(2)/log(10)
printf("\t{ %d, \"%s\" },\t// * %d\n",
int(log2*NR+1), $0, 2**NR)
}'
*/
{0, ""},
{1, "5"}, // * 2
{1, "25"}, // * 4
{1, "125"}, // * 8
{2, "625"}, // * 16
{2, "3125"}, // * 32
{2, "15625"}, // * 64
{3, "78125"}, // * 128
{3, "390625"}, // * 256
{3, "1953125"}, // * 512
{4, "9765625"}, // * 1024
{4, "48828125"}, // * 2048
{4, "244140625"}, // * 4096
{4, "1220703125"}, // * 8192
{5, "6103515625"}, // * 16384
{5, "30517578125"}, // * 32768
{5, "152587890625"}, // * 65536
{6, "762939453125"}, // * 131072
{6, "3814697265625"}, // * 262144
{6, "19073486328125"}, // * 524288
{7, "95367431640625"}, // * 1048576
{7, "476837158203125"}, // * 2097152
{7, "2384185791015625"}, // * 4194304
{7, "11920928955078125"}, // * 8388608
{8, "59604644775390625"}, // * 16777216
{8, "298023223876953125"}, // * 33554432
{8, "1490116119384765625"}, // * 67108864
{9, "7450580596923828125"}, // * 134217728
{9, "37252902984619140625"}, // * 268435456
{9, "186264514923095703125"}, // * 536870912
{10, "931322574615478515625"}, // * 1073741824
{10, "4656612873077392578125"}, // * 2147483648
{10, "23283064365386962890625"}, // * 4294967296
{10, "116415321826934814453125"}, // * 8589934592
{11, "582076609134674072265625"}, // * 17179869184
{11, "2910383045673370361328125"}, // * 34359738368
{11, "14551915228366851806640625"}, // * 68719476736
{12, "72759576141834259033203125"}, // * 137438953472
{12, "363797880709171295166015625"}, // * 274877906944
{12, "1818989403545856475830078125"}, // * 549755813888
{13, "9094947017729282379150390625"}, // * 1099511627776
{13, "45474735088646411895751953125"}, // * 2199023255552
{13, "227373675443232059478759765625"}, // * 4398046511104
{13, "1136868377216160297393798828125"}, // * 8796093022208
{14, "5684341886080801486968994140625"}, // * 17592186044416
{14, "28421709430404007434844970703125"}, // * 35184372088832
{14, "142108547152020037174224853515625"}, // * 70368744177664
{15, "710542735760100185871124267578125"}, // * 140737488355328
{15, "3552713678800500929355621337890625"}, // * 281474976710656
{15, "17763568394002504646778106689453125"}, // * 562949953421312
{16, "88817841970012523233890533447265625"}, // * 1125899906842624
{16, "444089209850062616169452667236328125"}, // * 2251799813685248
{16, "2220446049250313080847263336181640625"}, // * 4503599627370496
{16, "11102230246251565404236316680908203125"}, // * 9007199254740992
{17, "55511151231257827021181583404541015625"}, // * 18014398509481984
{17, "277555756156289135105907917022705078125"}, // * 36028797018963968
{17, "1387778780781445675529539585113525390625"}, // * 72057594037927936
{18, "6938893903907228377647697925567626953125"}, // * 144115188075855872
{18, "34694469519536141888238489627838134765625"}, // * 288230376151711744
{18, "173472347597680709441192448139190673828125"}, // * 576460752303423488
{19, "867361737988403547205962240695953369140625"}, // * 1152921504606846976
}
// Is the leading prefix of b lexicographically less than s?
func prefixIsLessThan(b []byte, s string) bool {
for i := 0; i < len(s); i++ {
if i >= len(b) {
return true
}
if b[i] != s[i] {
return b[i] < s[i]
}
}
return false
}
// Binary shift left (* 2) by k bits. k <= maxShift to avoid overflow.
func leftShift(a *decimal, k uint) {
delta := leftcheats[k].delta
if prefixIsLessThan(a.d[0:a.nd], leftcheats[k].cutoff) {
delta--
}
r := a.nd // read index
w := a.nd + delta // write index
// Pick up a digit, put down a digit.
var n uint
for r--; r >= 0; r-- {
n += (uint(a.d[r]) - '0') << k
quo := n / 10
rem := n - 10*quo
w--
if w < len(a.d) {
a.d[w] = byte(rem + '0')
} else if rem != 0 {
a.trunc = true
}
n = quo
}
// Put down extra digits.
for n > 0 {
quo := n / 10
rem := n - 10*quo
w--
if w < len(a.d) {
a.d[w] = byte(rem + '0')
} else if rem != 0 {
a.trunc = true
}
n = quo
}
a.nd += delta
if a.nd >= len(a.d) {
a.nd = len(a.d)
}
a.dp += delta
trim(a)
}
// Binary shift left (k > 0) or right (k < 0).
func (a *decimal) Shift(k int) {
switch {
case a.nd == 0:
// nothing to do: a == 0
case k > 0:
for k > maxShift {
leftShift(a, maxShift)
k -= maxShift
}
leftShift(a, uint(k))
case k < 0:
for k < -maxShift {
rightShift(a, maxShift)
k += maxShift
}
rightShift(a, uint(-k))
}
}
// If we chop a at nd digits, should we round up?
func shouldRoundUp(a *decimal, nd int) bool {
if nd < 0 || nd >= a.nd {
return false
}
if a.d[nd] == '5' && nd+1 == a.nd { // exactly halfway - round to even
// if we truncated, a little higher than what's recorded - always round up
if a.trunc {
return true
}
return nd > 0 && (a.d[nd-1]-'0')%2 != 0
}
// not halfway - digit tells all
return a.d[nd] >= '5'
}
// Round a to nd digits (or fewer).
// If nd is zero, it means we're rounding
// just to the left of the digits, as in
// 0.09 -> 0.1.
func (a *decimal) Round(nd int) {
if nd < 0 || nd >= a.nd {
return
}
if shouldRoundUp(a, nd) {
a.RoundUp(nd)
} else {
a.RoundDown(nd)
}
}
// Round a down to nd digits (or fewer).
func (a *decimal) RoundDown(nd int) {
if nd < 0 || nd >= a.nd {
return
}
a.nd = nd
trim(a)
}
// Round a up to nd digits (or fewer).
func (a *decimal) RoundUp(nd int) {
if nd < 0 || nd >= a.nd {
return
}
// round up
for i := nd - 1; i >= 0; i-- {
c := a.d[i]
if c < '9' { // can stop after this digit
a.d[i]++
a.nd = i + 1
return
}
}
// Number is all 9s.
// Change to single 1 with adjusted decimal point.
a.d[0] = '1'
a.nd = 1
a.dp++
}
// Extract integer part, rounded appropriately.
// No guarantees about overflow.
func (a *decimal) RoundedInteger() uint64 {
if a.dp > 20 {
return 0xFFFFFFFFFFFFFFFF
}
var i int
n := uint64(0)
for i = 0; i < a.dp && i < a.nd; i++ {
n = n*10 + uint64(a.d[i]-'0')
}
for ; i < a.dp; i++ {
n *= 10
}
if shouldRoundUp(a, a.dp) {
n++
}
return n
}

2339
vendor/github.com/shopspring/decimal/decimal.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

160
vendor/github.com/shopspring/decimal/rounding.go generated vendored Normal file
View File

@ -0,0 +1,160 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Multiprecision decimal numbers.
// For floating-point formatting only; not general purpose.
// Only operations are assign and (binary) left/right shift.
// Can do binary floating point in multiprecision decimal precisely
// because 2 divides 10; cannot do decimal floating point
// in multiprecision binary precisely.
package decimal
type floatInfo struct {
mantbits uint
expbits uint
bias int
}
var float32info = floatInfo{23, 8, -127}
var float64info = floatInfo{52, 11, -1023}
// roundShortest rounds d (= mant * 2^exp) to the shortest number of digits
// that will let the original floating point value be precisely reconstructed.
func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) {
// If mantissa is zero, the number is zero; stop now.
if mant == 0 {
d.nd = 0
return
}
// Compute upper and lower such that any decimal number
// between upper and lower (possibly inclusive)
// will round to the original floating point number.
// We may see at once that the number is already shortest.
//
// Suppose d is not denormal, so that 2^exp <= d < 10^dp.
// The closest shorter number is at least 10^(dp-nd) away.
// The lower/upper bounds computed below are at distance
// at most 2^(exp-mantbits).
//
// So the number is already shortest if 10^(dp-nd) > 2^(exp-mantbits),
// or equivalently log2(10)*(dp-nd) > exp-mantbits.
// It is true if 332/100*(dp-nd) >= exp-mantbits (log2(10) > 3.32).
minexp := flt.bias + 1 // minimum possible exponent
if exp > minexp && 332*(d.dp-d.nd) >= 100*(exp-int(flt.mantbits)) {
// The number is already shortest.
return
}
// d = mant << (exp - mantbits)
// Next highest floating point number is mant+1 << exp-mantbits.
// Our upper bound is halfway between, mant*2+1 << exp-mantbits-1.
upper := new(decimal)
upper.Assign(mant*2 + 1)
upper.Shift(exp - int(flt.mantbits) - 1)
// d = mant << (exp - mantbits)
// Next lowest floating point number is mant-1 << exp-mantbits,
// unless mant-1 drops the significant bit and exp is not the minimum exp,
// in which case the next lowest is mant*2-1 << exp-mantbits-1.
// Either way, call it mantlo << explo-mantbits.
// Our lower bound is halfway between, mantlo*2+1 << explo-mantbits-1.
var mantlo uint64
var explo int
if mant > 1<<flt.mantbits || exp == minexp {
mantlo = mant - 1
explo = exp
} else {
mantlo = mant*2 - 1
explo = exp - 1
}
lower := new(decimal)
lower.Assign(mantlo*2 + 1)
lower.Shift(explo - int(flt.mantbits) - 1)
// The upper and lower bounds are possible outputs only if
// the original mantissa is even, so that IEEE round-to-even
// would round to the original mantissa and not the neighbors.
inclusive := mant%2 == 0
// As we walk the digits we want to know whether rounding up would fall
// within the upper bound. This is tracked by upperdelta:
//
// If upperdelta == 0, the digits of d and upper are the same so far.
//
// If upperdelta == 1, we saw a difference of 1 between d and upper on a
// previous digit and subsequently only 9s for d and 0s for upper.
// (Thus rounding up may fall outside the bound, if it is exclusive.)
//
// If upperdelta == 2, then the difference is greater than 1
// and we know that rounding up falls within the bound.
var upperdelta uint8
// Now we can figure out the minimum number of digits required.
// Walk along until d has distinguished itself from upper and lower.
for ui := 0; ; ui++ {
// lower, d, and upper may have the decimal points at different
// places. In this case upper is the longest, so we iterate from
// ui==0 and start li and mi at (possibly) -1.
mi := ui - upper.dp + d.dp
if mi >= d.nd {
break
}
li := ui - upper.dp + lower.dp
l := byte('0') // lower digit
if li >= 0 && li < lower.nd {
l = lower.d[li]
}
m := byte('0') // middle digit
if mi >= 0 {
m = d.d[mi]
}
u := byte('0') // upper digit
if ui < upper.nd {
u = upper.d[ui]
}
// Okay to round down (truncate) if lower has a different digit
// or if lower is inclusive and is exactly the result of rounding
// down (i.e., and we have reached the final digit of lower).
okdown := l != m || inclusive && li+1 == lower.nd
switch {
case upperdelta == 0 && m+1 < u:
// Example:
// m = 12345xxx
// u = 12347xxx
upperdelta = 2
case upperdelta == 0 && m != u:
// Example:
// m = 12345xxx
// u = 12346xxx
upperdelta = 1
case upperdelta == 1 && (m != '9' || u != '0'):
// Example:
// m = 1234598x
// u = 1234600x
upperdelta = 2
}
// Okay to round up if upper has a different digit and either upper
// is inclusive or upper is bigger than the result of rounding up.
okup := upperdelta > 0 && (inclusive || upperdelta > 1 || ui+1 < upper.nd)
// If it's okay to do either, then round to the nearest one.
// If it's okay to do only one, do it.
switch {
case okdown && okup:
d.Round(mi + 1)
return
case okdown:
d.RoundDown(mi + 1)
return
case okup:
d.RoundUp(mi + 1)
return
}
}
}

3
vendor/modules.txt vendored
View File

@ -286,6 +286,9 @@ github.com/redis/go-redis/v9/push
## explicit; go 1.24.0
github.com/rubenv/sql-migrate
github.com/rubenv/sql-migrate/sqlparse
# github.com/shopspring/decimal v1.4.0
## explicit; go 1.10
github.com/shopspring/decimal
# github.com/sirupsen/logrus v1.9.3
## explicit; go 1.13
github.com/sirupsen/logrus

View File

@ -2,14 +2,14 @@ package walletapp
import (
"git.gocasts.ir/ebhomengo/niki/adapter/redis"
"git.gocasts.ir/ebhomengo/niki/domain/shoppingbasket/repository"
"git.gocasts.ir/ebhomengo/niki/domain/wallet/repository/postgres"
"git.gocasts.ir/ebhomengo/niki/pkg/httpserver"
logger "git.gocasts.ir/ebhomengo/niki/pkg/logger"
)
type Config struct {
Redis redis.Config `koanf:"redis" json:"redis"`
Repo repository.Config `koanf:"repo" json:"repo"`
Repo postgres.Config `koanf:"repo" json:"repo"`
HTTPServer httpserver.Config `koanf:"http_server" json:"http_server"`
Logger logger.Config `koanf:"logger" json:"logger"`
}