-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Kruse <[email protected]>
- Loading branch information
Showing
12 changed files
with
337 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"result": [ | ||
{ | ||
"name": "pool1", | ||
"id": "1", | ||
"healthy": true, | ||
"origins": [ | ||
{ | ||
"name": "origin1", | ||
"address": "127.0.0.1", | ||
"healthy": true | ||
}, | ||
{ | ||
"name": "origin2", | ||
"address": "192.168.1.1", | ||
"healthy": false | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "pool2", | ||
"id": "2", | ||
"healthy": true, | ||
"origins": [ | ||
{ | ||
"name": "origin3", | ||
"address": "10.0.0.1", | ||
"healthy": true | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "pool3", | ||
"id": "3", | ||
"healthy": false, | ||
"origins": [ | ||
{ | ||
"name": "origin4", | ||
"address": "10.0.0.1", | ||
"healthy": false | ||
} | ||
] | ||
} | ||
] | ||
} |
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,55 @@ | ||
package transformers | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/itchyny/gojq" | ||
) | ||
|
||
// JQTransformer struct for jq transformation | ||
type JQTransformer struct { | ||
Query string | ||
} | ||
|
||
// NewJQTransformer creates a new JQTransformer with a given query | ||
func NewJQTransformer(query string) JQTransformer { | ||
return JQTransformer{Query: query} | ||
} | ||
|
||
// Transform applies the jq filter transformation to the input data | ||
func (jq JQTransformer) Transform(data []byte) ([]byte, error) { | ||
return applyJQFilter(data, jq.Query) | ||
} | ||
|
||
// applyJQFilter uses gojq to apply a jq transformation to the input data | ||
func applyJQFilter(jsonData []byte, jqQuery string) ([]byte, error) { | ||
var input interface{} | ||
if err := json.Unmarshal(jsonData, &input); err != nil { | ||
return nil, err | ||
} | ||
|
||
query, err := gojq.Parse(jqQuery) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
iter := query.Run(input) | ||
var results []interface{} | ||
for { | ||
v, ok := iter.Next() | ||
if !ok { | ||
break | ||
} | ||
if err, ok := v.(error); ok { | ||
return nil, err | ||
} | ||
results = append(results, v) | ||
} | ||
|
||
// Convert the transformed result back to JSON []byte | ||
transformedJSON, err := json.Marshal(results) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return transformedJSON, 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,21 @@ | ||
package transformers | ||
|
||
import "fmt" | ||
|
||
type Transformer interface { | ||
Transform(data []byte) ([]byte, error) | ||
} | ||
|
||
type TransformationConfig struct { | ||
Type string | ||
Query string | ||
} | ||
|
||
func NewTransformer(config TransformationConfig) (Transformer, error) { | ||
switch config.Type { | ||
case "jq": | ||
return NewJQTransformer(config.Query), nil | ||
default: | ||
return nil, fmt.Errorf("unsupported transformer type: %s", config.Type) | ||
} | ||
} |
Oops, something went wrong.