mirror of
https://github.com/lukaszraczylo/go-telegram.git
synced 2026-06-05 22:43:59 +00:00
e4614d800f
Telegram's optional int/bool/float fields are pointers so callers can explicitly send false or 0 to override a chat default — distinct from 'absent', which uses the chat default. The pointer construction has been ergonomically painful: photoLimit := int64(5) Limit: &photoLimit api.Ptr[T any](v T) *T collapses that to a single line: Limit: api.Ptr[int64](5) DisableNotification: api.Ptr(true) Pointers stay because the explicit-zero distinction matters for fields like DisableNotification, ProtectContent, and getUpdates.Offset where sending 0 / false explicitly is semantically different from omitting the field.
29 lines
640 B
Go
29 lines
640 B
Go
package api_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/lukaszraczylo/go-telegram/api"
|
|
)
|
|
|
|
func TestPtr(t *testing.T) {
|
|
if got := api.Ptr[int64](5); got == nil || *got != 5 {
|
|
t.Fatalf("Ptr[int64](5) = %v, want *5", got)
|
|
}
|
|
if got := api.Ptr(false); got == nil || *got != false {
|
|
t.Fatalf("Ptr(false) = %v, want *false", got)
|
|
}
|
|
if got := api.Ptr("hello"); got == nil || *got != "hello" {
|
|
t.Fatalf("Ptr(\"hello\") = %v, want *\"hello\"", got)
|
|
}
|
|
|
|
n := int64(42)
|
|
got := api.Ptr(n)
|
|
if got == nil || *got != 42 {
|
|
t.Fatalf("Ptr(n) = %v, want *42", got)
|
|
}
|
|
if got == &n {
|
|
t.Fatalf("Ptr should copy, not alias caller's variable")
|
|
}
|
|
}
|