Write codec data as byte.
(d *CodecData)
| 109 | |
| 110 | // Write codec data as byte. |
| 111 | func (c *jsonClientCodec) Write(d *CodecData) ([]byte, error) { |
| 112 | // If return error: it will be returned as is for this call. |
| 113 | // Allow param to be only Array, Slice, Map or Struct. |
| 114 | // When param is nil or uninitialized Map or Slice - omit "params". |
| 115 | param := d.Args |
| 116 | if param != nil { |
| 117 | switch k := reflect.TypeOf(param).Kind(); k { |
| 118 | case reflect.Map: |
| 119 | if reflect.TypeOf(param).Key().Kind() == reflect.String && reflect.ValueOf(param).IsNil() { |
| 120 | param = nil |
| 121 | } |
| 122 | case reflect.Slice: |
| 123 | if reflect.ValueOf(param).IsNil() { |
| 124 | param = nil |
| 125 | } |
| 126 | case reflect.Array, reflect.Struct: |
| 127 | case reflect.Ptr: |
| 128 | switch ptrK := reflect.TypeOf(param).Elem().Kind(); ptrK { |
| 129 | case reflect.Map: |
| 130 | if reflect.TypeOf(param).Elem().Key().Kind() == reflect.String && reflect.ValueOf(param).Elem().IsNil() { |
| 131 | param = nil |
| 132 | } |
| 133 | case reflect.Slice: |
| 134 | if reflect.ValueOf(param).Elem().IsNil() { |
| 135 | param = nil |
| 136 | } |
| 137 | case reflect.Array, reflect.Struct: |
| 138 | default: |
| 139 | return nil, perrors.New("unsupported param type: Ptr to " + ptrK.String()) |
| 140 | } |
| 141 | default: |
| 142 | return nil, perrors.New("unsupported param type: " + k.String()) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | c.req.Version = "2.0" |
| 147 | c.req.Method = d.Method |
| 148 | c.req.Params = param |
| 149 | c.req.ID = d.ID & MAX_JSONRPC_ID |
| 150 | // can not use d.ID. otherwise you will get error: can not find method of response id 280698512 |
| 151 | c.pending[c.req.ID] = d.Method |
| 152 | |
| 153 | buf := bytes.NewBuffer(nil) |
| 154 | defer buf.Reset() |
| 155 | enc := json.NewEncoder(buf) |
| 156 | if err := enc.Encode(&c.req); err != nil { |
| 157 | return nil, perrors.WithStack(err) |
| 158 | } |
| 159 | |
| 160 | return buf.Bytes(), nil |
| 161 | } |
| 162 | |
| 163 | // Read bytes as structured data |
| 164 | func (c *jsonClientCodec) Read(streamBytes []byte, x any) error { |