docs(phrasebook,sieve,towel,wire,wristband): compile README Go examples via src= (quick-261118-qsd)
This commit is contained in:
@@ -30,35 +30,41 @@ Use cases:
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
package blog
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/wire"
|
||||
)
|
||||
|
||||
```go src=modules/wire/example_test.go#postJSON
|
||||
// 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 showPost(w http.ResponseWriter, r *http.Request) {
|
||||
var tags []string // nil when the post has no tags
|
||||
body := postJSON{
|
||||
ID: 1,
|
||||
Title: "Hello & welcome", // "&" stays unescaped
|
||||
Tags: wire.Slice(tags), // marshals as []
|
||||
Featured: wire.TriBool{}, // marshals as null
|
||||
PublishedAt: wire.Time{Time: time.Now()}, // "...+00:00"
|
||||
}
|
||||
wire.WriteJSON(w, http.StatusOK, map[string]any{"data": body})
|
||||
```go src=modules/wire/example_test.go#ExampleWriteJSON
|
||||
var tags []string // nil: the post has no tags
|
||||
warsaw := time.FixedZone("CEST", 2*60*60)
|
||||
body := postJSON{
|
||||
ID: 1,
|
||||
Title: "Tips & <tricks>",
|
||||
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 & <tricks>","tags":[],"featured":null,"pinned":true,"published_at":"2026-09-30T12:05:00+00:00"}}|
|
||||
// 500 {"error":true,"message":"Internal server error"}
|
||||
```
|
||||
|
||||
## API reference
|
||||
|
||||
Reference in New Issue
Block a user