From c0b4e34cfa497c64eb7de0c38ff1662cf24b0607 Mon Sep 17 00:00:00 2001 From: Vladislav Kibenko Date: Wed, 7 Feb 2024 22:01:21 +0300 Subject: [PATCH] Fix start_param unmarshalling --- parse.go | 10 +++++++++- parse_test.go | 11 ++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/parse.go b/parse.go index 7387f14..40e9b05 100644 --- a/parse.go +++ b/parse.go @@ -7,6 +7,13 @@ import ( "strings" ) +var ( + // List of properties which should always be interpreted as strings. + _stringProps = map[string]bool{ + "start_param": true, + } +) + // Parse converts passed init data presented as query string to InitData // object. func Parse(initData string) (InitData, error) { @@ -27,9 +34,10 @@ func Parse(initData string) (InitData, error) { // If passed value is valid in the context of JSON, it means, we could // insert this value without formatting. - if json.Valid([]byte(val)) { + if isString := _stringProps[k]; !isString && json.Valid([]byte(val)) { valFormat = "%q:%s" } + pairs = append(pairs, fmt.Sprintf(valFormat, k, val)) } diff --git a/parse_test.go b/parse_test.go index 2f05810..ecabc7c 100644 --- a/parse_test.go +++ b/parse_test.go @@ -6,7 +6,7 @@ import ( ) const ( - parseTestInitData = "query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2" + _parseTestInitData = "query_id=AAHdF6IQAAAAAN0XohDhrOrc&user=%7B%22id%22%3A279058397%2C%22first_name%22%3A%22Vladislav%22%2C%22last_name%22%3A%22Kibenko%22%2C%22username%22%3A%22vdkfrost%22%2C%22language_code%22%3A%22ru%22%2C%22is_premium%22%3Atrue%7D&auth_date=1662771648&hash=c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2&start_param=abc" ) type testParse struct { @@ -17,11 +17,11 @@ type testParse struct { var testsParse = []testParse{ { - initData: parseTestInitData + ";", + initData: _parseTestInitData + ";", expectedErr: ErrUnexpectedFormat, }, { - initData: parseTestInitData, + initData: _parseTestInitData, expectedRes: InitData{ QueryID: "AAHdF6IQAAAAAN0XohDhrOrc", User: User{ @@ -34,6 +34,7 @@ var testsParse = []testParse{ }, CanSendAfterRaw: 0, AuthDateRaw: 1662771648, + StartParam: "abc", Hash: "c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2", }, }, @@ -42,9 +43,9 @@ var testsParse = []testParse{ func TestParse(t *testing.T) { for _, test := range testsParse { if data, err := Parse(test.initData); err != test.expectedErr { - t.Errorf("expected error to be %q. Received %q", test.expectedErr, err) + t.Errorf("expected error to be %q. \nReceived %q", test.expectedErr, err) } else if !reflect.DeepEqual(data, test.expectedRes) { - t.Errorf("expected result to be %+v. Received %+v", test.expectedRes, data) + t.Errorf("expected result to be %+v. \nReceived %+v", test.expectedRes, data) } } }