Files
prysm/encoding/ssz/query/testutil/util.go
Jun Song 26d8b6b786 Initialize SSZ-QL package with support for fixed-size types (#15588)
* Add basic PathElement

* Add ssz_type.go

* Add basic sszInfo

* Add containerInfo

* Add basic analyzer without analyzing list/vector

* Add analyzer for homogeneous collection types

* Add offset/length calculator

* Add testutil package in encoding/ssz/query

* Add first round trip test for IndexedAttestationElectra

* Go mod tidy

* Add Print function for debugging purpose

* Add changelog

* Add testonly flag for testutil package & Nit for nogo

* Apply reviews from Radek

* Replace fastssz with prysmaticlabs one

* Add proto/ssz_query package for testing purpose

* Update encoding/ssz/query tests to decouple with beacon types

* Use require.* instead of assert.*

* Fix import name for proto ssz_query package

* Remove uint8/uint16 and some byte arrays in FixedTestContainer

* Add newline for files

* Fix comment about byte array in ssz_query.proto

---------

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
2025-08-25 14:29:26 +00:00

50 lines
1.3 KiB
Go

package testutil
import (
"fmt"
"reflect"
ssz "github.com/prysmaticlabs/fastssz"
)
// marshalAny marshals any value into SSZ format.
func marshalAny(value any) ([]byte, error) {
// First check if it implements ssz.Marshaler (this catches custom types like primitives.Epoch)
if marshaler, ok := value.(ssz.Marshaler); ok {
return marshaler.MarshalSSZ()
}
// Handle custom type aliases by checking if they're based on primitive types
valueType := reflect.TypeOf(value)
if valueType.PkgPath() != "" {
switch valueType.Kind() {
case reflect.Uint64:
return ssz.MarshalUint64(make([]byte, 0), reflect.ValueOf(value).Uint()), nil
case reflect.Uint32:
return ssz.MarshalUint32(make([]byte, 0), uint32(reflect.ValueOf(value).Uint())), nil
case reflect.Bool:
return ssz.MarshalBool(make([]byte, 0), reflect.ValueOf(value).Bool()), nil
}
}
switch v := value.(type) {
case []byte:
return v, nil
case []uint64:
buf := make([]byte, 0, len(v)*8)
for _, val := range v {
buf = ssz.MarshalUint64(buf, val)
}
return buf, nil
case uint64:
return ssz.MarshalUint64(make([]byte, 0), v), nil
case uint32:
return ssz.MarshalUint32(make([]byte, 0), v), nil
case bool:
return ssz.MarshalBool(make([]byte, 0), v), nil
default:
return nil, fmt.Errorf("unsupported type for SSZ marshalling: %T", value)
}
}