From 5dc8e17177826c74f53a27e613a38b75c86e3cdb Mon Sep 17 00:00:00 2001 From: Delaney Gillilan Date: Mon, 27 Nov 2023 14:30:54 -0800 Subject: [PATCH] cleanup and add more marshalling to protobufs --- Taskfile.yml | 2 +- database.go | 10 +++++++--- protobuf.go | 23 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index bb87c5f..f52ac52 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -3,7 +3,7 @@ version: "3" vars: - VERSION: 0.1.1 + VERSION: 0.2.0 tasks: bump: diff --git a/database.go b/database.go index f4a8094..85c9874 100644 --- a/database.go +++ b/database.go @@ -119,6 +119,10 @@ const ( UnixEpochJulianDay = 2440587.5 ) +var ( + JulianZeroTime = JulianDayToTime(0) +) + // TimeToJulianDay converts a time.Time into a Julian day. func TimeToJulianDay(t time.Time) float64 { return float64(t.UTC().Unix())/secondsInADay + UnixEpochJulianDay @@ -142,17 +146,17 @@ func JulianDayToTimestamp(f float64) *timestamppb.Timestamp { return timestamppb.New(t) } -func JulianDayToTimestampStmt(stmt *sqlite.Stmt, colName string) *timestamppb.Timestamp { +func StmtJulianToTimestamp(stmt *sqlite.Stmt, colName string) *timestamppb.Timestamp { julianDays := stmt.GetFloat(colName) return JulianDayToTimestamp(julianDays) } -func JulianDayToTimeStmt(stmt *sqlite.Stmt, colName string) time.Time { +func StmtJulianToTime(stmt *sqlite.Stmt, colName string) time.Time { julianDays := stmt.GetFloat(colName) return JulianDayToTime(julianDays) } -func Bytes(stmt *sqlite.Stmt, colName string) []byte { +func StmtBytes(stmt *sqlite.Stmt, colName string) []byte { bl := stmt.GetLen(colName) if bl == 0 { return nil diff --git a/protobuf.go b/protobuf.go index 599b6c3..fdf311b 100644 --- a/protobuf.go +++ b/protobuf.go @@ -32,3 +32,26 @@ func MustProtoJSONUnmarshal(b []byte, msg proto.Message) { panic(err) } } + +type MustProtobufHandler struct { + isJSON bool +} + +func NewProtobufHandler(isJSON bool) *MustProtobufHandler { + return &MustProtobufHandler{isJSON: isJSON} +} + +func (h *MustProtobufHandler) Marshal(msg proto.Message) []byte { + if h.isJSON { + return MustProtoJSONMarshal(msg) + } + return MustProtoMarshal(msg) +} + +func (h *MustProtobufHandler) Unmarshal(b []byte, msg proto.Message) { + if h.isJSON { + MustProtoJSONUnmarshal(b, msg) + } else { + MustProtoUnmarshal(b, msg) + } +}