MCPcopy Create free account
hub / github.com/IBM/newrelic-cli / Decode

Method Decode

utils/decoder.go:214–250  ·  view source on GitHub ↗

Decode unmarshals the next object from the underlying stream into the provide object, or returns an error.

(into interface{})

Source from the content-addressed store, hash-verified

212// Decode unmarshals the next object from the underlying stream into the
213// provide object, or returns an error.
214func (d *YAMLOrJSONDecoder) Decode(into interface{}) error {
215 if d.decoder == nil {
216 buffer, origData, isJSON := GuessJSONStream(d.r, d.bufferSize)
217 if isJSON {
218 // fmt.Println("[DEBUG] decoding stream as JSON")
219 d.decoder = json.NewDecoder(buffer)
220 d.rawData = origData
221 } else {
222 // fmt.Println("[DEBUG] decoding stream as YAML")
223 d.decoder = NewYAMLToJSONDecoder(buffer)
224 }
225 }
226 err := d.decoder.Decode(into)
227 if jsonDecoder, ok := d.decoder.(*json.Decoder); ok {
228 if syntax, ok := err.(*json.SyntaxError); ok {
229 data, readErr := ioutil.ReadAll(jsonDecoder.Buffered())
230 if readErr != nil {
231 fmt.Printf("reading stream failed: %v\n", readErr)
232 }
233 js := string(data)
234
235 // if contents from io.Reader are not complete,
236 // use the original raw data to prevent panic
237 if int64(len(js)) <= syntax.Offset {
238 js = string(d.rawData)
239 }
240
241 start := strings.LastIndex(js[:syntax.Offset], "\n") + 1
242 line := strings.Count(js[:start], "\n")
243 return JSONSyntaxError{
244 Line: line,
245 Err: fmt.Errorf(syntax.Error()),
246 }
247 }
248 }
249 return err
250}
251
252type Reader interface {
253 Read() ([]byte, error)

Callers

nothing calls this directly

Calls 4

GuessJSONStreamFunction · 0.85
NewYAMLToJSONDecoderFunction · 0.85
DecodeMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected