feat(11.1-04): add the Database section and the core Services pages
- docs/database: models, migrations, queries and pagination, relations, casts and validation, attachments and transactions (lagoon.Transaction, lagoon.AfterCommit, nested savepoints, lagoon.OnDatabase) - docs/services: configuration, events, routing with auth groups, rate limiting, authentication, the OAuth server, mail and localization - runnable Examples for lagoon, attach, compass, surf, wire, bouncer, wristband, postcard, phrasebook and festival; lagoon TestDocs* regions run on the package's Postgres harness through DocsDB - 15 new required pages
This commit is contained in:
45
modules/wire/example_test.go
Normal file
45
modules/wire/example_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
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 & <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"}
|
||||
}
|
||||
Reference in New Issue
Block a user