Documentation
¶
Overview ¶
package adapters provides helper functions for the google.type protobuf messages (Decimal, Fraction, etc.).
Index ¶
- func Float64ToProtoDecimal(f float64) *dpb.Decimal
- func FloatToProtoDecimal(f *big.Float) *dpb.Decimal
- func ProtoColorToRGBA(c *cpb.Color) *color.RGBA
- func ProtoDateTimeToTime(d *dtpb.DateTime) (time.Time, error)
- func ProtoDateToLocalTime(d *dpb.Date) time.Time
- func ProtoDateToTime(d *dpb.Date, l *time.Location) time.Time
- func ProtoDateToUTCTime(d *dpb.Date) time.Time
- func ProtoDecimalToFloat(d *dpb.Decimal) (*big.Float, error)
- func ProtoDecimalToFloat64(d *dpb.Decimal) (float64, big.Accuracy, error)
- func ProtoFractionToRat(f *fpb.Fraction) *big.Rat
- func RGBAToProtoColor(rgba *color.RGBA) *cpb.Color
- func RatToProtoFraction(r *big.Rat) *fpb.Fraction
- func TimeToProtoDate(t time.Time) *dpb.Date
- func TimeToProtoDateTime(t time.Time) (*dtpb.DateTime, error)
- func ToMonth(m mpb.Month) time.Month
- func ToProtoMonth(m time.Month) mpb.Month
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Float64ToProtoDecimal ¶
Float64ToProtoDecimal converts the provided float64 to a google.type.Decimal.
func FloatToProtoDecimal ¶
FloatToProtoDecimal converts the provided big.Float to a google.type.Decimal.
func ProtoColorToRGBA ¶
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 ¶
ProtoDateTimeToTime returns a new Time based on the google.type.DateTime.
It errors if it gets invalid time zone information.
func ProtoDateToLocalTime ¶
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 ¶
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 ¶
ProtoDateToUTCTime returns a new Time based on the google.type.Date, in UTC.
Hours, minutes, seconds, and nanoseconds are set to 0.
func ProtoDecimalToFloat ¶
ProtoDecimalToFloat converts the provided google.type.Decimal to a big.Float.
func ProtoDecimalToFloat64 ¶
ProtoDecimalToFloat64 converts the provided google.type.Decimal to a float64.
func ProtoFractionToRat ¶
ProtoFractionToRat returns a math/big Rat (rational number) based on the given google.type.fraction.
func RGBAToProtoColor ¶
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 ¶
RatToProtoFraction returns a google.type.Fraction from a math/big Rat.
func TimeToProtoDate ¶
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 ¶
TimeToProtoDateTime returns a new google.type.DateTime based on the provided time.Time.
It errors if it gets invalid time zone information.
Types ¶
This section is empty.