about summary refs log tree commit diff
path: root/fun/clbot/gerrit/gerritevents/time.go
diff options
context:
space:
mode:
authorLuke Granger-Brown <git@lukegb.com>2020-06-13T22·43+0100
committerlukegb <lukegb@tvl.fyi>2020-06-14T17·16+0000
commitc05803ff14f7d7c911bdc91383703b5cd9342396 (patch)
tree0d930afc22b541f06f3778378185d85a455a10db /fun/clbot/gerrit/gerritevents/time.go
parentf6c7c85d9410a8314191913f4f1a22dc526b8b79 (diff)
feat(clbot): Create Gerrit watcher and basic clbot binary. r/950
gerrit.Watcher is a class which watches the Gerrit stream-events SSH
connection and produces events.

There's a basic CLBot binary as well, to demonstrate driving it to
produce messages on the logging output. It doesn't really do anything
else.

Change-Id: I274fe0a77c8329f79456425405e2fbdc3ca2edf0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/245
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'fun/clbot/gerrit/gerritevents/time.go')
-rw-r--r--fun/clbot/gerrit/gerritevents/time.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/fun/clbot/gerrit/gerritevents/time.go b/fun/clbot/gerrit/gerritevents/time.go
new file mode 100644
index 0000000000..7fbfaa3f5c
--- /dev/null
+++ b/fun/clbot/gerrit/gerritevents/time.go
@@ -0,0 +1,38 @@
+package gerritevents
+
+import (
+	"fmt"
+	"strconv"
+	"time"
+)
+
+// Time is a time.Time that is formatted as a Unix timestamp in JSON.
+type Time struct {
+	time.Time
+}
+
+// UnmarshalJSON unmarshals a Unix timestamp into a Time.
+func (t *Time) UnmarshalJSON(bs []byte) error {
+	if string(bs) == "null" {
+		return nil
+	}
+	u, err := strconv.ParseInt(string(bs), 10, 64)
+	if err != nil {
+		return err
+	}
+	t.Time = time.Unix(u, 0)
+	return nil
+}
+
+// MarshalJSON marshals a Time into a Unix timestamp.
+func (t *Time) MarshalJSON() ([]byte, error) {
+	if t.IsZero() {
+		return []byte("null"), nil
+	}
+	return []byte(fmt.Sprintf("%d", t.Unix())), nil
+}
+
+// IsSet returns true if the time.Time is non-zero.
+func (t *Time) IsSet() bool {
+	return !t.IsZero()
+}