Skip to content

Commit

Permalink
always validate json files
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardev committed Aug 10, 2019
1 parent 52fde60 commit 90f1a97
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
19 changes: 12 additions & 7 deletions internal/exporter/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,27 @@ func (e *exporter) isExtensionValid(extension string) bool {
func (e *exporter) parseFile(filePath string, value string, localData map[string]string) {
// Extract our file extension and cleanup file path
ext := filepath.Ext(filePath)
path := e.cleanFilePath(filePath)
// Check if we should parse JSON files
if e.config.ShouldExpandJSON() {
// Check if the file is a JSON one
if ext == ".json" {
cleanedPath := e.cleanFilePath(filePath)

// Check if the file is a JSON one
if ext == ".json" {
// Check if we should parse JSON files
if e.config.ShouldExpandJSON() {
// Great, we should iterate our JSON (And that's the value)
e.expandJSON(path, value, localData)
e.expandJSON(cleanedPath, value, localData)

// we must return here, to avoid importing the file as blob
return
}

// Not expanding json file, but we should validate anyways
// HEADS UP: Below function will exit program if any error found
_ = e.validateJSON(cleanedPath, value)
}

// Not expanding JSON files, create new single "piece" with the
// value given (the file content) and add to collection
piece := e.createPiece(path, value)
piece := e.createPiece(cleanedPath, value)
localData[piece.KVPath] = piece.Value
}

Expand Down
13 changes: 10 additions & 3 deletions internal/exporter/jsonexpander.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"strconv"
)

// expandJSON ...
func (e *exporter) expandJSON(path string, jsonData string, localData map[string]string) {
// validateJSON ...
func (e *exporter) validateJSON(path string, jsonData string) map[string]interface{} {
// Create "generic" json struct
var arbitraryJSON map[string]interface{}

Expand All @@ -20,12 +20,19 @@ func (e *exporter) expandJSON(path string, jsonData string, localData map[string
// Decoded JSON ok?
if err != nil {
util.ExitError(
errors.New(fmt.Sprintf("error parsing JSON file: %s", err.Error())),
errors.New(fmt.Sprintf("error parsing JSON file: %s with Message: %s", path, err.Error())),
util.ErrorFailedJsonDecode,
e.logger,
)
}

return arbitraryJSON
}

// expandJSON ...
func (e *exporter) expandJSON(path string, jsonData string, localData map[string]string) {
arbitraryJSON := e.validateJSON(path, jsonData)

// Iterate over our "generic" JSON structure
e.traverseJSON(path, arbitraryJSON, localData)
}
Expand Down

0 comments on commit 90f1a97

Please sign in to comment.