libRequest

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package libRequest provides request parsing and validation utilities.

Index

Constants

View Source
const ErrorInGetRequest = "ERROR_IN_GET_REQUEST_%s"

ErrorInGetRequest is the error description template for request parsing failures.

View Source
const ModuleName = "RequestHandler"

ModuleName is the module name used for request handler DML operations.

View Source
const PaginationLocalTag string = "pagination"

PaginationLocalTag is the local-storage key for pagination data.

Variables

This section is empty.

Functions

func InitGlobalHeaderConfig added in v0.18.0

func InitGlobalHeaderConfig(configPath string) error

InitGlobalHeaderConfig initializes the global header configuration

func TypeStrings added in v0.5.7

func TypeStrings() []string

TypeStrings returns a slice of all String values of the enum

Types

type DynamicHeaderConfig added in v0.18.0

type DynamicHeaderConfig struct {
	// Required headers that must be present
	RequiredHeaders map[string]HeaderConfig `yaml:"required"`
	// Optional headers that can be present
	OptionalHeaders map[string]HeaderConfig `yaml:"optional"`
	// Custom headers that can be dynamically added
	CustomHeaders map[string]HeaderConfig `yaml:"custom"`
}

DynamicHeaderConfig defines the configuration for dynamic headers

func GetGlobalHeaderConfig added in v0.18.0

func GetGlobalHeaderConfig() *DynamicHeaderConfig

GetGlobalHeaderConfig returns the global header configuration

type DynamicRequestHeader added in v0.18.0

type DynamicRequestHeader struct {
	// Core headers that are always present
	RequestID string `header:"Request-Id" reqHeader:"Request-Id" validate:"required,min=10,max=64"`
	Program   string `header:"Program-Id" reqHeader:"Program-Id"`
	Module    string `header:"Module-Id"  reqHeader:"Module-Id"`
	Method    string `header:"Method-Id"  reqHeader:"Method-Id"`
	User      string `header:"User-Id"    reqHeader:"User-Id"`

	// Dynamic headers stored as key-value pairs
	DynamicHeaders map[string]string `json:"dynamicHeaders"`

	// Configuration for header validation
	Config *DynamicHeaderConfig `json:"-"`
}

DynamicRequestHeader represents a dynamic header structure

func CreateDynamicRequestHeader added in v0.18.0

func CreateDynamicRequestHeader() *DynamicRequestHeader

CreateDynamicRequestHeader creates a new dynamic request header with global configuration

func NewDynamicRequestHeader added in v0.18.0

func NewDynamicRequestHeader(config *DynamicHeaderConfig) *DynamicRequestHeader

NewDynamicRequestHeader creates a new dynamic request header with configuration

func (DynamicRequestHeader) GetBank added in v0.18.0

func (r DynamicRequestHeader) GetBank() string

GetBank returns the bank ID from the dynamic header with default fallback.

func (DynamicRequestHeader) GetBranch added in v0.18.0

func (r DynamicRequestHeader) GetBranch() string

GetBranch returns the branch ID from the dynamic header with default fallback.

func (*DynamicRequestHeader) GetDynamicHeader added in v0.18.0

func (r *DynamicRequestHeader) GetDynamicHeader(key string) string

GetDynamicHeader gets a dynamic header value

func (*DynamicRequestHeader) GetDynamicHeaderWithDefault added in v0.18.0

func (r *DynamicRequestHeader) GetDynamicHeaderWithDefault(key string) string

GetDynamicHeaderWithDefault gets a dynamic header value with default fallback

func (*DynamicRequestHeader) GetHeaderMap added in v0.18.0

func (r *DynamicRequestHeader) GetHeaderMap() map[string]string

GetHeaderMap returns all headers as a map for easy access

func (DynamicRequestHeader) GetID added in v0.28.1

func (r DynamicRequestHeader) GetID() string

GetID returns the request ID from the dynamic header.

func (DynamicRequestHeader) GetMethod added in v0.18.0

func (r DynamicRequestHeader) GetMethod() string

GetMethod returns the method ID from the dynamic header.

func (DynamicRequestHeader) GetModule added in v0.18.0

func (r DynamicRequestHeader) GetModule() string

GetModule returns the module ID from the dynamic header.

func (DynamicRequestHeader) GetPerson added in v0.18.0

func (r DynamicRequestHeader) GetPerson() string

GetPerson returns the person ID from the dynamic header with default fallback.

func (DynamicRequestHeader) GetProgram added in v0.18.0

func (r DynamicRequestHeader) GetProgram() string

GetProgram returns the program ID from the dynamic header.

