Documentation
¶
Overview ¶
Package protox 收口 go-toolbox 中真正需要 google.golang.org/protobuf 的反射辅助 分出来的目的:pkg/types 被 mathx / convert / syncx 等大量基础包依赖,一旦它引入 protobuf,所有使用者(go-cachex、go-toolbox/pkg/i18n 等)都会被动承担 30+ 个 protobuf 包
[EN] Package protox holds the reflect helpers that genuinely need protobuf. pkg/types is imported by many foundational packages, so keeping protobuf out of it spares every downstream consumer ~30 protobuf packages they never use.
Index ¶
- Variables
- func BoolToPB(src *bool) *wrapperspb.BoolValue
- func DoubleToPB(src *float64) *wrapperspb.DoubleValue
- func IfProtoTimeOr(protoTime *timestamppb.Timestamp, duration time.Duration) time.Time
- func IfProtoTimeOrPtr(protoTime *timestamppb.Timestamp, duration time.Duration) *time.Time
- func IfTimeToProto(t *time.Time, duration time.Duration) *timestamppb.Timestamp
- func Int32ToPB(src *int32) *wrapperspb.Int32Value
- func Int64ToPB(src *int64) *wrapperspb.Int64Value
- func IsProtoMessageType(t reflect.Type) bool
- func NewProtoMessage[T proto.Message]() T
- func PtrToBool(src *wrapperspb.BoolValue) *bool
- func PtrToDouble(src *wrapperspb.DoubleValue) *float64
- func PtrToInt32(src *wrapperspb.Int32Value) *int32
- func PtrToInt64(src *wrapperspb.Int64Value) *int64
- func PtrToString(src *wrapperspb.StringValue) *string
- func PtrToTime(src *timestamppb.Timestamp) *time.Time
- func StringToPB(src *string) *wrapperspb.StringValue
- func TimeToTimestampPB(src *time.Time) *timestamppb.Timestamp
- func UnwrapPBValue(iface interface{}) interface{}
Constants ¶
This section is empty.
Variables ¶
ProtoMessageType protobuf 消息类型
Functions ¶
func BoolToPB ¶
func BoolToPB(src *bool) *wrapperspb.BoolValue
BoolToPB 安全转换 *bool 到 wrapperspb.BoolValue
func DoubleToPB ¶
func DoubleToPB(src *float64) *wrapperspb.DoubleValue
DoubleToPB 安全转换 *float64 到 wrapperspb.DoubleValue
func IfProtoTimeOr ¶
IfProtoTimeOr proto时间戳转换三元运算 如果 proto 时间戳不为 nil 且有效,转换为 time.Time;否则返回当前时间加上偏移时间 支持任意精度的时间偏移(秒、分钟、小时、天等),正数表示未来,负数表示过去
示例:
30天前作为默认值 startTime := mathx.IfProtoTimeOr(req.StartTime, -30*24*time.Hour) 当前时间作为默认值 endTime := mathx.IfProtoTimeOr(req.EndTime, 0) 1小时后作为默认值 futureTime := mathx.IfProtoTimeOr(req.FutureTime, 1*time.Hour) 30秒前作为默认值 recentTime := mathx.IfProtoTimeOr(req.RecentTime, -30*time.Second)
func IfProtoTimeOrPtr ¶
IfProtoTimeOrPtr proto时间戳转指针三元运算 如果 proto 时间戳不为 nil 且有效,转换为 *time.Time;否则返回当前时间加上偏移时间的指针 支持任意精度的时间偏移,适用于需要返回指针的场景
示例:
startTime := mathx.IfProtoTimeOrPtr(req.StartTime, -30*24*time.Hour) // 返回 *time.Time endTime := mathx.IfProtoTimeOrPtr(req.EndTime, 0) // 返回 *time.Time recentTime := mathx.IfProtoTimeOrPtr(req.RecentTime, -5*time.Minute) // 5分钟前
func IfTimeToProto ¶
IfTimeToProto time.Time 转 proto 时间戳三元运算 如果 time 指针不为 nil,转换为 *timestamppb.Timestamp;否则返回当前时间加上偏移时间的 proto 时间戳 支持任意精度的时间偏移,适用于反向转换场景
示例:
将 time.Time 转为 proto(存在则转换) protoTime := mathx.IfTimeToProto(timePtr, 0) 将 time.Time 转为 proto(nil时用30天前) protoStartTime := mathx.IfTimeToProto(startTimePtr, -30*24*time.Hour) 将 time.Time 转为 proto(nil时用1小时后) protoFutureTime := mathx.IfTimeToProto(futureTimePtr, 1*time.Hour)
func Int32ToPB ¶
func Int32ToPB(src *int32) *wrapperspb.Int32Value
Int32ToPB 安全转换 *int32 到 wrapperspb.Int32Value
func Int64ToPB ¶
func Int64ToPB(src *int64) *wrapperspb.Int64Value
Int64ToPB 安全转换 *int64 到 wrapperspb.Int64Value
func IsProtoMessageType ¶
IsProtoMessageType 判断类型是否实现 proto.Message 接口
func PtrToBool ¶
func PtrToBool(src *wrapperspb.BoolValue) *bool
PtrToBool 安全转换 *wrapperspb.BoolValue 到 *bool
func PtrToDouble ¶
func PtrToDouble(src *wrapperspb.DoubleValue) *float64
PtrToDouble 安全转换 *wrapperspb.DoubleValue 到 *float64
func PtrToInt32 ¶
func PtrToInt32(src *wrapperspb.Int32Value) *int32
PtrToInt32 安全转换 *wrapperspb.Int32Value 到 *int32
func PtrToInt64 ¶
func PtrToInt64(src *wrapperspb.Int64Value) *int64
PtrToInt64 安全转换 *wrapperspb.Int64Value 到 *int64
func PtrToString ¶
func PtrToString(src *wrapperspb.StringValue) *string
PtrToString 安全转换 *wrapperspb.StringValue 到 *string
func PtrToTime ¶
func PtrToTime(src *timestamppb.Timestamp) *time.Time
PtrToTime 安全转换 *timestamppb.Timestamp 到 *time.Time
func StringToPB ¶
func StringToPB(src *string) *wrapperspb.StringValue
StringToPB 安全转换 *string 到 wrapperspb.StringValue
func TimeToTimestampPB ¶
func TimeToTimestampPB(src *time.Time) *timestamppb.Timestamp
TimeToTimestampPB 安全转换 *time.Time 到 *timestamppb.Timestamp
func UnwrapPBValue ¶
func UnwrapPBValue(iface interface{}) interface{}
UnwrapPBValue 解包 protobuf wrapper 类型,返回底层值 支持 wrapperspb 全部 9 种类型:String, Bool, Int32, Int64, UInt32, UInt64, Float, Double, Bytes 如果 wrapper 为 nil 或 typed nil,返回 nil 如果不是 wrapper 类型,返回原始值
Types ¶
This section is empty.