Skip to content

Commit

Permalink
Fix start_param unmarshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
heyqbnk committed Feb 7, 2024
1 parent 797028e commit c0b4e34
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
10 changes: 9 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))
}

Expand Down
11 changes: 6 additions & 5 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand All @@ -34,6 +34,7 @@ var testsParse = []testParse{
},
CanSendAfterRaw: 0,
AuthDateRaw: 1662771648,
StartParam: "abc",
Hash: "c501b71e775f74ce10e377dea85a7ea24ecd640b223ea86dfe453e0eaed2e2b2",
},
},
Expand All @@ -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)
}
}
}

0 comments on commit c0b4e34

Please sign in to comment.