ReadMessage reads a framed message and unmarshals it based on tag. The returned buf should be passed back on the next call for buffer reuse.
(r io.Reader, buf []byte)
| 147 | // ReadMessage reads a framed message and unmarshals it based on tag. The |
| 148 | // returned buf should be passed back on the next call for buffer reuse. |
| 149 | func ReadMessage(r io.Reader, buf []byte) (proto.Message, []byte, error) { |
| 150 | tag, data, err := ReadFrame(r, buf) |
| 151 | if err != nil { |
| 152 | return nil, data, err |
| 153 | } |
| 154 | |
| 155 | var msg proto.Message |
| 156 | switch tag { |
| 157 | case TagV1: |
| 158 | var req agentproto.ReportBoundaryLogsRequest |
| 159 | if err := proto.Unmarshal(data, &req); err != nil { |
| 160 | return nil, data, xerrors.Errorf("unmarshal TagV1: %w", err) |
| 161 | } |
| 162 | msg = &req |
| 163 | case TagV2: |
| 164 | var envelope BoundaryMessage |
| 165 | if err := proto.Unmarshal(data, &envelope); err != nil { |
| 166 | return nil, data, xerrors.Errorf("unmarshal TagV2: %w", err) |
| 167 | } |
| 168 | msg = &envelope |
| 169 | default: |
| 170 | // maxSizeForTag already rejects unknown tags during ReadFrame, |
| 171 | // but handle it here for safety. |
| 172 | return nil, data, xerrors.Errorf("%w: %d", ErrUnsupportedTag, tag) |
| 173 | } |
| 174 | |
| 175 | return msg, data, nil |
| 176 | } |
| 177 | |
| 178 | // WriteMessage marshals a proto message and writes it as a framed message |
| 179 | // with the given tag. |
no test coverage detected