package wire_test import ( "fmt" "net/http" "net/http/httptest" "time" "git.golem15.com/golem15/summercms/modules/wire" ) // postJSON is the response shape of one acme.blog post. type postJSON struct { ID uint `json:"id"` Title string `json:"title"` Tags []string `json:"tags"` Featured wire.TriBool `json:"featured"` Pinned wire.TriBool `json:"pinned"` PublishedAt wire.Time `json:"published_at"` } func ExampleWriteJSON() { var tags []string // nil: the post has no tags warsaw := time.FixedZone("CEST", 2*60*60) body := postJSON{ ID: 1, Title: "Tips & ", Tags: wire.Slice(tags), Featured: wire.TriBool{}, Pinned: wire.TriBool{Value: true, Valid: true}, PublishedAt: wire.Time{Time: time.Date(2026, 9, 30, 14, 5, 0, 0, warsaw)}, } rec := httptest.NewRecorder() wire.WriteJSON(rec, http.StatusOK, map[string]any{"data": body}) fmt.Println(rec.Code, rec.Header().Get("Content-Type")) fmt.Printf("%s|\n", rec.Body.String()) rec = httptest.NewRecorder() wire.WriteOpaque500(rec) fmt.Println(rec.Code, rec.Body.String()) // Output: // 200 application/json // {"data":{"id":1,"title":"Tips & ","tags":[],"featured":null,"pinned":true,"published_at":"2026-09-30T12:05:00+00:00"}}| // 500 {"error":true,"message":"Internal server error"} }