mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-10-12 00:45:02 +00:00
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
78 lines
2.2 KiB
Go
78 lines
2.2 KiB
Go
// Code generated by cmd/genapi. DO NOT EDIT.
|
|
|
|
//go:build !ignore_autogenerated
|
|
|
|
// Package api contains the Telegram Bot API object types and method
|
|
// wrappers, generated from the live documentation by cmd/genapi.
|
|
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"github.com/goccy/go-json"
|
|
"io"
|
|
)
|
|
|
|
var _ = io.Discard // keep import even if no fields use io
|
|
var _ = json.Marshal // keep import for UnmarshalXxx helpers
|
|
var _ = fmt.Errorf // keep import for UnmarshalXxx helpers
|
|
var _ = bytes.TrimLeft // keep import for shape-dispatching UnmarshalXxx helpers (unions with alternates)
|
|
|
|
// This object represents a Telegram user or bot.
|
|
type User struct {
|
|
// Unique identifier.
|
|
ID int64 `json:"id"`
|
|
// True, if this user is a bot.
|
|
IsBot bool `json:"is_bot"`
|
|
// User's or bot's first name.
|
|
FirstName string `json:"first_name"`
|
|
// Optional. User's or bot's last name.
|
|
LastName string `json:"last_name,omitempty"`
|
|
}
|
|
|
|
// ChatMember is a union type. The following concrete variants implement
|
|
// it:
|
|
// - ChatMemberOwner
|
|
// - ChatMemberAdministrator
|
|
//
|
|
// This object contains information about one member of a chat. Currently, the following 6 types of chat members are supported:
|
|
type ChatMember interface{ isChatMember() }
|
|
|
|
// isChatMember is the marker method that makes ChatMemberOwner implement ChatMember.
|
|
func (*ChatMemberOwner) isChatMember() {}
|
|
|
|
// isChatMember is the marker method that makes ChatMemberAdministrator implement ChatMember.
|
|
func (*ChatMemberAdministrator) isChatMember() {}
|
|
|
|
// UnmarshalChatMember decodes a ChatMember from JSON by inspecting the
|
|
// "status" field and dispatching to the correct concrete type.
|
|
func UnmarshalChatMember(data []byte) (ChatMember, error) {
|
|
var probe struct {
|
|
V string `json:"status"`
|
|
}
|
|
if err := json.Unmarshal(data, &probe); err != nil {
|
|
return nil, err
|
|
}
|
|
var v ChatMember
|
|
switch probe.V {
|
|
case "administrator":
|
|
v = &ChatMemberAdministrator{}
|
|
case "creator":
|
|
v = &ChatMemberOwner{}
|
|
case "kicked":
|
|
v = &ChatMemberBanned{}
|
|
case "left":
|
|
v = &ChatMemberLeft{}
|
|
case "member":
|
|
v = &ChatMemberMember{}
|
|
case "restricted":
|
|
v = &ChatMemberRestricted{}
|
|
default:
|
|
return nil, fmt.Errorf("ChatMember: unknown status %q", probe.V)
|
|
}
|
|
if err := json.Unmarshal(data, v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|