Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some type can not unmarshal correctly. #1

Open
yg0x01 opened this issue Oct 16, 2018 · 0 comments
Open

Some type can not unmarshal correctly. #1

yg0x01 opened this issue Oct 16, 2018 · 0 comments

Comments

@yg0x01
Copy link

yg0x01 commented Oct 16, 2018

Hi @Arman92 , I have use your awesome td-lib for some days. Today I found when I call GetInlineQueryResults methon and I got an error:

cannot unmarshal object into Go struct field InlineQueryResults.results of type tdlib.InlineQueryResult

I trace the error and found unmarshalInlineQueryResult have no used by any function of type.go and method.go. And InlineQueryResults have not implement UnmarshalJSON function.
I try to write it by hand with a simple code:

// UnmarshalJSON unmarshal to json
func (inlineQueryResults *InlineQueryResults) UnmarshalJSON(b []byte) error {
	var objMap map[string]*json.RawMessage
	err := json.Unmarshal(b, &objMap)
	if err != nil {
		return err
	}
	tempObj := struct {
		tdCommon
		InlineQueryID     JSONInt64 `json:"inline_query_id"`     // Unique identifier of the inline query
		NextOffset        string    `json:"next_offset"`         // The offset for the next request. If empty, there are no more results
		SwitchPmText      string    `json:"switch_pm_text"`      // If non-empty, this text should be shown on the button, which opens a private chat with the bot and sends the bot a start message with the switch_pm_parameter
		SwitchPmParameter string    `json:"switch_pm_parameter"` // Parameter for the bot start message
	}{}
	err = json.Unmarshal(b, &tempObj)
	if err != nil {
		return err
	}

	inlineQueryResults.tdCommon = tempObj.tdCommon
	inlineQueryResults.InlineQueryID = tempObj.InlineQueryID
	inlineQueryResults.NextOffset = tempObj.NextOffset
	inlineQueryResults.SwitchPmText = tempObj.SwitchPmText
	inlineQueryResults.SwitchPmParameter = tempObj.SwitchPmParameter
	var Results []InlineQueryResult
	var tempResults []*json.RawMessage
	byteResults, err := objMap["results"].MarshalJSON()
	if err != nil {
		return err
	}
	err = json.Unmarshal(byteResults, &tempResults)
	if err != nil {
		return err
	}
	for _, r := range tempResults {
		fieldResult, _ := unmarshalInlineQueryResult(r)
		Results = append(Results, fieldResult)
	}
	inlineQueryResults.Results = Results

	return nil
}

and the error gone. So I think the parser may have some bugs and cause some necessary code not generated. But the parser code is a bit complex and I dont know how to fix it now. Can you check it out? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant