mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 04:57:17 +00:00
30 lines
839 B
Go
30 lines
839 B
Go
package hexutility
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"reflect"
|
|
)
|
|
|
|
// UnmarshalFixedJSON decodes the input as a string with 0x prefix. The length of out
|
|
// determines the required input length. This function is commonly used to implement the
|
|
// UnmarshalJSON method for fixed-size types.
|
|
func UnmarshalFixedJSON(typ reflect.Type, input, out []byte) error {
|
|
if !isString(input) {
|
|
return &json.UnmarshalTypeError{Value: "non-string", Type: typ}
|
|
}
|
|
return wrapTypeError(UnmarshalFixedText(typ.String(), input[1:len(input)-1], out), typ)
|
|
}
|
|
|
|
func isString(input []byte) bool {
|
|
return len(input) >= 2 && input[0] == '"' && input[len(input)-1] == '"'
|
|
}
|
|
|
|
func wrapTypeError(err error, typ reflect.Type) error {
|
|
var dec *decError
|
|
if errors.As(err, &dec) {
|
|
return &json.UnmarshalTypeError{Value: err.Error(), Type: typ}
|
|
}
|
|
return err
|
|
}
|