mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-06-05 22:43:59 +00:00
refactor(api): typed enums for emoji-list fields (DiceEmoji, ReactionEmoji)
Two API fields carry restricted emoji-value sets that the scraper's
curly-quote regex strips during IR extraction (multi-byte boundary
issue): ReactionTypeEmoji.Emoji and sendDice.Emoji. They previously
typed as plain string with no compile-time guarantee on values.
Add hand-curated typed-string enums in api/enums.go (the manual file,
not enums.gen.go):
- DiceEmoji: 6 constants (Dice, Dart, Basketball, Football, Bowling,
SlotMachine) covering Telegram's full set for sendDice.
- ReactionEmoji: 73 constants covering the canonical reaction set
from https://core.telegram.org/bots/api#reactiontypeemoji. Names
follow Unicode CLDR short names where one exists, otherwise stable
common-English labels (e.g. ThumbsUp, Heart, Clown, ManTechnologist).
Wire the field-type override via cmd/genapi/emitter.go:
- fieldTypeOverrides map keyed "<TypeOrParamsName>.<FieldName>".
- goField/multipartFieldEntry consult the override after the enum-plan
lookup; falls through to the default goType when nothing matches.
- methods.tmpl gains goFieldP/multipartFieldEntryP helpers that pass
the params type name as override-parent (the params struct doesn't
share a Go type with the field, so the existing parent="" enum-key
convention is preserved).
Regenerated api/types.gen.go and api/methods.gen.go now type the two
fields as ReactionEmoji and DiceEmoji respectively. No other Emoji
field is affected (override is scoped per parent type). regen-from-
fixture is byte-deterministic across runs.
Add api/emoji_enums_test.go covering const wire values, reflection
checks on field types, and a marshal/unmarshal round-trip for
ReactionTypeEmoji.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestDiceEmoji_Constants pins the canonical six dice-emoji values so a
|
||||
// regen, refactor, or accidental rename can't silently break the wire
|
||||
// contract.
|
||||
func TestDiceEmoji_Constants(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
got DiceEmoji
|
||||
want string
|
||||
}{
|
||||
{"Dice", DiceEmojiDice, "🎲"},
|
||||
{"Dart", DiceEmojiDart, "🎯"},
|
||||
{"Basketball", DiceEmojiBasketball, "🏀"},
|
||||
{"Football", DiceEmojiFootball, "⚽"},
|
||||
{"Bowling", DiceEmojiBowling, "🎳"},
|
||||
{"SlotMachine", DiceEmojiSlotMachine, "🎰"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
require.Equal(t, c.want, string(c.got))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestSendDiceParams_EmojiFieldType asserts the codegen override wired
|
||||
// SendDiceParams.Emoji to the typed enum (not plain string). Reflection
|
||||
// catches a regression even if the file compiles via implicit string
|
||||
// conversion of an untyped literal.
|
||||
func TestSendDiceParams_EmojiFieldType(t *testing.T) {
|
||||
rt := reflect.TypeOf(SendDiceParams{})
|
||||
f, ok := rt.FieldByName("Emoji")
|
||||
require.True(t, ok, "SendDiceParams.Emoji not present")
|
||||
require.Equal(t, "DiceEmoji", f.Type.Name())
|
||||
}
|
||||
|
||||
// TestSendDiceParams_MarshalJSON exercises the marshalled wire form to
|
||||
// prove the typed enum still serialises as a JSON string holding the
|
||||
// raw emoji bytes — i.e. the type override doesn't accidentally
|
||||
// double-encode.
|
||||
func TestSendDiceParams_MarshalJSON(t *testing.T) {
|
||||
p := &SendDiceParams{
|
||||
ChatID: ChatIDFromInt(1),
|
||||
Emoji: DiceEmojiBasketball,
|
||||
}
|
||||
data, err := json.Marshal(p)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(data), `"emoji":"🏀"`)
|
||||
}
|
||||
|
||||
// TestReactionEmoji_Constants spot-checks a representative slice of the
|
||||
// 73-value enum. A full enumeration would be redundant — the test is
|
||||
// here to lock the wire form, not to retest the const-block.
|
||||
func TestReactionEmoji_Constants(t *testing.T) {
|
||||
require.Equal(t, "👍", string(ReactionEmojiThumbsUp))
|
||||
require.Equal(t, "👎", string(ReactionEmojiThumbsDown))
|
||||
require.Equal(t, "❤", string(ReactionEmojiHeart))
|
||||
require.Equal(t, "🔥", string(ReactionEmojiFire))
|
||||
require.Equal(t, "💯", string(ReactionEmojiHundredPoints))
|
||||
require.Equal(t, "🤡", string(ReactionEmojiClown))
|
||||
}
|
||||
|
||||
// TestReactionTypeEmoji_FieldType asserts the codegen override wired
|
||||
// ReactionTypeEmoji.Emoji to the typed enum.
|
||||
func TestReactionTypeEmoji_FieldType(t *testing.T) {
|
||||
rt := reflect.TypeOf(ReactionTypeEmoji{})
|
||||
f, ok := rt.FieldByName("Emoji")
|
||||
require.True(t, ok, "ReactionTypeEmoji.Emoji not present")
|
||||
require.Equal(t, "ReactionEmoji", f.Type.Name())
|
||||
}
|
||||
|
||||
// TestReactionTypeEmoji_RoundTrip proves a typed-enum value survives
|
||||
// JSON marshal → unmarshal cycle without losing fidelity. The
|
||||
// discriminator MarshalJSON on ReactionTypeEmoji forces type="emoji",
|
||||
// so we set it explicitly here for symmetry with the unmarshal path.
|
||||
func TestReactionTypeEmoji_RoundTrip(t *testing.T) {
|
||||
in := &ReactionTypeEmoji{
|
||||
Type: ReactionTypeKindEmoji,
|
||||
Emoji: ReactionEmojiThumbsUp,
|
||||
}
|
||||
data, err := json.Marshal(in)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(data), `"emoji":"👍"`)
|
||||
|
||||
var out ReactionTypeEmoji
|
||||
require.NoError(t, json.Unmarshal(data, &out))
|
||||
require.Equal(t, ReactionEmojiThumbsUp, out.Emoji)
|
||||
}
|
||||
+104
@@ -32,3 +32,107 @@ const (
|
||||
UpdateChatBoost UpdateType = "chat_boost"
|
||||
UpdateRemovedChatBoost UpdateType = "removed_chat_boost"
|
||||
)
|
||||
|
||||
// DiceEmoji is the set of emoji values accepted by sendDice. Telegram's
|
||||
// canonical list is "🎲", "🎯", "🏀", "⚽", "🎳", "🎰". The codegen
|
||||
// scraper drops these values during regex extraction (multi-byte
|
||||
// boundary issues with curly-quoted emoji), so this enum is hand-
|
||||
// curated and wired into SendDiceParams.Emoji via the per-field type
|
||||
// override in cmd/genapi/emitter.go.
|
||||
type DiceEmoji string
|
||||
|
||||
const (
|
||||
DiceEmojiDice DiceEmoji = "🎲"
|
||||
DiceEmojiDart DiceEmoji = "🎯"
|
||||
DiceEmojiBasketball DiceEmoji = "🏀"
|
||||
DiceEmojiFootball DiceEmoji = "⚽"
|
||||
DiceEmojiBowling DiceEmoji = "🎳"
|
||||
DiceEmojiSlotMachine DiceEmoji = "🎰"
|
||||
)
|
||||
|
||||
// ReactionEmoji is the set of emoji Telegram allows in a
|
||||
// ReactionTypeEmoji.Emoji value. Hand-curated from
|
||||
// https://core.telegram.org/bots/api#reactiontypeemoji because the
|
||||
// scraper's curly-quote regex strips the emoji literals (byte-boundary
|
||||
// issue on multi-byte sequences). Names mirror the Unicode CLDR short
|
||||
// name where one exists; otherwise a stable common-English label.
|
||||
// Telegram occasionally extends this set — passers of unrecognised
|
||||
// strings still type-check (ReactionEmoji is a string alias) so this
|
||||
// list need not block runtime use of newer values.
|
||||
type ReactionEmoji string
|
||||
|
||||
const (
|
||||
ReactionEmojiHeart ReactionEmoji = "❤"
|
||||
ReactionEmojiThumbsUp ReactionEmoji = "👍"
|
||||
ReactionEmojiThumbsDown ReactionEmoji = "👎"
|
||||
ReactionEmojiFire ReactionEmoji = "🔥"
|
||||
ReactionEmojiSmilingFaceWithHearts ReactionEmoji = "🥰"
|
||||
ReactionEmojiClappingHands ReactionEmoji = "👏"
|
||||
ReactionEmojiBeamingFace ReactionEmoji = "😁"
|
||||
ReactionEmojiThinkingFace ReactionEmoji = "🤔"
|
||||
ReactionEmojiExplodingHead ReactionEmoji = "🤯"
|
||||
ReactionEmojiScreamingFace ReactionEmoji = "😱"
|
||||
ReactionEmojiCursingFace ReactionEmoji = "🤬"
|
||||
ReactionEmojiCryingFace ReactionEmoji = "😢"
|
||||
ReactionEmojiPartyPopper ReactionEmoji = "🎉"
|
||||
ReactionEmojiStarStruck ReactionEmoji = "🤩"
|
||||
ReactionEmojiVomiting ReactionEmoji = "🤮"
|
||||
ReactionEmojiPileOfPoo ReactionEmoji = "💩"
|
||||
ReactionEmojiFoldedHands ReactionEmoji = "🙏"
|
||||
ReactionEmojiOKHand ReactionEmoji = "👌"
|
||||
ReactionEmojiDove ReactionEmoji = "🕊"
|
||||
ReactionEmojiClown ReactionEmoji = "🤡"
|
||||
ReactionEmojiYawning ReactionEmoji = "🥱"
|
||||
ReactionEmojiWoozyFace ReactionEmoji = "🥴"
|
||||
ReactionEmojiHeartEyes ReactionEmoji = "😍"
|
||||
ReactionEmojiWhale ReactionEmoji = "🐳"
|
||||
ReactionEmojiHeartOnFire ReactionEmoji = "❤🔥"
|
||||
ReactionEmojiNewMoonFace ReactionEmoji = "🌚"
|
||||
ReactionEmojiHotDog ReactionEmoji = "🌭"
|
||||
ReactionEmojiHundredPoints ReactionEmoji = "💯"
|
||||
ReactionEmojiRollingOnFloor ReactionEmoji = "🤣"
|
||||
ReactionEmojiLightning ReactionEmoji = "⚡"
|
||||
ReactionEmojiBanana ReactionEmoji = "🍌"
|
||||
ReactionEmojiTrophy ReactionEmoji = "🏆"
|
||||
ReactionEmojiBrokenHeart ReactionEmoji = "💔"
|
||||
ReactionEmojiRaisedEyebrow ReactionEmoji = "🤨"
|
||||
ReactionEmojiNeutralFace ReactionEmoji = "😐"
|
||||
ReactionEmojiStrawberry ReactionEmoji = "🍓"
|
||||
ReactionEmojiChampagne ReactionEmoji = "🍾"
|
||||
ReactionEmojiKissMark ReactionEmoji = "💋"
|
||||
ReactionEmojiMiddleFinger ReactionEmoji = "🖕"
|
||||
ReactionEmojiDevil ReactionEmoji = "😈"
|
||||
ReactionEmojiSleeping ReactionEmoji = "😴"
|
||||
ReactionEmojiLoudlyCrying ReactionEmoji = "😭"
|
||||
ReactionEmojiNerd ReactionEmoji = "🤓"
|
||||
ReactionEmojiGhost ReactionEmoji = "👻"
|
||||
ReactionEmojiManTechnologist ReactionEmoji = "👨💻"
|
||||
ReactionEmojiEyes ReactionEmoji = "👀"
|
||||
ReactionEmojiJackOLantern ReactionEmoji = "🎃"
|
||||
ReactionEmojiSeeNoEvil ReactionEmoji = "🙈"
|
||||
ReactionEmojiHalo ReactionEmoji = "😇"
|
||||
ReactionEmojiFearful ReactionEmoji = "😨"
|
||||
ReactionEmojiHandshake ReactionEmoji = "🤝"
|
||||
ReactionEmojiWriting ReactionEmoji = "✍"
|
||||
ReactionEmojiHugging ReactionEmoji = "🤗"
|
||||
ReactionEmojiSaluting ReactionEmoji = "🫡"
|
||||
ReactionEmojiSantaClaus ReactionEmoji = "🎅"
|
||||
ReactionEmojiChristmasTree ReactionEmoji = "🎄"
|
||||
ReactionEmojiSnowman ReactionEmoji = "☃"
|
||||
ReactionEmojiNailPolish ReactionEmoji = "💅"
|
||||
ReactionEmojiZanyFace ReactionEmoji = "🤪"
|
||||
ReactionEmojiMoai ReactionEmoji = "🗿"
|
||||
ReactionEmojiCool ReactionEmoji = "🆒"
|
||||
ReactionEmojiHeartWithArrow ReactionEmoji = "💘"
|
||||
ReactionEmojiHearNoEvil ReactionEmoji = "🙉"
|
||||
ReactionEmojiUnicorn ReactionEmoji = "🦄"
|
||||
ReactionEmojiKissingFace ReactionEmoji = "😘"
|
||||
ReactionEmojiPill ReactionEmoji = "💊"
|
||||
ReactionEmojiSpeakNoEvil ReactionEmoji = "🙊"
|
||||
ReactionEmojiSmilingFaceWithSunglasses ReactionEmoji = "😎"
|
||||
ReactionEmojiAlienMonster ReactionEmoji = "👾"
|
||||
ReactionEmojiManShrugging ReactionEmoji = "🤷♂"
|
||||
ReactionEmojiPersonShrugging ReactionEmoji = "🤷"
|
||||
ReactionEmojiWomanShrugging ReactionEmoji = "🤷♀"
|
||||
ReactionEmojiPoutingFace ReactionEmoji = "😡"
|
||||
)
|
||||
|
||||
+1
-1
@@ -1953,7 +1953,7 @@ type SendDiceParams struct {
|
||||
// Identifier of the direct messages topic to which the message will be sent; required if the message is sent to a direct messages chat
|
||||
DirectMessagesTopicID *int64 `json:"direct_messages_topic_id,omitempty"`
|
||||
// Emoji on which the dice throw animation is based. Currently, must be one of “”, “”, “”, “”, “”, or “”. Dice can have values 1-6 for “”, “” and “”, values 1-5 for “” and “”, and values 1-64 for “”. Defaults to “”
|
||||
Emoji string `json:"emoji,omitempty"`
|
||||
Emoji DiceEmoji `json:"emoji,omitempty"`
|
||||
// Sends the message silently. Users will receive a notification with no sound.
|
||||
DisableNotification *bool `json:"disable_notification,omitempty"`
|
||||
// Protects the contents of the sent message from forwarding
|
||||
|
||||
+1
-1
@@ -3472,7 +3472,7 @@ type ReactionTypeEmoji struct {
|
||||
// Type of the reaction, always “emoji”
|
||||
Type ReactionTypeKind `json:"type"`
|
||||
// Reaction emoji. Currently, it can be one of "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
|
||||
Emoji string `json:"emoji"`
|
||||
Emoji ReactionEmoji `json:"emoji"`
|
||||
}
|
||||
|
||||
// MarshalJSON encodes ReactionTypeEmoji with the discriminator field
|
||||
|
||||
Reference in New Issue
Block a user