// Codec marshals structs (typically generated from a schema) to and from bytes. type Codec interface { // Name returns the name of the Codec. // // This may be used as part of the Content-Type within HTTP. For example, // with gRPC this is the content subtype, so "application/grpc+proto" will // map to the Codec with name "proto". // // Names must not be empty. Name() string // Marshal marshals the given message. // // Marshal may expect a specific type of message, and will error if this type // is not given. Marshal(interface{}) ([]byte, error) // Unmarshal unmarshals the given message. // // Unmarshal may expect a specific type of message, and will error if this // type is not given. Unmarshal([]byte, interface{}) error }
// readOnlyCodecs is a read-only interface to a map of named codecs. type readOnlyCodecs interface { // Get gets the Codec with the given name. Get(string) Codec // Protobuf gets the user-supplied protobuf codec, falling back to the default // implementation if necessary. // // This is helpful in the gRPC protocol, where the wire protocol requires // marshaling protobuf structs to binary even if the RPC procedures were // generated from a different IDL. Protobuf() Codec // Names returns a copy of the registered codec names. The returned slice is // safe for the caller to mutate. Names() []string }
// A Decompressor is a reusable wrapper that decompresses an underlying data // source. The standard library's [*gzip.Reader] implements Decompressor. type Decompressor interface { io.Reader
// Close closes the Decompressor, but not the underlying data source. It may // return an error if the Decompressor wasn't read to EOF. Close() error
// Reset discards the Decompressor's internal state, if any, and prepares it // to read from a new source of compressed data. Reset(io.Reader) error }
Compressor 接口定义了压缩方法,他继承了 io.Reader:
1 2 3 4 5 6 7 8 9 10 11 12 13
// A Compressor is a reusable wrapper that compresses data written to an // underlying sink. The standard library's [*gzip.Writer] implements Compressor. type Compressor interface { io.Writer
// Close flushes any buffered data to the underlying sink, then closes the // Compressor. It must not close the underlying sink. Close() error
// Reset discards the Compressor's internal state, if any, and prepares it to // write compressed data to a new sink. Reset(io.Writer) }
// readOnlyCompressionPools is a read-only interface to a map of named // compressionPools. type readOnlyCompressionPools interface { Get(string) *compressionPool Contains(string) bool // Wordy, but clarifies how this is different from readOnlyCodecs.Names(). CommaSeparatedNames() string }
funcnewReadOnlyCompressionPools( nameToPool map[string]*compressionPool, reversedNames []string, )readOnlyCompressionPools { // Client and handler configs keep compression names in registration order, // but we want the last registered to be the most preferred. names := make([]string, 0, len(reversedNames)) seen := make(map[string]struct{}, len(reversedNames)) for i := len(reversedNames) - 1; i >= 0; i-- { name := reversedNames[i] if _, ok := seen[name]; ok { continue } seen[name] = struct{}{} names = append(names, name) } return &namedCompressionPools{ nameToPool: nameToPool, commaSeparatedNames: strings.Join(names, ","), } }
type namedCompressionPools struct { nameToPool map[string]*compressionPool commaSeparatedNames string }