Skip to content

Some type can not unmarshal correctly. #1

@yg0x01

Description

@yg0x01

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions