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

[feature] DecodeClientResponse to allow 'reply == nil' #86

Open
pvyleta opened this issue Jun 22, 2021 · 0 comments
Open

[feature] DecodeClientResponse to allow 'reply == nil' #86

pvyleta opened this issue Jun 22, 2021 · 0 comments

Comments

@pvyleta
Copy link

pvyleta commented Jun 22, 2021

Is your feature request related to a problem? Please describe.

When I send JSON2-RPC (and others), I would like to check if there were any errors, but I do not need to parse the content of the message, for example, because I know it will always be empty. Currently, the DecodeClientResponse() will fail if I provide parameter reply == nil, as the Unmarshall() is not able to handle it. It would make sense in my eyes to allow for such an option.

Describe the solution you'd like

The API change would be negligible - only from now on, on providing reply == nil, an error would be returned only if the RPC itself would return an error. it would be enough just to add:

if reply == nil {
    return nil
}

to the DecodeClientResponse() code:

// DecodeClientResponse decodes the response body of a client request into
// the interface reply.
func DecodeClientResponse(r io.Reader, reply interface{}) error {
    var c clientResponse
    if err := json.NewDecoder(r).Decode(&c); err != nil {
	    return err
    }
    if c.Error != nil {
	    jsonErr := &Error{}
	    if err := json.Unmarshal(*c.Error, jsonErr); err != nil {
		    return &Error{
			    Code:    E_SERVER,
			    Message: string(*c.Error),
		    }
	    }
	    return jsonErr
    }

    if c.Result == nil {
	    return ErrNullResult
    }

    if reply == nil {
	    return nil
    }

    return json.Unmarshal(*c.Result, reply)
}

Describe alternatives you've considered

The workaround is to set the reply to a struct with string inside and ignore the results. This seems unnecessarily complicated.

@pvyleta pvyleta changed the title [feature] [feature] DecodeClientResponse to allow 'reply == nil' Jun 22, 2021
ryanrishi added a commit to ryanrishi/glinet-client-go that referenced this issue Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

1 participant