Skip to content

Commit

Permalink
refactor: make make lint happy
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Nov 23, 2024
1 parent fc165e2 commit c8a4375
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions go/summarize/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
dbInfoFile
)

var fileTypeMap = map[string]fileType{
var fileTypeMap = map[string]fileType{ //nolint:gochecknoglobals // this is instead of a const
"trace": traceFile,
"keys": keysFile,
"dbinfo": dbInfoFile,
Expand All @@ -43,47 +43,46 @@ var fileTypeMap = map[string]fileType{
func getFileType(filename string) (fileType, error) {
file, err := os.Open(filename)
if err != nil {
return unknownFile, errors.New(fmt.Sprintf("error opening file: %v", err))
return unknownFile, fmt.Errorf("error opening file: %v", err)
}
defer file.Close()

decoder := json.NewDecoder(file)

token, err := decoder.Token()
if err != nil {
return unknownFile, errors.New(fmt.Sprintf("Error reading token: %v", err))
return unknownFile, fmt.Errorf("error reading token: %v", err)
}

if delim, ok := token.(json.Delim); !ok || delim != '{' {
return unknownFile, errors.New(fmt.Sprintf("Expected start of object '{'"))
return unknownFile, errors.New("expected start of object '{'")
}

for decoder.More() {
keyToken, err := decoder.Token()
if err != nil {
return unknownFile, errors.New(fmt.Sprintf("Error reading key token: %v", err))
}
if !decoder.More() {
return unknownFile, nil
}

key, ok := keyToken.(string)
if !ok {
return unknownFile, errors.New(fmt.Sprintf("Expected key to be a string: %s", keyToken))
}
keyToken, err := decoder.Token()
if err != nil {
return unknownFile, fmt.Errorf("error reading key token: %v", err)
}

valueToken, err := decoder.Token()
if err != nil {
return unknownFile, errors.New(fmt.Sprintf("Error reading value token: %v", err))
}
key, ok := keyToken.(string)
if !ok {
return unknownFile, fmt.Errorf("expected key to be a string: %s", keyToken)
}

if key == "fileType" {
if fileType, ok := fileTypeMap[valueToken.(string)]; ok {
return fileType, nil
} else {
return unknownFile, errors.New(fmt.Sprintf("Unknown FileType: %s", valueToken))
}
} else {
// Currently we expect the first key to be FileType, for optimization reasons
return unknownFile, nil
valueToken, err := decoder.Token()
if err != nil {
return unknownFile, fmt.Errorf("error reading value token: %v", err)
}

if key == "fileType" {
if fileType, ok := fileTypeMap[valueToken.(string)]; ok {
return fileType, nil
}
return unknownFile, fmt.Errorf("unknown FileType: %s", valueToken)
}

return unknownFile, nil
}

0 comments on commit c8a4375

Please sign in to comment.