types¶
import "github.com/frgrisk/vor-stream/queue/types"
Package types contains functions for converting to VOR Stream data types. This package is recommended for use in node development to ensure consistent and reliable data type conversions. The package includes a generic Cast() function that will attempt to convert any Go type to a VOR Stream data type. It also includes functions for returning typed values from any type so that later type assertion is not required.
Index¶
- Variables
- func Cast(from any, toType DataType) (any, error)
- func CastDateToProtoDate(d date.Date) *datepb.Date
- func CastDatetimeToProtoTimestamp(dt time.Time) *timestamppb.Timestamp
- func CastToArray(from any) ([]float64, error)
- func CastToBool(from any) (bool, error)
- func CastToChar(from any) (string, error)
- func CastToDate(from any) (date.Date, error)
- func CastToDatetime(from any) (time.Time, error)
- func CastToInt(from any) (int64, error)
- func CastToNum(from any) (float64, error)
- func CopyMapToProto(in map[string]any, out proto.Message) error
- func Equal(a, b any) bool
- func ParseDate(dt string) (date.Date, error)
- func Sort[S \~[]E, E proto.Message](s S, field string) error
- func ZipMap(x []string, y []any) map[string]any
- type DataType
- func GoTypeToDataType(t string) (DataType, error)
- func StringToDataType(s string) (DataType, error)
- func (DataType) Descriptor() protoreflect.EnumDescriptor
- func (x DataType) Enum() *DataType
- func (DataType) EnumDescriptor() ([]byte, []int)
- func (d DataType) Missing() any
- func (x DataType) Number() protoreflect.EnumNumber
- func (x DataType) String() string
- func (DataType) Type() protoreflect.EnumType
Variables¶
var (
DataType_name = map[int32]string{
0: "UNSPECIFIED",
1: "ARRAY",
2: "BOOL",
3: "CHAR",
4: "DATE",
5: "DATETIME",
6: "INT",
7: "NUM",
}
DataType_value = map[string]int32{
"UNSPECIFIED": 0,
"ARRAY": 1,
"BOOL": 2,
"CHAR": 3,
"DATE": 4,
"DATETIME": 5,
"INT": 6,
"NUM": 7,
}
)
Extension fields to descriptorpb.FieldOptions.
var (
// optional vor_stream.queue.types.DataType vor_stream_type = 50001;
E_VorStreamType = &file_vor_stream_queue_types_datatype_proto_extTypes[0]
)
var File_vor_stream_queue_types_datatype_proto protoreflect.FileDescriptor
func Cast¶
func Cast(from any, toType DataType) (any, error)
Cast converts the input value (from) to the specified data type (toType). It returns the converted value and an error. If the conversion is successful, the error is nil. If the conversion fails, the error is not nil and the converted value is the zero value of the specified data type. See type-specific conversion functions for more information on how specific data type conversions are handled.
func CastDateToProtoDate¶
func CastDateToProtoDate(d date.Date) *datepb.Date
CastDateToProtoDate converts a Stream date to a Protobuf date.
func CastDatetimeToProtoTimestamp¶
func CastDatetimeToProtoTimestamp(dt time.Time) *timestamppb.Timestamp
CastDatetimeToProtoTimestamp converts a Stream datetime to a Protobuf timestamp.
func CastToArray¶
func CastToArray(from any) ([]float64, error)
CastToArray converts the input value (from) to an array and returns an error if the conversion fails. It uses CastToNum to convert each element of the array to a float64.
func CastToBool¶
func CastToBool(from any) (bool, error)
CastToBool converts the input value (from) to a boolean value and returns an error if the conversion fails. It's a convenience function that wraps the spf13/cast library's ToBoolE function.
func CastToChar¶
func CastToChar(from any) (string, error)
CastToChar converts the input value (from) to a string and returns an error if the conversion fails. If the input value is a date.Date object, it converts it to a string in the format "dd/mm/yyyy". If the input value is any other type, it uses the spf13/cast library to convert the input value to a string.
func CastToDate¶
func CastToDate(from any) (date.Date, error)
CastToDate converts the input value (from) to a date.Date object and returns an error if the conversion fails. If the input value is a time.Time object, it converts it to a date.Date object. If the input value is a date.Date object, it returns the input value. If the input value is nil, it returns the zero value for the date.Date object. If the input value can be converted to an integer, it returns the date.Date object corresponding to the number of days since the epoch (1st January 1970). If the input value is any other type, it converts it to a string and then passes it to the ParseDate function. See that function's documentation for more information.
func CastToDatetime¶
func CastToDatetime(from any) (time.Time, error)
CastToDatetime converts the input value (from) to a time.Time object and returns an error if the conversion fails. If the type is a date.Date object, it converts it to a time.Time object with the time set to 00:00:00 UTC. Similarly, if the input can be parsed as a regular date using ParseDate, it will return that date with the time set to 00:00:00 UTC. If the input value is nil, it returns the zero value for the time.Time object. If the input value can be converted to an integer, it returns the time.Time object corresponding to the number of nanoseconds since the epoch (1st January 1970). If the input value is any other type, it uses the spf13/cast library to convert the input value to a time.Time object.
func CastToInt¶
func CastToInt(from any) (int64, error)
CastToInt converts the input value (from) to an int64 and returns an error if the conversion fails. If the input value is a date.Date object, it returns the number of days since the epoch (1st January 1970). If the input value is a time.Time object, it returns the number of nanoseconds since the epoch. If the input value is any other type, it uses the spf13/cast library to convert the input value to an int64.
func CastToNum¶
func CastToNum(from any) (float64, error)
CastToNum converts the input value (from) to a float64 and returns an error if the conversion fails. It's a convenience function that wraps the spf13/cast library's ToFloat64E function with the exception that it returns the math.NaN() value if the input value is nil.
func CopyMapToProto¶
func CopyMapToProto(in map[string]any, out proto.Message) error
CopyMapToProto copies the values from the input map to the output protocol buffer message. It uses reflection to set the fields of the output message based on the keys of the input map. If the key is not found in the output message, it skips that key. If the key is found in the output message, it converts the value from the input map to the appropriate data type and sets the field in the output message. If the conversion fails, it returns an error.
func Equal¶
func Equal(a, b any) bool
Equal returns true if a and b are equal.
func ParseDate¶
func ParseDate(dt string) (date.Date, error)
ParseDate parses the input date string (dt) into a date.Date object. It attempts to parse the date using several supported formats:
- "dd/mm/yyyy" (2/1/2006)
- "ddmmmyyy" (02Jan2006)
- "d-mmm-yy" (2-Jan-06)
- "yyyy Qn, Qn yyyy" (2006 Q1, Q1 2006)
- long date (January 2, 2006)
- Automatic date parsing
If none of the formats are successful, it returns an error and returns an empty date.Date.
func Sort¶
func Sort[S ~[]E, E proto.Message](s S, field string) error
Sort sorts the given slice of protocol buffer messages based on the specified field. It uses reflection to get the field value and type, and then sorts the slice based on the field value.
func ZipMap¶
func ZipMap(x []string, y []any) map[string]any
ZipMap creates a map from two slices, one of strings and one of any type. The function returns a map with the strings as keys and any values as values. The function panics if the length of the two slices are not equal. This function is useful for creating a map to pass to the CopyMapToProto function.
type DataType¶
type DataType int32
const (
DataType_UNSPECIFIED DataType = 0 // Default value
DataType_ARRAY DataType = 1
DataType_BOOL DataType = 2
DataType_CHAR DataType = 3
DataType_DATE DataType = 4
DataType_DATETIME DataType = 5
DataType_INT DataType = 6
DataType_NUM DataType = 7
)
func GoTypeToDataType¶
func GoTypeToDataType(t string) (DataType, error)
GoTypeToDataType converts a Go type to a VOR Stream data type. If the Go type is not supported, it returns an error and the data type is set to the zero value of the DataType type.
func StringToDataType¶
func StringToDataType(s string) (DataType, error)
StringToDataType converts a string to a VOR Stream data type. If the string is not a valid data type, it returns an error and the data type is set to the zero value of the DataType type.
func (DataType) Descriptor¶
func (DataType) Descriptor() protoreflect.EnumDescriptor
func (DataType) Enum¶
func (x DataType) Enum() *DataType
func (DataType) EnumDescriptor¶
func (DataType) EnumDescriptor() ([]byte, []int)
Deprecated: Use DataType.Descriptor instead.
func (DataType) Missing¶
func (d DataType) Missing() any
Missing returns the missing value for the given data type. TODO: The missing values for INT, DATE, and DATETIME are arbitrary and could conflict with real data.
func (DataType) Number¶
func (x DataType) Number() protoreflect.EnumNumber
func (DataType) String¶
func (x DataType) String() string
func (DataType) Type¶
func (DataType) Type() protoreflect.EnumType
Generated by gomarkdoc