mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-10-16 01:07:40 +00:00
feat(genapi): derive discriminators and decode RichText alternates
The generator gated all union decoder emission behind knownDiscriminators, a hand-curated map of 13 entries. Any sealed union absent from that map received no decoder, so decoding must fail. Bot API v10 added RichBlock and RichText, which were absent. Roughly 8.6 percent of Telegram updates failed with HTTP 400. The generator now derives discriminators from the specification instead of a fixed list. A gate check fails the build when a union resolves no discriminator. Eleven unions gain decoders that never had one: BotCommandScope, InputMedia, InputPaidMedia, InputPollMedia, InputPollOptionMedia, InputProfilePhoto, InputRichBlock, InputStoryContent, PassportElementError, RichBlock and RichText. RichText can also appear as a bare string or an array of RichText. The scraper now extracts these alternate shapes into the intermediate representation. The emitter adds RichTextPlain and RichTextSequence. The decoder dispatches on the first JSON byte before it reads any discriminator. Unions without alternates must regenerate byte for byte. The golden test proves this. Claude-Session: https://claude.ai/code/session_01Fu2wXqGUrfVBi5aLafKk8U
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"github.com/lukaszraczylo/go-telegram/internal/spec"
|
||||
)
|
||||
|
||||
// altStringRE matches the scalar-string alternate clause Telegram uses
|
||||
// for union types that can also decode as plain text on the wire, e.g.
|
||||
// "a String for plain text". The trailing "for ..." clause is optional
|
||||
// and, when present, is not captured — only the shape matters. "an"
|
||||
// (rather than "a") is tolerated so a future rephrasing ("an String")
|
||||
// doesn't silently stop matching, and the word boundary keeps "Strings"
|
||||
// or "SubString" from misfiring.
|
||||
var altStringRE = regexp.MustCompile(`(?i)\ban?\s+String\b`)
|
||||
|
||||
// altArrayRE builds the pattern that matches the self-referential array
|
||||
// alternate clause Telegram uses for recursive union types, e.g. "an
|
||||
// Array of RichText". Matching is anchored to the union's own name so a
|
||||
// doc mentioning "an Array of" some other type is never mistaken for a
|
||||
// shape alternate.
|
||||
func altArrayRE(unionName string) *regexp.Regexp {
|
||||
return regexp.MustCompile(`(?i)\ban?\s+Array\s+of\s+` + regexp.QuoteMeta(unionName) + `\b`)
|
||||
}
|
||||
|
||||
// extractAlternates detects Telegram's "can be either a String for
|
||||
// plain text, an Array of <Union>, or any of the following types:"
|
||||
// phrasing on a union type's doc string and returns the non-object
|
||||
// shapes it declares, in a fixed (string, then array) order regardless
|
||||
// of how the sentence orders its clauses.
|
||||
//
|
||||
// Conservative by design: callers invoke this only for types that
|
||||
// already have OneOf populated (see typeFromSection in scrape.go), and
|
||||
// it only recognises the two shapes Telegram's docs currently use for
|
||||
// this ("a String" and "an Array of <the union's own name>") — anything
|
||||
// else in the doc is ignored, so free-text mentioning "String" or
|
||||
// "Array" elsewhere in a union's description can't produce a false
|
||||
// positive for a type this pattern wasn't written for. As of Bot API
|
||||
// v10, RichText is the only union whose doc matches either pattern.
|
||||
func extractAlternates(unionName, doc string) []spec.Alternate {
|
||||
var alts []spec.Alternate
|
||||
if altStringRE.MatchString(doc) {
|
||||
alts = append(alts, spec.Alternate{Shape: spec.AlternateString})
|
||||
}
|
||||
if altArrayRE(unionName).MatchString(doc) {
|
||||
alts = append(alts, spec.Alternate{Shape: spec.AlternateArray})
|
||||
}
|
||||
return alts
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/lukaszraczylo/go-telegram/internal/spec"
|
||||
)
|
||||
|
||||
func TestExtractAlternates_RichTextDoc(t *testing.T) {
|
||||
// Exact doc string as scraped from the live Bot API v10 docs page
|
||||
// (internal/spec/api.json / testdata/html/latest.html).
|
||||
doc := "This object represents a rich formatted text. Currently, it can be either a String for plain text, " +
|
||||
"an Array of RichText, or any of the following types:"
|
||||
got := extractAlternates("RichText", doc)
|
||||
require.Equal(t, []spec.Alternate{
|
||||
{Shape: spec.AlternateString},
|
||||
{Shape: spec.AlternateArray},
|
||||
}, got)
|
||||
}
|
||||
|
||||
func TestExtractAlternates_OrderIndependent(t *testing.T) {
|
||||
// Same two clauses, reversed, plus an extra clause in between —
|
||||
// order and surrounding prose must not matter.
|
||||
doc := "Currently, it can be either an Array of RichText, something else entirely, a String for plain text, " +
|
||||
"or any of the following types:"
|
||||
got := extractAlternates("RichText", doc)
|
||||
require.Equal(t, []spec.Alternate{
|
||||
{Shape: spec.AlternateString},
|
||||
{Shape: spec.AlternateArray},
|
||||
}, got)
|
||||
}
|
||||
|
||||
func TestExtractAlternates_ObjectOnlyUnion_NoAlternates(t *testing.T) {
|
||||
// RichBlock's doc is object-only: no String/Array alternate clause.
|
||||
doc := "This object represents a block in a rich formatted message. Currently, it can be any of the following types:"
|
||||
got := extractAlternates("RichBlock", doc)
|
||||
require.Nil(t, got)
|
||||
}
|
||||
|
||||
func TestExtractAlternates_ArrayOfDifferentType_NotSelfReferential(t *testing.T) {
|
||||
// "an Array of X" only counts when X is the union's own name — an
|
||||
// array of some other type mentioned in the doc must not match.
|
||||
doc := "This object can be either an Array of PhotoSize or any of the following types:"
|
||||
got := extractAlternates("RichText", doc)
|
||||
require.Nil(t, got)
|
||||
}
|
||||
|
||||
func TestExtractAlternates_StringOnly(t *testing.T) {
|
||||
doc := "This object can be either a String or any of the following types:"
|
||||
got := extractAlternates("Widget", doc)
|
||||
require.Equal(t, []spec.Alternate{{Shape: spec.AlternateString}}, got)
|
||||
}
|
||||
|
||||
func TestExtractAlternates_PlainTypeDoc_NoFalsePositive(t *testing.T) {
|
||||
// Ordinary field/type prose mentioning neither shape must not match.
|
||||
doc := "This object represents a chat photo."
|
||||
got := extractAlternates("ChatPhoto", doc)
|
||||
require.Nil(t, got)
|
||||
}
|
||||
|
||||
// TestTypeFromSection_RichText_RealSnapshot grounds the extraction in the
|
||||
// real pinned docs snapshot (not just a literal fixture string): scraping
|
||||
// testdata/html/latest.html end-to-end must produce a RichText TypeDecl
|
||||
// with both alternates, matching the same live text that broke decoding
|
||||
// of plain-text and array-shaped rich_message payloads.
|
||||
func TestTypeFromSection_RichText_RealSnapshot(t *testing.T) {
|
||||
htmlBytes, err := os.ReadFile("../../testdata/html/latest.html")
|
||||
require.NoError(t, err)
|
||||
|
||||
api, err := scrape(htmlBytes)
|
||||
require.NoError(t, err)
|
||||
|
||||
var richText *spec.TypeDecl
|
||||
for i := range api.Types {
|
||||
if api.Types[i].Name == "RichText" {
|
||||
richText = &api.Types[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
require.NotNil(t, richText, "RichText type must be present in the real docs snapshot")
|
||||
require.Equal(t, []spec.Alternate{
|
||||
{Shape: spec.AlternateString},
|
||||
{Shape: spec.AlternateArray},
|
||||
}, richText.Alternates)
|
||||
|
||||
// RichBlock is the control: object-only union, no alternates.
|
||||
var richBlock *spec.TypeDecl
|
||||
for i := range api.Types {
|
||||
if api.Types[i].Name == "RichBlock" {
|
||||
richBlock = &api.Types[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
require.NotNil(t, richBlock)
|
||||
require.Nil(t, richBlock.Alternates)
|
||||
}
|
||||
@@ -38,6 +38,10 @@ func typeFromSection(s section) spec.TypeDecl {
|
||||
} else if len(s.Lists) > 0 {
|
||||
// Union: extract variant names from <li><a>...</a></li>.
|
||||
td.OneOf = extractListLinks(s.Lists[0])
|
||||
// Some unions (e.g. RichText) also declare non-object wire shapes
|
||||
// ("a String for plain text", "an Array of RichText") in the same
|
||||
// sentence as the object-variant list; extract those too.
|
||||
td.Alternates = extractAlternates(td.Name, td.Doc)
|
||||
}
|
||||
return td
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user