-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move schema info definition from flag to parameters, add FileType to …
…schema info and deduce that type from the json input itself Signed-off-by: Rohit Nayak <[email protected]>
- Loading branch information
1 parent
a39acc3
commit db5e6bf
Showing
5 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package summarize | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
type fileType int | ||
|
||
const ( | ||
unknownFile fileType = iota | ||
traceFile | ||
keysFile | ||
dbInfoFile | ||
) | ||
|
||
var fileTypeMap = map[string]fileType{ | ||
"trace": traceFile, | ||
"keys": keysFile, | ||
"dbinfo": dbInfoFile, | ||
} | ||
|
||
// getFileType reads the first key-value pair from a JSON file and returns the type of the file | ||
// Note: | ||
func getFileType(filename string) (fileType, error) { | ||
// read json file | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return unknownFile, errors.New(fmt.Sprintf("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)) | ||
} | ||
|
||
// Ensure the token is the start of an object | ||
if delim, ok := token.(json.Delim); !ok || delim != '{' { | ||
return unknownFile, errors.New(fmt.Sprintf("Expected start of object '{'")) | ||
} | ||
|
||
// Read the key-value pairs | ||
for decoder.More() { | ||
// Read the key | ||
keyToken, err := decoder.Token() | ||
if err != nil { | ||
return unknownFile, errors.New(fmt.Sprintf("Error reading key token: %v", err)) | ||
} | ||
|
||
key, ok := keyToken.(string) | ||
if !ok { | ||
return unknownFile, errors.New(fmt.Sprintf("Expected key to be a string: %s", keyToken)) | ||
} | ||
|
||
// Read the value | ||
valueToken, err := decoder.Token() | ||
if err != nil { | ||
return unknownFile, errors.New(fmt.Sprintf("Error reading value token: %v", err)) | ||
} | ||
|
||
// Check if the key is "FileType" | ||
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 | ||
} | ||
} | ||
return unknownFile, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package summarize | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Test utils getFileType function | ||
func TestGetFileType(t *testing.T) { | ||
type testCase struct { | ||
filename string | ||
expectedType fileType | ||
expectedError string | ||
} | ||
testCases := []testCase{ | ||
{ | ||
filename: "../testdata/keys-log.json", | ||
expectedType: unknownFile, | ||
}, | ||
{ | ||
filename: "../testdata/sakila-schema-info.json", | ||
expectedType: dbInfoFile, | ||
}, | ||
{ | ||
filename: "../testdata/mysql.query.log", | ||
expectedType: unknownFile, | ||
expectedError: "Error reading token", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.filename, func(t *testing.T) { | ||
ft, err := getFileType(tc.filename) | ||
if tc.expectedError != "" { | ||
require.Error(t, err) | ||
} | ||
if err != nil { | ||
require.Contains(t, err.Error(), tc.expectedError) | ||
} | ||
if ft != tc.expectedType { | ||
t.Errorf("Expected type: %v, got: %v", tc.expectedType, ft) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"FileType": "dbinfo", | ||
"Tables": [ | ||
{ | ||
"Name": "actor", | ||
|