Commits

Ross Light committed a4b3085

Initial import

  • Participants

Comments (0)

Files changed (3)

File stackexchange.go

+// Package stackexchange provides access to the Stack Exchange 2.0 API.
+package stackexchange
+package stackexchange
+
+import (
+	"encoding/json"
+	"time"
+)
+
+// Question represents a question on one of the Stack Exchange sites.
+//
+// https://api.stackexchange.com/docs/types/question
+type Question struct {
+	ID               int    `json:"question_id"`
+	AcceptedAnswerID int    `json:"accepted_answer_id"`
+	AnswerCount      int    `json:"answer_count"`
+	ClosedDate       Time   `json:"closed_date"`
+	ClosedReason     string `json:"closed_reason"`
+	Created          Time   `json:"creation_date"`
+	IsAnswered       bool   `json:"is_answered"`
+	Link             string `json:"link"`
+}
+
+// Answer represents an answer to a question on one of the Stack Exchange sites.
+//
+// https://api.stackexchange.com/docs/types/answer
+type Answer struct {
+	ID         int    `json:"answer_id"`
+	Body       string `json:"body"`
+	Created    Time   `json:"creation_date"`
+	IsAccepted bool   `json:"is_accepted"`
+	Link       string `json:"link"`
+	QuestionID int    `json:"question_id"`
+	Score      int    `json:"score"`
+}
+
+// Time converts Stack Exchange API time values to/from JSON into Go time values.
+// Stack Exchange stores time values over the wire as int64 Unix epoch time.
+//
+// See: https://api.stackexchange.com/docs/dates
+type Time time.Time
+
+func (t Time) MarshalJSON() ([]byte, error) {
+	return json.Marshal(time.Time(t).Unix())
+}
+
+func (t *Time) UnmarshalJSON(data []byte) error {
+	var tt int64
+	if err := json.Unmarshal(data, &tt); err != nil {
+		return err
+	}
+	*t = Time(time.Unix(tt, 0))
+	return nil
+}

File types_test.go

+package stackexchange
+
+import (
+	"testing"
+	"time"
+)
+
+func TestUnmarshalTime(t *testing.T) {
+	tests := []struct {
+		In  string
+		Out time.Time
+	}{
+		{"0", time.Unix(0, 0)},
+		{"-42", time.Unix(-42, 0)},
+		{"42", time.Unix(42, 0)},
+	}
+	for _, test := range tests {
+		mytime := new(Time)
+		if in, out, err := test.In, test.Out, mytime.UnmarshalJSON([]byte(test.In)); err != nil {
+			t.Errorf("new(Time).UnmarshalJSON(%q) error: %v", in, err)
+		} else if mytime := time.Time(*mytime); mytime != out {
+			t.Errorf("new(Time).UnmarshalJSON(%q) = %v; want %v", in, mytime, out)
+		}
+	}
+}