gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/codec/codec.go (about) 1 // Package codec is an interface for encoding messages 2 package codec 3 4 import ( 5 "io" 6 ) 7 8 const ( 9 Error MessageType = iota 10 Request 11 Response 12 Event 13 ) 14 15 type MessageType int 16 17 // Takes in a connection/buffer and returns a new Codec 18 type NewCodec func(io.ReadWriteCloser) Codec 19 20 // Codec encodes/decodes various types of messages used within go-micro. 21 // ReadHeader and ReadBody are called in pairs to read requests/responses 22 // from the connection. Close is called when finished with the 23 // connection. ReadBody may be called with a nil argument to force the 24 // body to be read and discarded. 25 type Codec interface { 26 Reader 27 Writer 28 Close() error 29 String() string 30 } 31 32 type Reader interface { 33 ReadHeader(*Message, MessageType) error 34 ReadBody(interface{}) error 35 } 36 37 type Writer interface { 38 Write(*Message, interface{}) error 39 } 40 41 // Marshaler is a simple encoding interface used for the broker/transport 42 // where headers are not supported by the underlying implementation. 43 type Marshaler interface { 44 Marshal(interface{}) ([]byte, error) 45 Unmarshal([]byte, interface{}) error 46 String() string 47 } 48 49 // Message represents detailed information about 50 // the communication, likely followed by the body. 51 // In the case of an error, body may be nil. 52 type Message struct { 53 Id string 54 Type MessageType 55 Target string 56 Method string 57 Endpoint string 58 Error string 59 60 // The values read from the socket 61 Header map[string]string 62 Body []byte 63 }