adapters

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: Apache-2.0 Imports: 16 Imported by: 1

Documentation

Overview

package adapters provides helper functions for the google.type protobuf messages (Decimal, Fraction, etc.).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Float64ToProtoDecimal

func Float64ToProtoDecimal(f float64) *dpb.Decimal

Float64ToProtoDecimal converts the provided float64 to a google.type.Decimal.

func FloatToProtoDecimal

func FloatToProtoDecimal(f *big.Float) *dpb.Decimal

FloatToProtoDecimal converts the provided big.Float to a google.type.Decimal.

func ProtoColorToRGBA

func ProtoColorToRGBA(c *cpb.Color) *color.RGBA

ProtoColorToRGBA returns an RGBA based on the provided google.type.Color. If alpha is not set in the proto, full opacity is assumed.

Note: Converting between a float using [0, 1] to an int using [0, 256) causes some cognitive dissonance between accuracy and user expectations. For example, most people writing CSS use 0x80 (decimal 128) to mean "half", but it is not actually half (it is slightly over). There is actually no way to precisely specify the 0.5 float value in a [0, 256) range of integers.

This function uses math.Round to address this, meaning that 0.5 will be rounded up to 128 rather than rounded down to 127.

Because of this fuzziness and precision loss, it is NOT guaranteed that ProtoColorToRGBA and RGBAToProtoColor are exact inverses, and both functions will lose precision.

func ProtoDateTimeToTime

func ProtoDateTimeToTime(d *dtpb.DateTime) (time.Time, error)

ProtoDateTimeToTime returns a new Time based on the google.type.DateTime.

It errors if it gets invalid time zone information.

func ProtoDateToLocalTime

func ProtoDateToLocalTime(d *dpb.Date) time.Time

ProtoDateToLocalTime returns a new Time based on the google.type.Date, in the system's time zone.

Hours, minues, seconds, and nanoseconds are set to 0.

func ProtoDateToTime

func ProtoDateToTime(d *dpb.Date, l *time.Location) time.Time

ProtoDateToTime returns a new Time based on the google.type.Date and provided *time.Location.

Hours, minutes, seconds, and nanoseconds are set to 0.

func ProtoDateToUTCTime

func ProtoDateToUTCTime(d *dpb.Date) time.Time

ProtoDateToUTCTime returns a new Time based on the google.type.Date, in UTC.

Hours, minutes, seconds, and nanoseconds are set to 0.

func ProtoDecimalToFloat

func ProtoDecimalToFloat(d *dpb.Decimal) (*big.Float, error)

ProtoDecimalToFloat converts the provided google.type.Decimal to a big.Float.

func ProtoDecimalToFloat64

func ProtoDecimalToFloat64(d *dpb.Decimal) (float64, big.Accuracy, error)

ProtoDecimalToFloat64 converts the provided google.type.Decimal to a float64.

func ProtoFractionToRat

func ProtoFractionToRat(f *fpb.Fraction) *big.Rat

ProtoFractionToRat returns a math/big Rat (rational number) based on the given google.type.fraction.

func RGBAToProtoColor

func RGBAToProtoColor(rgba *color.RGBA) *cpb.Color

RGBAToProtoColor returns a google.type.Color based on the provided RGBA.

Note: Converting between ints using [0, 256) and a float using [0, 1] causes some cognitive dissonance between accuracy and user expectations. For example, most people using CSS use 0x80 (decimal 128) to mean "half", but it is not actually half (it is slightly over). These is actually no way to precisely specify the 0.5 float value in a [0, 256) range of integers.

This function addresses this by limiting decimal precision to 0.01, on the rationale that most precision beyond this point is probably unintentional.

Because of this fuzziness and precision loss, it is NOT guaranteed that ProtoColorToRGBA and RGBAToProtoColor are exact inverses, and both functions will lose precision.

func RatToProtoFraction

func RatToProtoFraction(r *big.Rat) *fpb.Fraction

RatToProtoFraction returns a google.type.Fraction from a math/big Rat.

func TimeToProtoDate

func TimeToProtoDate(t time.Time) *dpb.Date

TimeToProtoDate returns a new google.type.Date based on the provided time.Time. The location is ignored, as is anything more precise than the day.

func TimeToProtoDateTime

func TimeToProtoDateTime(t time.Time) (*dtpb.DateTime, error)

TimeToProtoDateTime returns a new google.type.DateTime based on the provided time.Time.

It errors if it gets invalid time zone information.

func ToMonth

func ToMonth(m mpb.Month) time.Month

ToMonth converts a google.type.Month to a golang Month.

func ToProtoMonth

func ToProtoMonth(m time.Month) mpb.Month

ToProtoMonth converts a Go Month to a google.type.Month.

Types

This section is empty.

Jump to

Keyboard shortcuts

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