Documentation
¶
Overview ¶
Package handlerparams provides functions for parsing handlers parameters.
Index ¶
- Constants
- Variables
- func AliasFromType(v any) string
- func ExtractParams(doc *types.Document, command string, value any, l *zap.Logger) error
- func GetBoolOptionalParam(key string, v any) (bool, error)
- func GetValidatedNumberParamWithMinValue(command string, param string, value any, minValue int32) (int64, error)
- func GetWholeNumberParam(value any) (int64, error)
- func HasSameTypeElements(array *types.Array) bool
- func SplitNamespace(ns, argument string) (string, string, error)
- type TypeCode
Constants ¶
const ( // TypeCodeDouble is a double type code. TypeCodeDouble = TypeCode(1) // double // TypeCodeString is a string type code. TypeCodeString = TypeCode(2) // string // TypeCodeObject is an object type code. TypeCodeObject = TypeCode(3) // object // TypeCodeArray is an array type code. TypeCodeArray = TypeCode(4) // array // TypeCodeBinData is a binary data type code. TypeCodeBinData = TypeCode(5) // binData // TypeCodeObjectID is an object id type code. TypeCodeObjectID = TypeCode(7) // objectId // TypeCodeBool is a boolean type code. TypeCodeBool = TypeCode(8) // bool // TypeCodeDate is a date type code. TypeCodeDate = TypeCode(9) // date // TypeCodeNull is a null type code. TypeCodeNull = TypeCode(10) // null // TypeCodeRegex is a regex type code. TypeCodeRegex = TypeCode(11) // regex // TypeCodeInt is an int type code. TypeCodeInt = TypeCode(16) // int // TypeCodeTimestamp is a timestamp type code. TypeCodeTimestamp = TypeCode(17) // timestamp // TypeCodeLong is a long type code. TypeCodeLong = TypeCode(18) // long // TypeCodeDecimal is a decimal type code. TypeCodeDecimal = TypeCode(19) // decimal // TypeCodeMinKey is a minKey type code. TypeCodeMinKey = TypeCode(-1) // minKey // TypeCodeMaxKey is a maxKey type code. TypeCodeMaxKey = TypeCode(127) // maxKey // TypeCodeNumber is a number type code. TypeCodeNumber = TypeCode(-128) // number )
Variables ¶
var ( // ErrNotWholeNumber is returned when a non-whole number is given. ErrNotWholeNumber = fmt.Errorf("not a whole number") // ErrUnexpectedLeftOpType is returned when an unexpected left operand type is given. ErrUnexpectedLeftOpType = fmt.Errorf("unexpected left operand type") // ErrUnexpectedRightOpType is returned when an unexpected right operand type is given. ErrUnexpectedRightOpType = fmt.Errorf("unexpected right operand type") // ErrLongExceededPositive is returned when a positive long value is given that exceeds the maximum value. ErrLongExceededPositive = fmt.Errorf("long exceeded - positive value") // ErrLongExceededNegative is returned when a negative long value is given that exceeds the minimum value. ErrLongExceededNegative = fmt.Errorf("long exceeded - negative value") // ErrIntExceeded is returned when an int value is given that exceeds the maximum value. ErrIntExceeded = fmt.Errorf("int exceeded") // ErrInfinity is returned when an infinity value is given. ErrInfinity = fmt.Errorf("infinity") // ErrUnexpectedType is returned when an unexpected type is given. ErrUnexpectedType = fmt.Errorf("unexpected type") )
Functions ¶
func AliasFromType ¶
AliasFromType returns BSON type alias name for given value.
func ExtractParams ¶
ExtractParams fill passed value structure with parameters from the document. If the passed value is not a pointer to the structure it panics. Parameters are extracted by the field name or by the `ferretdb` tag.
Possible tags:
- `opt` - field is optional, the field value would not be set if it's not present in the document;
- `unimplemented-non-default` - error would be returned if non-default value for the field is provided;
- `unimplemented` - error would be returned if the value is present in the document;
- `ignored` - field is ignored and would not be set, but it would be logged;
- `positiveNumber` - provided value must be of types [int, long, double] and greater than 0, double values would be rounded to long;
- `wholePositiveNumber` - provided value must be of types [int, long] and greater than 0;
- `numericBool` - provided value must be of types [bool, int, long, double] and would be converted to bool;
- `zeroOrOneAsBool` - provided value must be of types [int, long, double] with possible values `0` or `1`.
- `collection` - Collection field value holds the name of the collection and must be of type string. An error is returned if `collection` tag is not set.
It returns command errors with the following codes:
- `ErrFailedToParse` when provided field is not present in passed structure;
- `ErrFailedToParse` when provided field must be 0 or 1, but it is not;
- `ErrNotImplemented` when support for provided field is not implemented yet;
- `ErrNotImplemented`when support for non-default field value is not implemented yet;
- `ErrValueNegative` - field of numeric type is negative;
- `ErrTypeMismatch` - field is type is not matched with the type of the value;
- `ErrBadValue` when field is not a number;
- `ErrBadValue` when field is not of integer type;
- `ErrBadValue` when field is out of integer range;
- `ErrBadValue` when field has negative value;
- `ErrInvalidNamespace` - collection name has invalid type.
func GetBoolOptionalParam ¶
GetBoolOptionalParam returns bool value of v. Non-zero double, long, and int values return true. Zero values for those types, as well as nulls and missing fields, return false. Other types return a protocol error.
func GetValidatedNumberParamWithMinValue ¶
func GetValidatedNumberParamWithMinValue(command string, param string, value any, minValue int32) (int64, error)
GetValidatedNumberParamWithMinValue converts and validates a value into a number.
The function checks the type, ensures it can be represented as a whole number, isn't negative and falls within a given minimum value and the limit of a 32-bit integer.
It returns the processed integer value, or a handlererrors.CommandError error if the value fails validation. Error codes list: - ErrTypeMismatch - if the value is not a number; - ErrValueNegative - if the value is negative of lower than the minimum value.
func GetWholeNumberParam ¶
GetWholeNumberParam checks if the given value is int32, int64, or float64 containing a whole number, such as used in the limit, $size, etc.
func HasSameTypeElements ¶
HasSameTypeElements returns true if types.Array elements has the same type. MongoDB consider int32, int64 and float64 that could be converted to int as the same type.
Types ¶
type TypeCode ¶
type TypeCode int32
TypeCode represents BSON type codes. BSON type codes represent corresponding codes in BSON specification. They could be used to query fields with particular type values using $type operator. Type code `number` is added to support MongoDB surrogate alias `number` which matches double, int and long type values.
func NewTypeCode ¶
NewTypeCode returns TypeCode and error by given code.
func ParseTypeCode ¶
ParseTypeCode returns TypeCode and error by given type code alias.