func (DynamicRequestHeader) GetUser added in v0.18.0

func (r DynamicRequestHeader) GetUser() string

GetUser returns the user ID from the dynamic header.

func (*DynamicRequestHeader) SetBank added in v0.18.0

func (r *DynamicRequestHeader) SetBank(bank string)

SetBank sets the bank ID as a dynamic header.

func (*DynamicRequestHeader) SetBranch added in v0.18.0

func (r *DynamicRequestHeader) SetBranch(branch string)

SetBranch sets the branch ID as a dynamic header.

func (*DynamicRequestHeader) SetDynamicHeader added in v0.18.0

func (r *DynamicRequestHeader) SetDynamicHeader(key, value string)

SetDynamicHeader sets a dynamic header value

func (*DynamicRequestHeader) SetMethod added in v0.18.0

func (r *DynamicRequestHeader) SetMethod(method string)

SetMethod sets the method ID on the dynamic header.

func (*DynamicRequestHeader) SetModule added in v0.18.0

func (r *DynamicRequestHeader) SetModule(module string)

SetModule sets the module ID on the dynamic header.

func (*DynamicRequestHeader) SetPerson added in v0.18.0

func (r *DynamicRequestHeader) SetPerson(person string)

SetPerson sets the person ID as a dynamic header.

func (*DynamicRequestHeader) SetProgram added in v0.18.0

func (r *DynamicRequestHeader) SetProgram(program string)

SetProgram sets the program ID on the dynamic header.

func (*DynamicRequestHeader) SetUser added in v0.18.0

func (r *DynamicRequestHeader) SetUser(user string)

SetUser sets the user ID on the dynamic header.

func (*DynamicRequestHeader) ValidateDynamicHeaders added in v0.18.0

func (r *DynamicRequestHeader) ValidateDynamicHeaders() error

ValidateDynamicHeaders validates all dynamic headers against their configuration

type EventData

type EventData struct {
	Time time.Time         `json:"dt"`
	Tags map[string]string `json:"tags"`
	Logs []LogData         `json:"logs"`
}

EventData holds a collection of log entries with associated tags.

type HeaderConfig added in v0.18.0

type HeaderConfig struct {
	HeaderName     string `yaml:"headerName"`     // The actual HTTP header name
	Required       bool   `yaml:"required"`       // Whether this header is required
	MinLength      int    `yaml:"minLength"`      // Minimum length validation
	MaxLength      int    `yaml:"maxLength"`      // Maximum length validation
	DefaultValue   string `yaml:"defaultValue"`   // Default value if not provided
	ValidationRule string `yaml:"validationRule"` // Custom validation rule
	Description    string `yaml:"description"`    // Description for documentation
}

HeaderConfig defines the configuration for a single header

type HeaderConfigLoader added in v0.18.0

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

HeaderConfigLoader handles loading and managing dynamic header configurations

func NewHeaderConfigLoader added in v0.18.0

func NewHeaderConfigLoader(configPath string) *HeaderConfigLoader

NewHeaderConfigLoader creates a new header configuration loader

func (*HeaderConfigLoader) GetConfig added in v0.18.0

func (h *HeaderConfigLoader) GetConfig() *DynamicHeaderConfig

GetConfig returns the loaded configuration

func (*HeaderConfigLoader) GetHeaderConfig added in v0.18.0

func (h *HeaderConfigLoader) GetHeaderConfig(headerName string) (HeaderConfig, bool)

GetHeaderConfig returns the configuration for a specific header

func (*HeaderConfigLoader) GetHeaderNames added in v0.18.0

func (h *HeaderConfigLoader) GetHeaderNames() []string

GetHeaderNames returns all configured header names

func (*HeaderConfigLoader) IsHeaderRequired added in v0.18.0

func (h *HeaderConfigLoader) IsHeaderRequired(headerName string) bool

IsHeaderRequired checks if a header is required

func (*HeaderConfigLoader) LoadConfig added in v0.18.0

func (h *HeaderConfigLoader) LoadConfig() error

LoadConfig loads the header configuration from the specified file

func (*HeaderConfigLoader) LoadConfigFromEnv added in v0.18.0

func (h *HeaderConfigLoader) LoadConfigFromEnv() error

LoadConfigFromEnv loads configuration from environment variables

func (*HeaderConfigLoader) ValidateConfig added in v0.18.0

func (h *HeaderConfigLoader) ValidateConfig() error

ValidateConfig validates the loaded configuration

type HeaderMigration added in v0.18.0

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

HeaderMigration provides utilities for migrating from fixed headers to dynamic headers

func GetGlobalMigration added in v0.18.0

