Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions sdks/go/pkg/beam/core/graph/coder/coder.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func validateDecoder(t reflect.Type, decode any) error {

// NewCustomCoder creates a coder for the supplied parameters defining a
// particular encoding strategy.
//
// encode and decode must be functions matching one of the signatures documented
// on RegisterCoder. NewCustomCoder returns an error if either function fails
// signature validation.
func NewCustomCoder(id string, t reflect.Type, encode, decode any) (*CustomCoder, error) {
if err := validateEncoder(t, encode); err != nil {
return nil, errors.WithContext(err, "NewCustomCoder")
Expand Down
25 changes: 25 additions & 0 deletions sdks/go/pkg/beam/core/graph/coder/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ var (
// over to check if element types implement them.
//
// Repeated registrations of the same type overrides prior ones.
//
// The enc and dec arguments must be functions matching one of the following
// signatures, where T is the user type registered (the concrete type
// represented by t, or an interface implemented by encoded values):
//
// Supported encoder signatures:
//
// func(T) []byte
// func(reflect.Type, T) []byte
// func(T) ([]byte, error)
// func(reflect.Type, T) ([]byte, error)
//
// Supported decoder signatures:
//
// func([]byte) T
// func(reflect.Type, []byte) T
// func([]byte) (T, error)
// func(reflect.Type, []byte) (T, error)
//
// The optional leading reflect.Type parameter is set to the concrete element
// type at coder construction time; it lets a single (enc, dec) pair serve
// multiple types (for example, when t is an interface).
//
// Passing a function that does not match one of these signatures will cause
// RegisterCoder to panic.
func RegisterCoder(t reflect.Type, enc, dec any) {
if _, err := NewCustomCoder(t.String(), t, enc, dec); err != nil {
panic(errors.Wrapf(err, "RegisterCoder failed for type %v", t))
Expand Down
Loading