From 43421707ddba9713db6d8da42e440b6a31284ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Thu, 11 Jul 2019 16:06:06 +0200 Subject: [PATCH] Apply Code Review patches --- integrations.go | 28 +++++++++++++++------------- sourcereader.go | 10 +++++----- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/integrations.go b/integrations.go index d3544a30f..408c93749 100644 --- a/integrations.go +++ b/integrations.go @@ -308,11 +308,16 @@ func (cfi *contextifyFramesIntegration) contextify(frames []Frame) []Frame { } var path string - // Optimize for happy path here - if fileExists(frame.AbsPath) { - path = frame.AbsPath + + if cachedPath, ok := cfi.cachedLocations[frame.AbsPath]; ok { + path = cachedPath } else { - path = cfi.findNearbySourceCodeLocation(frame.AbsPath) + // Optimize for happy path here + if fileExists(frame.AbsPath) { + path = frame.AbsPath + } else { + path = cfi.findNearbySourceCodeLocation(frame.AbsPath) + } } if path == "" { @@ -320,18 +325,14 @@ func (cfi *contextifyFramesIntegration) contextify(frames []Frame) []Frame { continue } - lines, initial := cfi.sr.readContextLines(path, frame.Lineno, cfi.contextLines) - contextifiedFrames = append(contextifiedFrames, cfi.addContextLinesToFrame(frame, lines, initial)) + lines, context_line := cfi.sr.readContextLines(path, frame.Lineno, cfi.contextLines) + contextifiedFrames = append(contextifiedFrames, cfi.addContextLinesToFrame(frame, lines, context_line)) } return contextifiedFrames } func (cfi *contextifyFramesIntegration) findNearbySourceCodeLocation(originalPath string) string { - if cachedPath, ok := cfi.cachedLocations[originalPath]; ok { - return cachedPath - } - trimmedPath := strings.TrimPrefix(originalPath, "/") components := strings.Split(trimmedPath, "/") @@ -345,15 +346,16 @@ func (cfi *contextifyFramesIntegration) findNearbySourceCodeLocation(originalPat } } + cfi.cachedLocations[originalPath] = "" return "" } -func (cfi *contextifyFramesIntegration) addContextLinesToFrame(frame Frame, lines [][]byte, initial int) Frame { +func (cfi *contextifyFramesIntegration) addContextLinesToFrame(frame Frame, lines [][]byte, context_line int) Frame { for i, line := range lines { switch { - case i < initial: + case i < context_line: frame.PreContext = append(frame.PreContext, string(line)) - case i == initial: + case i == context_line: frame.ContextLine = string(line) default: frame.PostContext = append(frame.PostContext, string(line)) diff --git a/sourcereader.go b/sourcereader.go index a772c0760..ac2392c02 100644 --- a/sourcereader.go +++ b/sourcereader.go @@ -36,12 +36,12 @@ func (sr *sourceReader) readContextLines(filename string, line, context int) ([] return sr.calculateContextLines(lines, line, context) } -// `initial` points to a line that's the `context_line` itself in relation to returned slice +// `context_line` points to a line that caused an issue itself, in relation to returned slice func (sr *sourceReader) calculateContextLines(lines [][]byte, line, context int) ([][]byte, int) { // Stacktrace lines are 1-indexed, slices are 0-indexed line-- - initial := context + context_line := context if lines == nil || line >= len(lines) || line < 0 { return nil, 0 @@ -49,13 +49,13 @@ func (sr *sourceReader) calculateContextLines(lines [][]byte, line, context int) if context < 0 { context = 0 - initial = 0 + context_line = 0 } start := line - context if start < 0 { - initial += start + context_line += start start = 0 } @@ -65,5 +65,5 @@ func (sr *sourceReader) calculateContextLines(lines [][]byte, line, context int) end = len(lines) } - return lines[start:end], initial + return lines[start:end], context_line }