func GetGlobalMigration() *HeaderMigration

GetGlobalMigration returns the global migration instance

func NewHeaderMigration added in v0.18.0

func NewHeaderMigration() *HeaderMigration

NewHeaderMigration creates a new header migration utility

func (*HeaderMigration) AddLegacyMapping added in v0.18.0

func (m *HeaderMigration) AddLegacyMapping(legacyName, newName string)

AddLegacyMapping adds a new legacy mapping

func (*HeaderMigration) ConvertLegacyHeaderName added in v0.18.0

func (m *HeaderMigration) ConvertLegacyHeaderName(legacyName string) string

ConvertLegacyHeaderName converts legacy header names to new format

func (*HeaderMigration) CreateDynamicHeaderFromMap added in v0.18.0

func (m *HeaderMigration) CreateDynamicHeaderFromMap(headers map[string]string) *DynamicRequestHeader

CreateDynamicHeaderFromMap creates a DynamicRequestHeader from a map of headers

func (*HeaderMigration) GetHeaderDifferences added in v0.18.0

func (m *HeaderMigration) GetHeaderDifferences(header1, header2 interface{}) []string

GetHeaderDifferences compares two header structures and returns differences

func (*HeaderMigration) GetLegacyMappings added in v0.18.0

func (m *HeaderMigration) GetLegacyMappings() map[string]string

GetLegacyMappings returns all legacy mappings

func (*HeaderMigration) MigrateFromLegacyHeader added in v0.18.0

func (m *HeaderMigration) MigrateFromLegacyHeader(legacy *RequestHeader) *DynamicRequestHeader

MigrateFromLegacyHeader migrates a legacy RequestHeader to DynamicRequestHeader

func (*HeaderMigration) MigrateToLegacyHeader added in v0.18.0

func (m *HeaderMigration) MigrateToLegacyHeader(dynamic *DynamicRequestHeader) *RequestHeader

MigrateToLegacyHeader migrates a DynamicRequestHeader to legacy RequestHeader

func (*HeaderMigration) ValidateMigration added in v0.18.0

func (m *HeaderMigration) ValidateMigration(legacy *RequestHeader, dynamic *DynamicRequestHeader) error

ValidateMigration validates that a migration was successful

type LogData

type LogData struct {
	Time    time.Time `json:"dt"`
	Program string    `json:"program"`
	Module  string    `json:"module"`
	Method  string    `json:"method"`
	LogText string    `json:"log_text"`
}

LogData holds a single log entry with timestamp and context metadata.

type LogRequest added in v0.11.19

type LogRequest struct {
}

LogRequest is a no-op RequestInterface implementation that logs requests via slog.

func (LogRequest) CheckDuplicateRequest added in v0.11.19

func (l LogRequest) CheckDuplicateRequest(_ RequestPtr) error

CheckDuplicateRequest is a no-op that always returns nil.

func (LogRequest) InitRequest added in v0.11.19

func (l LogRequest) InitRequest(_ webFramework.WebFramework, _, _ string) error

InitRequest is a no-op that always returns nil.

func (LogRequest) Initialize added in v0.11.19

func (l LogRequest) Initialize(_ webFramework.WebFramework, _, url string, _ RequestPtr, _ ...any) (int, map[string]string, error)

Initialize returns the URL path without performing any request initialization.

func (LogRequest) InitializeNoLog added in v0.11.19

func (l LogRequest) InitializeNoLog(_ webFramework.WebFramework, _, url string, _ RequestPtr, _ ...any) (int, map[string]string, error)

InitializeNoLog returns the URL path without performing any request initialization.

func (LogRequest) InsertRequest added in v0.11.19

func (l LogRequest) InsertRequest(req RequestPtr) error

InsertRequest logs the request via slog.

func (LogRequest) UpdateRequestWithContext added in v0.11.19

func (l LogRequest) UpdateRequestWithContext(_ context.Context, req RequestPtr) error

UpdateRequestWithContext logs the request completion via slog.

type PaginationData added in v0.10.29

type PaginationData struct {
	Start   int    `form:"_start" query:"_start" validate:"omitempty"`
	End     int    `form:"_end" query:"_end" validate:"omitempty"`
	Filters string `form:"_filters" query:"_filters" validate:"omitempty"`
	Sort    string `form:"_sort" query:"_sort" validate:"omitempty"`
	Order   string `form:"_order" query:"_order" validate:"omitempty,oneof=asc desc"`
}

PaginationData holds pagination parameters for query requests.

type ParseParams added in v0.15.0

