Skip to content

Commit

Permalink
cleanup and add more marshalling to protobufs
Browse files Browse the repository at this point in the history
  • Loading branch information
delaneyj committed Nov 27, 2023
1 parent 2711b5b commit 5dc8e17
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version: "3"

vars:
VERSION: 0.1.1
VERSION: 0.2.0

tasks:
bump:
Expand Down
10 changes: 7 additions & 3 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 5dc8e17

Please sign in to comment.