Skip to content

Commit 4d9d354

Browse files
authored
Merge pull request #17 from Telegram-Mini-Apps/16-use-initdata-by-reference
fix: use pointers instead of values
2 parents 40e7b16 + c2379ec commit 4d9d354

File tree

5 files changed

+28
-28
lines changed

5 files changed

+28
-28
lines changed

chat.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c ChatType) Known() bool {
2626
}
2727

2828
// Chat describes chat information:
29-
// https://docs.telegram-mini-apps.com/launch-parameters/init-data#chat
29+
// https://docs.telegram-mini-apps.com/platform/init-data#chat
3030
type Chat struct {
3131
// Unique identifier for this chat.
3232
ID int64 `json:"id"`
@@ -39,8 +39,8 @@ type Chat struct {
3939

4040
// Optional. URL of the chat’s photo. The photo can be in .jpeg or .svg
4141
// formats. Only returned for Web Apps launched from the attachment menu.
42-
PhotoURL string `json:"photo_url"`
42+
PhotoURL string `json:"photo_url,omitempty"`
4343

4444
// Optional. Username of the chat.
45-
Username string `json:"username"`
45+
Username string `json:"username,omitempty"`
4646
}

init_data.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
// InitData contains init data.
8-
// https://docs.telegram-mini-apps.com/launch-parameters/init-data#parameters-list
8+
// https://docs.telegram-mini-apps.com/platform/init-data
99
type InitData struct {
1010
// The date the initialization data was created. Is a number representing a
1111
// Unix timestamp.
@@ -14,20 +14,20 @@ type InitData struct {
1414
// Optional. The number of seconds after which a message can be sent via
1515
// the method answerWebAppQuery.
1616
// https://core.telegram.org/bots/api#answerwebappquery
17-
CanSendAfterRaw int `json:"can_send_after"`
17+
CanSendAfterRaw int `json:"can_send_after,omitempty"`
1818

1919
// Optional. An object containing information about the chat with the bot in
2020
// which the Mini Apps was launched. It is returned only for Mini Apps
2121
// opened through the attachment menu.
22-
Chat Chat `json:"chat"`
22+
Chat *Chat `json:"chat,omitempty"`
2323

2424
// Optional. The type of chat from which the Mini Apps was opened.
2525
// Returned only for applications opened by direct link.
26-
ChatType ChatType `json:"chat_type"`
26+
ChatType ChatType `json:"chat_type,omitempty"`
2727

2828
// Optional. A global identifier indicating the chat from which the Mini
2929
// Apps was opened. Returned only for applications opened by direct link.
30-
ChatInstance int64 `json:"chat_instance"`
30+
ChatInstance int64 `json:"chat_instance,omitempty"`
3131

3232
// Initialization data signature.
3333
// https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app
@@ -36,21 +36,21 @@ type InitData struct {
3636
// Optional. The unique session ID of the Mini App. Used in the process of
3737
// sending a message via the method answerWebAppQuery.
3838
// https://core.telegram.org/bots/api#answerwebappquery
39-
QueryID string `json:"query_id"`
39+
QueryID string `json:"query_id,omitempty"`
4040

4141
// Optional. An object containing data about the chat partner of the current
4242
// user in the chat where the bot was launched via the attachment menu.
4343
// Returned only for private chats and only for Mini Apps launched via the
4444
// attachment menu.
45-
Receiver User `json:"receiver"`
45+
Receiver *User `json:"receiver,omitempty"`
4646

4747
// Optional. The value of the startattach or startapp query parameter
4848
// specified in the link. It is returned only for Mini Apps opened through
4949
// the attachment menu.
50-
StartParam string `json:"start_param"`
50+
StartParam string `json:"start_param,omitempty"`
5151

5252
// Optional. An object containing information about the current user.
53-
User User `json:"user"`
53+
User *User `json:"user,omitempty"`
5454
}
5555

5656
// AuthDate returns AuthDateRaw as time.Time.

parse.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ var (
1616

1717
// Parse converts passed init data presented as query string to InitData
1818
// object.
19-
func Parse(initData string) (InitData, error) {
19+
func Parse(initData string) (*InitData, error) {
2020
// Parse passed init data as query string.
2121
q, err := url.ParseQuery(initData)
2222
if err != nil {
23-
return InitData{}, fmt.Errorf("parse init data as query: %w: %w", err, ErrUnexpectedFormat)
23+
return nil, fmt.Errorf("parse init data as query: %w: %w", err, ErrUnexpectedFormat)
2424
}
2525

2626
// According to documentation, we could only meet such types as int64,
@@ -42,10 +42,10 @@ func Parse(initData string) (InitData, error) {
4242
}
4343

4444
// Unmarshal JSON to our custom structure.
45-
var d InitData
45+
var d *InitData
4646
jStr := fmt.Sprintf("{%s}", strings.Join(pairs, ","))
4747
if err := json.Unmarshal([]byte(jStr), &d); err != nil {
48-
return InitData{}, fmt.Errorf("unmarshal init data: %w: %w", err, ErrUnexpectedFormat)
48+
return nil, fmt.Errorf("unmarshal init data: %w: %w", err, ErrUnexpectedFormat)
4949
}
5050
return d, nil
5151
}

parse_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const (
1313
type testParse struct {
1414
initData string
1515
expectedErr error
16-
expectedRes InitData
16+
expectedRes *InitData
1717
}
1818

1919
var testsParse = []testParse{
@@ -23,9 +23,9 @@ var testsParse = []testParse{
2323
},
2424
{
2525
initData: _parseTestInitData,
26-
expectedRes: InitData{
26+
expectedRes: &InitData{
2727
QueryID: "AAHdF6IQAAAAAN0XohDhrOrc",
28-
User: User{
28+
User: &User{
2929
ID: 279058397,
3030
FirstName: "Vladislav",
3131
LastName: "Kibenko",

user.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package initdata
22

33
// User describes user information:
4-
// https://docs.telegram-mini-apps.com/launch-parameters/init-data#user
4+
// https://docs.telegram-mini-apps.com/platform/init-data#user
55
type User struct {
66
// Optional. True, if this user added the bot to the attachment menu.
7-
AddedToAttachmentMenu bool `json:"added_to_attachment_menu"`
7+
AddedToAttachmentMenu bool `json:"added_to_attachment_menu,omitempty"`
88

99
// Optional. True, if this user allowed the bot to message them.
10-
AllowsWriteToPm bool `json:"allows_write_to_pm"`
10+
AllowsWriteToPm bool `json:"allows_write_to_pm,omitempty"`
1111

1212
// First name of the user or bot.
1313
FirstName string `json:"first_name"`
@@ -17,24 +17,24 @@ type User struct {
1717

1818
// Optional. True, if this user is a bot. Returned in the `receiver` field
1919
// only.
20-
IsBot bool `json:"is_bot"`
20+
IsBot bool `json:"is_bot,omitempty"`
2121

2222
// Optional. True, if this user is a Telegram Premium user.
23-
IsPremium bool `json:"is_premium"`
23+
IsPremium bool `json:"is_premium,omitempty"`
2424

2525
// Optional. Last name of the user or bot.
26-
LastName string `json:"last_name"`
26+
LastName string `json:"last_name,omitempty"`
2727

2828
// Optional. Username of the user or bot.
29-
Username string `json:"username"`
29+
Username string `json:"username,omitempty"`
3030

3131
// Optional. IETF language tag of the user's language. Returns in user
3232
// field only.
3333
// https://en.wikipedia.org/wiki/IETF_language_tag
34-
LanguageCode string `json:"language_code"`
34+
LanguageCode string `json:"language_code,omitempty"`
3535

3636
// Optional. URL of the user’s profile photo. The photo can be in .jpeg or
3737
// .svg formats. Only returned for Web Apps launched from the
3838
// attachment menu.
39-
PhotoURL string `json:"photo_url"`
39+
PhotoURL string `json:"photo_url,omitempty"`
4040
}

0 commit comments

Comments
 (0)