utils

package
v0.0.0-...-911e024 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package utils provides utility functions for internal use.

This file was generated by setup_project.sh script.

Index

Constants

View Source
const (
	RuntimeENVDev     = "dev"
	RuntimeENVLocal   = "local"
	RuntimeENVStaging = "staging"
	RuntimeENVProd    = "prod"
)
View Source
const ENVKey = "MEMORIA_NEXUS_ENV"

Variables

View Source
var (
	Zero = UInt64(0)
)

Functions

func Clamp

func Clamp[T constraints.Ordered](v, x, y T) T

func Env

func Env() string

func GinHandleError

func GinHandleError(c *gin.Context, log logrus.FieldLogger, status int, err error, msg string, options ...GinHandleErrorOption)

GinHandleError handles errors by logging them and sending a ErrorResponse.

func GinMWParseID

func GinMWParseID() gin.HandlerFunc

func GinMWParseTAG

func GinMWParseTAG() gin.HandlerFunc

func GinMustGetTAG

func GinMustGetTAG(c *gin.Context) (tag string)

GinMustGetTAG should be used with GinMWParseID

func ParallelBatchProcess

func ParallelBatchProcess(ctx context.Context, total int, processor BatchProcessor, opts ...Option) error

ParallelBatchProcess processes tasks in parallel batches with specified options.

Types

type BatchProcessor

type BatchProcessor func(ctx context.Context, start, end int) error

BatchProcessor defines a function to process a batch of tasks.

type ErrorResponse

type ErrorResponse struct {
	Message string `json:"message"`
	Error   string `json:"error,omitempty"`
}

ErrorResponse defines the standard error response structure.

type GinHandleErrorOption

type GinHandleErrorOption func(*ginHandleErrorOptions)

GinHandleErrorOption defines the type for functional options.

func GinErrWithExtra

func GinErrWithExtra(key string, val any) GinHandleErrorOption

GinErrWithExtra sets the requestContents option.

func GinErrWithReqBody

func GinErrWithReqBody(req any) GinHandleErrorOption

GinErrWithReqBody sets the requestBody

func GinErrWithReqContents

func GinErrWithReqContents(params map[string]any) GinHandleErrorOption

GinErrWithReqContents sets the requestContents option.

type Option

type Option func(*ParallelOptions)

Option defines a function to set options for parallel batch processing.

func BatchWithMaxGoroutines

func BatchWithMaxGoroutines(max int) Option

BatchWithMaxGoroutines sets the maximum number of concurrent goroutines.

func BatchWithSize

func BatchWithSize(size int) Option

BatchWithSize sets the size for each batch.

func BatchWithTimeout

func BatchWithTimeout(timeout time.Duration) Option

BatchWithTimeout sets a timeout duration for batch processing.

type Pager

type Pager struct {
	Offset int   `json:"offset"`
	Limit  int   `json:"limit"`
	Total  int64 `json:"total,omitempty"`
}

func GinGetPagerFromQuery

func GinGetPagerFromQuery(c *gin.Context) (pager *Pager)

func (*Pager) Page

func (resp *Pager) Page() int

Page start from 1

func (*Pager) SetFirstCount

func (resp *Pager) SetFirstCount(count int) *Pager

func (*Pager) SetOffsetAndLimit

func (resp *Pager) SetOffsetAndLimit(offset int, limit int) *Pager

func (*Pager) SetPageAndLimit

func (resp *Pager) SetPageAndLimit(page, limit int) *Pager

func (*Pager) SetTotal

func (resp *Pager) SetTotal(total int64) *Pager

func (*Pager) String

func (resp *Pager) String() string

type ParallelOptions

type ParallelOptions struct {
	MaxGoroutines int
	Timeout       time.Duration
	BatchSize     int
}

ParallelOptions defines options for parallel batch processing.

type Percentage

type Percentage uint8

Percentage is a custom type to handle JSON serialization of percentages as uint8.

func ParsePercentageFromString

func ParsePercentageFromString(value string) (Percentage, error)

ParsePercentageFromString supports parsing from a string value directly.

func Percentage100

func Percentage100() Percentage

func (Percentage) Clamp0100

func (p Percentage) Clamp0100() Percentage

func (*Percentage) FromNormalizedFloat

func (p *Percentage) FromNormalizedFloat(v float64)

func (*Percentage) MarshalJSON

func (p *Percentage) MarshalJSON() ([]byte, error)

MarshalJSON serializes the Percentage as a string to avoid precision loss in JavaScript.

func (Percentage) NormalizedFloat

func (p Percentage) NormalizedFloat() float64

func (Percentage) Raw

func (p Percentage) Raw() uint8

Raw returns the raw uint8 value of the percentage.

func (*Percentage) Times

func (p *Percentage) Times(v float64) float64

func (*Percentage) UnmarshalJSON

func (p *Percentage) UnmarshalJSON(b []byte) error

UnmarshalJSON supports parsing the Percentage from a number or a string in JSON.

type UInt64

type UInt64 uint64

UInt64 is a custom type to handle JSON serialization of 64-bit integers.

func GenIDU64

func GenIDU64(ctx context.Context) (UInt64, error)

func GinMustGetID

func GinMustGetID(c *gin.Context) (id UInt64)

GinMustGetID should be used with GinMWParseID

func GinMustGetUserID

func GinMustGetUserID(c *gin.Context) (id UInt64)

func MGenIDU64

func MGenIDU64(ctx context.Context, count int) ([]UInt64, error)

func ParseIDFromString

func ParseIDFromString(value string) (UInt64, error)

ParseIDFromString Support parsing from a string value directly.

func (*UInt64) MarshalJSON

func (val *UInt64) MarshalJSON() ([]byte, error)

MarshalJSON serializes the UInt64 as a string to avoid precision loss in JavaScript.

func (UInt64) Raw

func (val UInt64) Raw() uint64

func (*UInt64) Scan

func (val *UInt64) Scan(value any) error

Scan implements the sql.Scanner interface.

func (*UInt64) UnmarshalJSON

func (val *UInt64) UnmarshalJSON(b []byte) error

UnmarshalJSON supports parsing the UInt64 from a number or a string in JSON.

func (UInt64) Value

func (val UInt64) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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