feat(06-03): add wire JSON helpers, Time, TriBool, and Slice

- WriteJSON matches genre_controller byte-for-byte (SetEscapeHTML false, trim trailing newline)
- Time marshals Carbon +00:00 never Z; TriBool nulls when invalid; Slice never returns nil
This commit is contained in:
Jakub Zych
2026-09-19 20:01:27 +02:00
parent fa7e6d1870
commit 9ecf2d1868
2 changed files with 203 additions and 0 deletions

90
wire/response.go Normal file
View File

@@ -0,0 +1,90 @@
package wire
import (
"bytes"
"encoding/json"
"net/http"
"time"
)
// WriteJSON encodes v as JSON with HTML escaping disabled and no trailing
// newline, matching the PHP json_encode body shape used by house handlers.
func WriteJSON(w http.ResponseWriter, status int, v any) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(v); err != nil {
WriteOpaque500(w)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, _ = w.Write(bytes.TrimSuffix(buf.Bytes(), []byte("\n")))
}
// WriteOpaque500 writes the house opaque 500 body.
func WriteOpaque500(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"error":true,"message":"Internal server error"}`))
}
// Time marshals as Carbon's +00:00 form (2006-01-02T15:04:05+00:00), never
// Go's default "Z". Unmarshal accepts both +00:00 and Z for read paths.
type Time struct{ time.Time }
const (
carbonLayout = "2006-01-02T15:04:05-07:00"
rfc3339Z = time.RFC3339
)
func (t Time) MarshalJSON() ([]byte, error) {
s := t.UTC().Format("2006-01-02T15:04:05") + "+00:00"
return []byte(`"` + s + `"`), nil
}
func (t *Time) UnmarshalJSON(b []byte) error {
if t == nil {
return nil
}
if string(b) == "null" {
t.Time = time.Time{}
return nil
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
var err error
for _, layout := range []string{carbonLayout, rfc3339Z} {
var parsed time.Time
parsed, err = time.Parse(layout, s)
if err == nil {
t.Time = parsed
return nil
}
}
return err
}
// TriBool keeps PHP's nullable-boolean tri-state: Valid=false marshals null.
type TriBool struct{ Valid, Value bool }
func (b TriBool) MarshalJSON() ([]byte, error) {
if !b.Valid {
return []byte("null"), nil
}
if b.Value {
return []byte("true"), nil
}
return []byte("false"), nil
}
// Slice returns s unchanged if non-nil, else a non-nil empty slice that
// marshals to [].
func Slice[T any](s []T) []T {
if s == nil {
return []T{}
}
return s
}

113
wire/response_test.go Normal file
View File

@@ -0,0 +1,113 @@
package wire
import (
"encoding/json"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"time"
)
// carbonOffsetRe is tide/normalize.go's Carbon +00:00 form, copied so wire
// does not import tide.
var carbonOffsetRe = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+00:00$`)
func TestTimeMarshalJSONCarbonOffset(t *testing.T) {
tm := Time{Time: time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC)}
b, err := json.Marshal(tm)
if err != nil {
t.Fatal(err)
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
t.Fatal(err)
}
if !carbonOffsetRe.MatchString(s) {
t.Fatalf("got %q, want carbon +00:00 form", s)
}
if string(b) != `"2024-01-02T03:04:05+00:00"` {
t.Fatalf("bytes = %s", b)
}
}
func TestTimeUnmarshalJSONAcceptsOffsetAndZ(t *testing.T) {
var offset Time
if err := json.Unmarshal([]byte(`"2024-01-02T03:04:05+00:00"`), &offset); err != nil {
t.Fatal(err)
}
if !offset.Equal(time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC)) {
t.Fatalf("offset = %v", offset.Time)
}
var z Time
if err := json.Unmarshal([]byte(`"2024-01-02T03:04:05Z"`), &z); err != nil {
t.Fatal(err)
}
if !z.Equal(time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC)) {
t.Fatalf("z = %v", z.Time)
}
}
func TestTriBoolMarshalJSON(t *testing.T) {
null, err := json.Marshal(TriBool{Valid: false})
if err != nil {
t.Fatal(err)
}
if string(null) != "null" {
t.Fatalf("invalid = %s", null)
}
f, err := json.Marshal(TriBool{Valid: true, Value: false})
if err != nil {
t.Fatal(err)
}
if string(f) != "false" {
t.Fatalf("false = %s", f)
}
tr, err := json.Marshal(TriBool{Valid: true, Value: true})
if err != nil {
t.Fatal(err)
}
if string(tr) != "true" {
t.Fatalf("true = %s", tr)
}
}
func TestSliceNilMarshalsEmptyArray(t *testing.T) {
s := Slice[int](nil)
if s == nil {
t.Fatal("Slice(nil) returned nil")
}
b, err := json.Marshal(s)
if err != nil {
t.Fatal(err)
}
if string(b) != "[]" {
t.Fatalf("got %s", b)
}
}
func TestWriteJSONNoHTMLEscapeNoTrailingNewline(t *testing.T) {
rec := httptest.NewRecorder()
WriteJSON(rec, http.StatusOK, map[string]string{"a": "x<y"})
if rec.Code != http.StatusOK {
t.Fatalf("status = %d", rec.Code)
}
got := rec.Body.String()
if got != `{"a":"x<y"}` {
t.Fatalf("body = %q", got)
}
if rec.Header().Get("Content-Type") != "application/json" {
t.Fatalf("Content-Type = %q", rec.Header().Get("Content-Type"))
}
}
func TestWriteOpaque500(t *testing.T) {
rec := httptest.NewRecorder()
WriteOpaque500(rec)
if rec.Code != http.StatusInternalServerError {
t.Fatalf("status = %d", rec.Code)
}
if rec.Body.String() != `{"error":true,"message":"Internal server error"}` {
t.Fatalf("body = %q", rec.Body.String())
}
}