forked from blues/jsonata-go
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added new function ObjectsToDocument * lint & fixes * Updated to remove type conversion as already converted in the JSONata * add some errors --------- Co-authored-by: James Weeks <[email protected]> Co-authored-by: tbal999 <[email protected]>
- Loading branch information
1 parent
44fd010
commit bbead99
Showing
14 changed files
with
164 additions
and
34 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
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
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
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
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,65 @@ | ||
package jsonata | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type JsonataProcessor struct { | ||
tree *Expr | ||
} | ||
|
||
func NewProcessor(jsonataString string) (j *JsonataProcessor, err error) { | ||
defer func() { // go-jsonata uses panic fallthrough design so this is necessary | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("jsonata error: %v", r) | ||
} | ||
}() | ||
|
||
jsnt := replaceQuotesAndCommentsInPaths(jsonataString) | ||
|
||
e := MustCompile(jsnt) | ||
|
||
j = &JsonataProcessor{} | ||
|
||
j.tree = e | ||
|
||
return j, err | ||
} | ||
|
||
// Execute - helper function that lets you parse and run jsonata scripts against an object | ||
func (j *JsonataProcessor) Execute(input interface{}) (output []map[string]interface{}, err error) { | ||
defer func() { // go-jsonata uses panic fallthrough design so this is necessary | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("jsonata error: %v", r) | ||
} | ||
}() | ||
|
||
output = make([]map[string]interface{}, 0) | ||
|
||
item, err := j.tree.Eval(input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if aMap, ok := item.(map[string]interface{}); ok { | ||
output = append(output, aMap) | ||
|
||
return output, nil | ||
} | ||
|
||
if aList, ok := item.([]interface{}); ok { | ||
for index := range aList { | ||
if aMap, ok := aList[index].(map[string]interface{}); ok { | ||
output = append(output, aMap) | ||
} | ||
} | ||
|
||
return output, nil | ||
} | ||
|
||
if aList, ok := item.([]map[string]interface{}); ok { | ||
return aList, nil | ||
} | ||
|
||
return output, nil | ||
} |