mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-06-06 22:49:32 +00:00
3c04d7b0b1
The Telegram docs describe many string fields and parameters with
phrases like "can be ..., or ...", "must be one of ...", or "always X",
yet the generated Go API surface used raw `string` for every one of
them. Callers had to write magic strings or `string(api.ChatTypePrivate)`
to satisfy the field type. This change makes those fields typed Go
string enums emitted from the IR, so the IDE autocompletes valid values
and breaking-value drift surfaces at compile time.
Pipeline changes:
- internal/spec/ir.go: Field gains EnumValues []string. Empty for non-
enum fields; otherwise the wire-level values in doc order, deduped.
- cmd/scrape/enums.go: extractEnumValues recognises the curly-quoted
patterns Telegram uses ("can be either", "currently can be", "one
of", "must be", "always X") and rejects free-text quoted refs (e.g.
"Can be available only for X") via a tight gap check between the
trigger phrase and the first quoted value. parse_mode parameters
get the canonical Markdown / MarkdownV2 / HTML triple injected
because Telegram links to a separate formatting-options section
instead of listing values inline.
- cmd/genapi/enums.go: planEnums groups fields by sorted value-tuple,
picks a canonical Go enum name (most-common candidate, parent-
prefixed beats plain, shortest beats longer, alphabetical for
determinism), resolves cross-group name collisions by parent prefix.
- cmd/genapi/emitter.go + templates: goField rewrites the field type
to the planned enum name; multipartFieldEntry casts typed enum
values back to string when composing the wire map; enums.tmpl now
iterates the planned enums instead of hardcoding four hand-curated
ones; sentinelForField produces typed-constant test fixtures.
- api/enums.gen.go: regenerated from the live IR. 66 enum types, 155
constants. ParseMode, ChatType, MessageEntityType, ChatMember /
MessageOrigin / PaidMedia / Background / StoryAreaType / Reaction /
TransactionPartner / PassportElement variant Status & Type fields
are now typed.
- api/enums.go: hand-coded UpdateType (used by transport.LongPoller).
The Telegram docs do not enumerate Update payload kinds inline, so
the codegen pipeline cannot synthesise this enum.
- api/types.gen.go, api/methods.gen.go, api/methods_gen_test.go: 137
field declarations rewritten string -> typed enum.
- dispatch/, examples/: dropped every string(api.<Const>) cast. The
HasEntity filter now takes api.MessageEntityType; ChatType filter
compares typed values directly. ChatMember discriminator filter
casts variant.Status (typed per variant) to string for comparison.
- internal/spec/api.json, testdata/golden/*: regenerated and
refreshed. make regen-from-fixture is byte-deterministic across
runs.
Renames (no compat shims; v1 pre-public):
- EntityX -> MessageEntityTypeX (e.g. EntityBotCommand -> MessageEntityTypeBotCommand)
- EntityStrike -> MessageEntityTypeStrikethrough (full wire name)
86 lines
3.5 KiB
Go
86 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestExtractEnumValues_CanBeEither(t *testing.T) {
|
|
desc := "Type of the chat, can be either “private”, “group”, “supergroup” or “channel”"
|
|
got := extractEnumValues("type", desc)
|
|
require.Equal(t, []string{"private", "group", "supergroup", "channel"}, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_CurrentlyCanBe(t *testing.T) {
|
|
desc := "Type of the entity. Currently, can be “mention” (@username), “hashtag” (#hashtag), or “code” (monowidth string)"
|
|
got := extractEnumValues("type", desc)
|
|
require.Equal(t, []string{"mention", "hashtag", "code"}, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_AlwaysSingle(t *testing.T) {
|
|
desc := "Type of the message origin, always “user”"
|
|
got := extractEnumValues("type", desc)
|
|
require.Equal(t, []string{"user"}, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_MustBeOneOfMime(t *testing.T) {
|
|
desc := "Optional. MIME type of the thumbnail, must be one of “image/jpeg”, “image/gif”, or “video/mp4”. Defaults to “image/jpeg”"
|
|
got := extractEnumValues("thumbnail_mime_type", desc)
|
|
require.Equal(t, []string{"image/jpeg", "image/gif", "video/mp4"}, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_ParseModeSpecial(t *testing.T) {
|
|
desc := "Optional. Mode for parsing entities in the message text. See formatting options for more details."
|
|
got := extractEnumValues("parse_mode", desc)
|
|
require.Equal(t, []string{"Markdown", "MarkdownV2", "HTML"}, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_QuestionParseMode(t *testing.T) {
|
|
desc := "Mode for parsing entities in the question. See formatting options for more details."
|
|
got := extractEnumValues("question_parse_mode", desc)
|
|
require.Equal(t, []string{"Markdown", "MarkdownV2", "HTML"}, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_FalsePositiveReferencedValue(t *testing.T) {
|
|
// "Can be available only for "X"" is NOT an enum: the quote is a
|
|
// reference to a transaction-type value, not an introduced list.
|
|
desc := "Optional. Bot-specified invoice payload. Can be available only for “invoice_payment” transactions."
|
|
got := extractEnumValues("invoice_payload", desc)
|
|
require.Nil(t, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_FalsePositiveSingleQuotedNonEnum(t *testing.T) {
|
|
// "can be ignored" with a quoted reference value later — not an enum.
|
|
desc := "Optional. Thumbnail of the file sent; can be ignored if thumbnail generation for the file is supported server-side."
|
|
got := extractEnumValues("thumbnail", desc)
|
|
require.Nil(t, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_RefundedPaymentCurrentlyAlways(t *testing.T) {
|
|
desc := "Three-letter ISO 4217 currency code, or “XTR” for payments in Telegram Stars. Currently, always “XTR”"
|
|
got := extractEnumValues("currency", desc)
|
|
require.Equal(t, []string{"XTR"}, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_RejectURLValues(t *testing.T) {
|
|
// "attach://<file_attach_name>" must never be promoted to an enum value.
|
|
desc := "Pass “attach://<file_attach_name>” to upload a new one"
|
|
got := extractEnumValues("media", desc)
|
|
require.Nil(t, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_StringTypeOnly(t *testing.T) {
|
|
// (Sanity — table.go gates on string type, but the function itself
|
|
// should still respond consistently.)
|
|
desc := "ABC, can be “a”, “b”"
|
|
got := extractEnumValues("x", desc)
|
|
require.Equal(t, []string{"a", "b"}, got)
|
|
}
|
|
|
|
func TestExtractEnumValues_DedupeRepeatedValues(t *testing.T) {
|
|
desc := "Currently, one of “XTR” for Telegram Stars or “XTR” again"
|
|
got := extractEnumValues("currency", desc)
|
|
require.Equal(t, []string{"XTR"}, got)
|
|
}
|