at://bnewbold.net/pub.leaflet.document/3m7tlzbuph22u
Back to Collection
Record JSON
{
"$type": "pub.leaflet.document",
"author": "did:plc:44ybard66vv44zksje25o7dz",
"description": "Hacking in 'const' output behavior",
"pages": [
{
"$type": "pub.leaflet.pages.linearDocument",
"blocks": [
{
"$type": "pub.leaflet.pages.linearDocument#block",
"block": {
"$type": "pub.leaflet.blocks.text",
"facets": [
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#link",
"uri": "https://github.com/bluesky-social/indigo"
}
],
"index": {
"byteEnd": 111,
"byteStart": 105
}
}
],
"plaintext": "Something I've been noodling on as side-project is improving the Go lexicon codegen (\"lexgen\") output in indigo."
}
},
{
"$type": "pub.leaflet.pages.linearDocument#block",
"block": {
"$type": "pub.leaflet.blocks.text",
"facets": [
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#link",
"uri": "https://go.dev/blog/jsonv2-exp"
}
],
"index": {
"byteEnd": 333,
"byteStart": 326
}
}
],
"plaintext": "There are a couple tricky bits to this, partially because we need to support both CBOR and JSON encoding (at least for some types), and because Go's JSON tagging support is a bit minimal. That's probably the right thing for the language, but it makes supporting things like discriminated unions more complicated. The upcoming json/v2 changes look like an improvement, but don't help with these specific issues."
}
},
{
"$type": "pub.leaflet.pages.linearDocument#block",
"block": {
"$type": "pub.leaflet.blocks.text",
"facets": [
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#code"
}
],
"index": {
"byteEnd": 32,
"byteStart": 27
}
},
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#code"
}
],
"index": {
"byteEnd": 135,
"byteStart": 130
}
}
],
"plaintext": "An example friction is the $type field on record type structs. The lexicon encoding rules are that records should always have the $type populated in the object, even if they aren't in an union. That means when stored as a record, or a sub-field of another record type, or anywhere in API requests or responses."
}
},
{
"$type": "pub.leaflet.pages.linearDocument#block",
"block": {
"$type": "pub.leaflet.blocks.text",
"facets": [
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#code"
}
],
"index": {
"byteEnd": 87,
"byteStart": 82
}
}
],
"plaintext": "The straight-forward way to do this would be if Go JSON marshaling tags supported const. You could just put the NSID in there at the struct level, and it would always get inserted when serializing."
}
},
{
"$type": "pub.leaflet.pages.linearDocument#block",
"block": {
"$type": "pub.leaflet.blocks.text",
"facets": [
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#code"
}
],
"index": {
"byteEnd": 34,
"byteStart": 30
}
},
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#code"
}
],
"index": {
"byteEnd": 236,
"byteStart": 218
}
}
],
"plaintext": "The paper-cut way is to put a Type field on the struct and tell devs they are responsible for always populating that with the NSID (maybe using a codegen const). Nobody wants to do that! Even with constructor support (NewExampleRecord()) I think it'll get skipped a bunch. But this has been the best idea I could come up with."
}
},
{
"$type": "pub.leaflet.pages.linearDocument#block",
"block": {
"$type": "pub.leaflet.blocks.text",
"facets": [
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#code"
}
],
"index": {
"byteEnd": 65,
"byteStart": 52
}
}
],
"plaintext": "Maybe there is a good way to do this by declaring a MarshalJSON() method on the struct type? Eg, if you could insert a bit of code to set any constant fields, and then call the regular reflect code. But I think defining that method clobbers the reflect mechanism and I don't know how to get around that."
}
},
{
"$type": "pub.leaflet.pages.linearDocument#block",
"block": {
"$type": "pub.leaflet.blocks.text",
"facets": [
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#code"
}
],
"index": {
"byteEnd": 87,
"byteStart": 74
}
}
],
"plaintext": "An idea I just came up with is to define a string wrapper that implements MarshalText() with constant output:"
}
},
{
"$type": "pub.leaflet.pages.linearDocument#block",
"block": {
"$type": "pub.leaflet.blocks.code",
"language": "go",
"plaintext": "var ComExampleRecord_NSID = \"com.example.record\"\n\ntype ComExampleRecord_Type string\n\nfunc (r ComExampleRecord_Type) MarshalText() ([]byte, error) {\n\treturn []byte(ComExampleRecord_NSID), nil\n}\n\ntype ComExampleRecord struct {\n\tType ComExampleRecord_Type `json:\"$type\"`\n\tText string `json:\"text\"`\n\tYear int64 `json:\"year,omitempty\"`\n}"
}
},
{
"$type": "pub.leaflet.pages.linearDocument#block",
"block": {
"$type": "pub.leaflet.blocks.text",
"facets": [
{
"features": [
{
"$type": "pub.leaflet.richtext.facet#link",
"uri": "https://go.dev/play/p/9IJsJ8SFPY5"
}
],
"index": {
"byteEnd": 82,
"byteStart": 49
}
}
],
"plaintext": "You can give it a try in the Go playground here: https://go.dev/play/p/9IJsJ8SFPY5"
}
}
],
"id": "019b1571-e28b-788d-9714-995825d4638c"
}
],
"postRef": {
"cid": "bafyreiel23h5azsyzkmqhgc7gy3slkpla32dx6d7snmpglppbk4i4bzxpi",
"commit": {
"cid": "bafyreiem2ymw7cihdc4ficxuhwnfkq2pu3higkknnibglnx7zq54qfkwpq",
"rev": "3m7tlzhmt2c2e"
},
"uri": "at://did:plc:44ybard66vv44zksje25o7dz/app.bsky.feed.post/3m7tlzhknpk2u",
"validationStatus": "valid"
},
"publication": "at://did:plc:44ybard66vv44zksje25o7dz/pub.leaflet.publication/3m2x76zrtrs23",
"publishedAt": "2025-12-13T02:54:18.052Z",
"tags": [],
"title": "Go Lexicon Struct $type"
}