ParseProtoReader parses a compressed proto from an io.Reader.
(ctx context.Context, reader io.Reader, expectedSize, maxSize int, req proto.Message, compression CompressionType)
| 136 | |
| 137 | // ParseProtoReader parses a compressed proto from an io.Reader. |
| 138 | func ParseProtoReader(ctx context.Context, reader io.Reader, expectedSize, maxSize int, req proto.Message, compression CompressionType) error { |
| 139 | sp := trace.SpanFromContext(ctx) |
| 140 | if sp != nil { |
| 141 | sp.SetAttributes(attribute.String("event", "util.ParseProtoRequest[start reading]")) |
| 142 | } |
| 143 | body, err := decompressRequest(reader, expectedSize, maxSize, compression, sp) |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | |
| 148 | if sp != nil { |
| 149 | sp.SetAttributes(attribute.String("event", "util.ParseProtoRequest[unmarshal]"), attribute.Int("size", len(body))) |
| 150 | } |
| 151 | |
| 152 | // We re-implement proto.Unmarshal here as it calls XXX_Unmarshal first, |
| 153 | // which we can't override without upsetting golint. |
| 154 | req.Reset() |
| 155 | if u, ok := req.(proto.Unmarshaler); ok { |
| 156 | err = u.Unmarshal(body) |
| 157 | } else { |
| 158 | err = proto.NewBuffer(body).Unmarshal(req) |
| 159 | } |
| 160 | if err != nil { |
| 161 | return err |
| 162 | } |
| 163 | |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | func decompressRequest(reader io.Reader, expectedSize, maxSize int, compression CompressionType, sp trace.Span) (body []byte, err error) { |
| 168 | defer func() { |
nothing calls this directly
no test coverage detected