common

package module
v0.0.19-dev Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 10 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckPassword

func CheckPassword(hashedPassword, plainPassword string) error

CheckPassword compares plain vs hashed

func GetContextRequestID

func GetContextRequestID(ctx context.Context) string

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes the plain password

func RandomCode

func RandomCode(n int, option RandomCodeOption, separator ...any) string

RandomCode generates a random code with flexible separator/grouping. n: total character count (not including separators) option: character set type separator: []any{string separator, int groupSize} (optional; groupSize default 4)

func Rounder

func Rounder(num float64, intermed float64, precision int) float64

Rounder to round float number with some intermed and precision decimal e.g: 2.558--->2.6 Rounder(2.558, 0.5, 1)--->2.6 (intermed is a number that determine range of round ceil and round floor can accept negatif number from:https://play.golang.org/p/KNhgeuU5sT

func ValidTokenPermission

func ValidTokenPermission(ctx context.Context, perm string) bool

Types

type BaseRepositoryInterface

type BaseRepositoryInterface[T any] interface {
	WithContext(ctx context.Context) BaseRepositoryInterface[T]
	Insert(entity *T) error
	FindByID(id any) (*T, error)
	Update(entity *T, fields ...string) error
	SoftDelete(id any) error
}

BaseRepositoryInterface defines common repository behaviors for all datastores.

type BaseUsecase

type BaseUsecase[T any] struct {
	Repo    BaseRepositoryInterface[T]
	Context context.Context
}

func NewBaseUsecase

func NewBaseUsecase[T any](repo BaseRepositoryInterface[T]) *BaseUsecase[T]

func (*BaseUsecase[T]) Create

func (u *BaseUsecase[T]) Create(entity *T) error

func (*BaseUsecase[T]) Delete

func (u *BaseUsecase[T]) Delete(id any) error

func (*BaseUsecase[T]) GetByID

func (u *BaseUsecase[T]) GetByID(id any) (*T, error)

func (*BaseUsecase[T]) Update

func (u *BaseUsecase[T]) Update(entity *T, fields ...string) error

func (*BaseUsecase[T]) WithContext

func (u *BaseUsecase[T]) WithContext(ctx context.Context) *BaseUsecase[T]

type ContextKey

type ContextKey string
const (
	ContextRequestIDKey        ContextKey = "request_id"
	ContextUserKey             ContextKey = "user"
	ContextSessionKey          ContextKey = "session"
	ContextCorrelationIDKey    ContextKey = "correlation_id"
	ContextLocaleKey           ContextKey = "locale"
	ContextClientIPKey         ContextKey = "client_ip"
	ContextRequestStartTimeKey ContextKey = "request_start_time"
	ContextTraceIDKey          ContextKey = "trace_id"
	ContextSpanIDKey           ContextKey = "span_id"
)

type QueryOption

type QueryOption struct {
	Limit      int64    `query:"limit"`
	Page       int64    `query:"page"`
	Search     string   `query:"search"`
	OrderBy    string   `query:"order_by"`
	Offset     int64    `query:"-"`
	Orders     []string `query:"-"`
	Conditions []any    `query:"-"`
}

func (*QueryOption) BuildOption

func (r *QueryOption) BuildOption() *QueryOption

func (*QueryOption) GetLimit

func (r *QueryOption) GetLimit() int64

func (*QueryOption) GetOffset

func (r *QueryOption) GetOffset() int64

func (*QueryOption) GetOrders

func (r *QueryOption) GetOrders() []string

func (*QueryOption) GetPage

func (r *QueryOption) GetPage() int64

func (*QueryOption) GetSearch

func (r *QueryOption) GetSearch() string

type RandomCodeOption

type RandomCodeOption int

RandomCodeOption lets you customize the type of random code generated.

const (
	RandomCodeAlpha        RandomCodeOption = iota // Letters only (A-Z)
	RandomCodeNumeric                              // Numbers only (0-9)
	RandomCodeAlphaNumeric                         // Letters (A-Z) + numbers (0-9)
)

type SessionClaims

type SessionClaims struct {
	UserID      string   `json:"user_id"`
	Username    string   `json:"username"`
	DisplayName string   `json:"display_name"`
	Email       string   `json:"email"`
	Permissions []string `json:"permission"`
	Type        string   `json:"type"`

	jwt.RegisteredClaims
}

func GetContextSession

func GetContextSession(ctx context.Context) *SessionClaims

func GetSession

func GetSession(ctx context.Context) (*SessionClaims, error)

func TokenDecode

func TokenDecode(tokenStr string) (*SessionClaims, error)

func (*SessionClaims) Encode

func (c *SessionClaims) Encode(expiry time.Duration) (string, error)

type TokenPair

type TokenPair struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token,omitempty"`
}

func TokenEncode

func TokenEncode(claim *SessionClaims) (*TokenPair, error)

Jump to

Keyboard shortcuts

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