-
Notifications
You must be signed in to change notification settings - Fork 12
/
codec.go
48 lines (40 loc) · 1.63 KB
/
codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package types
import (
"context"
)
const (
ErrFieldNotFound = InvalidArgumentError("field not found")
ErrInvalidEncoding = InvalidArgumentError("invalid encoding")
ErrSliceWrongLen = InvalidArgumentError("slice is wrong length")
ErrNotASlice = InvalidArgumentError("element is not a slice")
)
type Encoder interface {
Encode(ctx context.Context, item any, itemType string) ([]byte, error)
// GetMaxEncodingSize returns the max size in bytes if n elements are supplied for all top level dynamically sized elements.
// If no elements are dynamically sized, the returned value will be the same for all n.
// If there are multiple levels of dynamically sized elements, or itemType cannot be found,
// ErrInvalidType will be returned.
GetMaxEncodingSize(ctx context.Context, n int, itemType string) (int, error)
}
type Decoder interface {
Decode(ctx context.Context, raw []byte, into any, itemType string) error
// GetMaxDecodingSize returns the max size in bytes if n elements are supplied for all top level dynamically sized elements.
// If no elements are dynamically sized, the returned value will be the same for all n.
// If there are multiple levels of dynamically sized elements, or itemType cannot be found,
// ErrInvalidType will be returned.
GetMaxDecodingSize(ctx context.Context, n int, itemType string) (int, error)
}
type Codec interface {
Encoder
Decoder
}
type TypeProvider interface {
CreateType(itemType string, forEncoding bool) (any, error)
}
type ContractTypeProvider interface {
CreateContractType(contractName, itemType string, forEncoding bool) (any, error)
}
type RemoteCodec interface {
Codec
TypeProvider
}