Update types.go

This commit is contained in:
oxBoni 2025-11-27 00:41:50 +01:00 committed by GitHub
parent 7805e203f0
commit de38537efc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -826,10 +826,10 @@ func formatPrimitiveValue(encType string, encValue interface{}) (string, error)
return fmt.Sprintf("%t", boolValue), nil return fmt.Sprintf("%t", boolValue), nil
} }
case "bytes", "string": case "bytes", "string":
return fmt.Sprintf("%s", encValue), nil return stringifyPrimitive(encType, encValue)
} }
if strings.HasPrefix(encType, "bytes") { if strings.HasPrefix(encType, "bytes") {
return fmt.Sprintf("%s", encValue), nil return stringifyPrimitive(encType, encValue)
} }
if strings.HasPrefix(encType, "uint") || strings.HasPrefix(encType, "int") { if strings.HasPrefix(encType, "uint") || strings.HasPrefix(encType, "int") {
if b, err := parseInteger(encType, encValue); err != nil { if b, err := parseInteger(encType, encValue); err != nil {
@ -841,6 +841,21 @@ func formatPrimitiveValue(encType string, encValue interface{}) (string, error)
return "", fmt.Errorf("unhandled type %v", encType) return "", fmt.Errorf("unhandled type %v", encType)
} }
func stringifyPrimitive(encType string, encValue interface{}) (string, error) {
switch v := encValue.(type) {
case string:
return v, nil
case []byte:
return string(v), nil
case hexutil.Bytes:
return v.String(), nil
case fmt.Stringer:
return v.String(), nil
default:
return "", fmt.Errorf("could not format value %v as %s", encValue, encType)
}
}
// validate checks if the types object is conformant to the specs // validate checks if the types object is conformant to the specs
func (t Types) validate() error { func (t Types) validate() error {
for typeKey, typeArr := range t { for typeKey, typeArr := range t {