type ParseParams struct {
	W              webFramework.WebFramework
	Mode           Type
	ValidateHeader bool
	Header         webFramework.HeaderInterface
	Name           string
	StoreTags      []string
	StoreHeaders   []string
}

ParseParams holds parameters for parsing a request.

type ParseResult added in v0.15.0

type ParseResult[Req any] struct {
	Request    Req
	RequestPtr RequestPtr
}

ParseResult holds the parsed request and its request pointer.

func GetRequest

func GetRequest[Q any](w webFramework.WebFramework, isJSON bool) (*ParseResult[Q], error)

GetRequest parses a request body or query into the given type and returns the parse result.

func Req added in v0.2.0

func Req[Req any, Header any, PT interface {
	webFramework.HeaderInterface
	*Header
}](params ParseParams) (*ParseResult[Req], error)

Req parses the request header and body using the given parse parameters.

type Request

type Request struct {
	Header    webFramework.HeaderInterface `json:"header"`
	ID        string                       `json:"id"`
	RequestID string                       `json:"request_id"`
	Time      time.Time                    `json:"dt"`
	Incoming  any                          `json:"incoming"`
	Req       string                       `json:"req"`
	Resp      string                       `json:"resp"`
	Outgoing  any                          `json:"outgoing"`
	Tags      map[string]string            `json:"tags"`
	Result    string                       `json:"result"`
	// Tracing fields
	TraceID string `json:"trace_id"`
	SpanID  string `json:"span_id"`
	Sampled bool   `json:"sampled"`
}

Request holds the full request lifecycle data including header, body, and tracing context.

func (*Request) GetTraceContext added in v0.18.0

func (r *Request) GetTraceContext() trace.SpanContext

GetTraceContext reconstructs and returns the trace span context from the request.

func (*Request) HasTraceContext added in v0.18.0

func (r *Request) HasTraceContext() bool

HasTraceContext reports whether the request has trace context information.

func (*Request) SetTraceContext added in v0.18.0

func (r *Request) SetTraceContext(spanCtx trace.SpanContext)

SetTraceContext stores the trace span context fields on the request.

type RequestHeader

type RequestHeader struct {
	RequestID string `header:"Request-Id" reqHeader:"Request-Id" validate:"required,min=10,max=64"`
	Program   string `header:"Program-Id" reqHeader:"Program-Id"`
	Module    string `header:"Module-Id"  reqHeader:"Module-Id"`
	Method    string `header:"Method-Id"  reqHeader:"Method-Id"`
	User      string `header:"User-Id"    reqHeader:"User-Id"`
	Branch    string `header:"Branch-Id"  reqHeader:"Branch-Id"`
	Bank      string `header:"Bank-Id"    reqHeader:"Bank-Id"`
	Person    string `header:"Person-Id"  reqHeader:"Person-Id"`
}

RequestHeader holds the standard HTTP request headers for requestCore.

func ParseRequest added in v0.9.0

func ParseRequest[Req any](
	w webFramework.WebFramework,
	mode Type,
	validateHeader bool,
) (*Req, *RequestHeader, error)

ParseRequest parses the request header and body into the given type.

func (RequestHeader) GetBank added in v0.4.7

func (r RequestHeader) GetBank() string

GetBank returns the bank ID from the header.

func (RequestHeader) GetBranch added in v0.4.7

func (r RequestHeader) GetBranch() string

GetBranch returns the branch ID from the header.

func (RequestHeader) GetID added in v0.28.1

func (r RequestHeader) GetID() string

GetID returns the request ID.

func (RequestHeader) GetMethod added in v0.2.0

func (r RequestHeader) GetMethod() string

GetMethod returns the method ID from the header.

func (RequestHeader) GetModule added in v0.2.0

func (r RequestHeader) GetModule() string

GetModule returns the module ID from the header.

func (RequestHeader) GetPerson added in v0.4.7

func (r RequestHeader) GetPerson() string

GetPerson returns the person ID from the header.

func (RequestHeader) GetProgram added in v0.2.0

func (r RequestHeader) GetProgram() string

GetProgram returns the program ID from the header.

func (RequestHeader) GetUser added in v0.2.0

func (r RequestHeader) GetUser() string

GetUser returns the user ID from the header.

func (*RequestHeader) SetBank added in v0.4.7

func (r *RequestHeader) SetBank(bank string)

SetBank sets the bank ID on the header.

func (*RequestHeader) SetBranch added in v0.4.7

func (r *RequestHeader) SetBranch(branch string)

SetBranch sets the branch ID on the header.

func (*RequestHeader) SetMethod added in v0.2.0

func (r *RequestHeader) SetMethod(method string)

SetMethod sets the method ID on the header.

