github.com/netdata/go.d.plugin@v0.58.1/modules/energid/jsonrpc.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package energid
     4  
     5  import (
     6  	"encoding/json"
     7  	"fmt"
     8  )
     9  
    10  // https://www.jsonrpc.org/specification#request_object
    11  type (
    12  	rpcRequest struct {
    13  		JSONRPC string `json:"jsonrpc"`
    14  		Method  string `json:"method"`
    15  		ID      int    `json:"id"`
    16  	}
    17  	rpcRequests []rpcRequest
    18  )
    19  
    20  // http://www.jsonrpc.org/specification#response_object
    21  type (
    22  	rpcResponse struct {
    23  		JSONRPC string          `json:"jsonrpc"`
    24  		Result  json.RawMessage `json:"result"`
    25  		Error   *rpcError       `json:"error"`
    26  		ID      int             `json:"id"`
    27  	}
    28  	rpcResponses []rpcResponse
    29  )
    30  
    31  func (rs rpcResponses) getByID(id int) *rpcResponse {
    32  	for _, r := range rs {
    33  		if r.ID == id {
    34  			return &r
    35  		}
    36  	}
    37  	return nil
    38  }
    39  
    40  // http://www.jsonrpc.org/specification#error_object
    41  type rpcError struct {
    42  	Code    int64  `json:"code"`
    43  	Message string `json:"message"`
    44  }
    45  
    46  func (e rpcError) String() string {
    47  	return fmt.Sprintf("%s (code %d)", e.Message, e.Code)
    48  }