go-tl-parser icon indicating copy to clipboard operation
go-tl-parser copied to clipboard

Some type can not unmarshal correctly.

Open yg0x01 opened this issue 7 years ago • 0 comments

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.

yg0x01 avatar Oct 16 '18 20:10 yg0x01