func (*RequestHeader) SetModule added in v0.2.0

func (r *RequestHeader) SetModule(module string)

SetModule sets the module ID on the header.

func (*RequestHeader) SetPerson added in v0.4.7

func (r *RequestHeader) SetPerson(person string)

SetPerson sets the person ID on the header.

func (*RequestHeader) SetProgram added in v0.2.0

func (r *RequestHeader) SetProgram(program string)

SetProgram sets the program ID on the header.

func (*RequestHeader) SetUser added in v0.2.0

func (r *RequestHeader) SetUser(user string)

SetUser sets the user ID on the header.

type RequestInterface

type RequestInterface interface {
	Initialize(c webFramework.WebFramework, method, url string, req RequestPtr, args ...any) (int, map[string]string, error)
	InitRequest(c webFramework.WebFramework, method, url string) error
	InitializeNoLog(c webFramework.WebFramework, method, url string, req RequestPtr, args ...any) (int, map[string]string, error)
	InsertRequest(request RequestPtr) error
	CheckDuplicateRequest(request RequestPtr) error
	UpdateRequestWithContext(ctx context.Context, request RequestPtr) error
}

RequestInterface defines the methods for initializing and persisting requests.

type RequestModel

type RequestModel struct {
	QueryInterface libQuery.QueryRunnerInterface
	InsertInDb     string
	UpdateInDb     string
	QueryInDb      string
}

RequestModel holds the database query interface and SQL statements for request persistence.

func (RequestModel) CheckDuplicateRequest

func (m RequestModel) CheckDuplicateRequest(request RequestPtr) error

CheckDuplicateRequest queries the database to verify the request ID is not a duplicate.

func (RequestModel) InitRequest added in v0.9.1

func (m RequestModel) InitRequest(w webFramework.WebFramework, method, _ string) error

InitRequest checks for duplicates and inserts the request from the parser's local storage.

func (RequestModel) Initialize

func (m RequestModel) Initialize(w webFramework.WebFramework, method, url string, req RequestPtr, args ...any) (int, map[string]string, error)

Initialize checks for duplicate requests, inserts the request, and builds the formatted path.

func (RequestModel) InitializeNoLog

func (m RequestModel) InitializeNoLog(w webFramework.WebFramework, _, url string, _ RequestPtr, args ...any) (int, map[string]string, error)

InitializeNoLog builds the formatted path without duplicate checking or request insertion.

func (RequestModel) InsertRequest

func (m RequestModel) InsertRequest(request RequestPtr) error

InsertRequest inserts a request into the database using a background context.

func (RequestModel) InsertRequestWithContext added in v0.6.10

func (m RequestModel) InsertRequestWithContext(ctx context.Context, request RequestPtr) error

InsertRequestWithContext inserts a request into the database using the given context.

func (RequestModel) UpdateRequest

func (m RequestModel) UpdateRequest(request RequestPtr) error

UpdateRequest updates a request in the database using a background context.

func (RequestModel) UpdateRequestWithContext added in v0.6.10

func (m RequestModel) UpdateRequestWithContext(ctx context.Context, request RequestPtr) error

UpdateRequestWithContext updates a request in the database using the given context.

type RequestPtr added in v0.9.0

type RequestPtr *Request

RequestPtr is a pointer to a Request.

type Type added in v0.5.7

type Type int

Type represents the request body binding mode (e.g. NoBinding, JSON, XML).

const (
	// NoBinding indicates no request body binding.
	NoBinding Type = iota
	// JSON indicates JSON body binding.
	JSON
	// JSONWithURI indicates JSON body and URI parameter binding.
	JSONWithURI
	// Query indicates URL query parameter binding.
	Query
	// QueryWithURI indicates URL query and URI parameter binding.
	QueryWithURI
	// QueryWithPagination indicates URL query with pagination parameter binding.
	QueryWithPagination
	// URI indicates URI parameter binding only.
	URI
	// URIAndPagination indicates URI parameter and pagination binding.
	URIAndPagination
)

func TypeString added in v0.5.7

func TypeString(s string) (Type, error)

TypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func TypeValues added in v0.5.7

func TypeValues() []Type

TypeValues returns all values of the enum

func (Type) IsAType added in v0.5.7

func (i Type) IsAType() bool

IsAType returns "true" if the value is listed in the enum definition. "false" otherwise

func (Type) MarshalJSON added in v0.5.7

func (i Type) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Type

func (Type) String added in v0.5.7

func (i Type) String() string

func (*Type) UnmarshalJSON added in v0.5.7

func (i *Type) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Type

Jump to

Keyboard shortcuts

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