-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ability to use '$ref' to refer to external code files
- Loading branch information
Showing
4 changed files
with
81 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
def update(x): | ||
x["value"] = x["value"] + 1 | ||
return x |
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,26 @@ | ||
|
||
class: Playbook | ||
name: codeTest | ||
|
||
inputs: | ||
startData: | ||
embedded: | ||
- {"value": 0, "name": "alice"} | ||
- {"value": 1, "name": "bob"} | ||
- {"value": 2, "name": "charlie"} | ||
|
||
|
||
pipelines: | ||
codeTest: | ||
- from: startData | ||
- map: | ||
method: update | ||
gpython: | ||
$ref: code.py | ||
- map: | ||
method: update | ||
gpython: | | ||
def update(x): | ||
x["value"] = x["value"] + 1 | ||
return x | ||
- debug: {} |
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 transform | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type CodeBlock struct { | ||
Code string | ||
Ref string | ||
BaseDir string | ||
} | ||
|
||
func (cb *CodeBlock) UnmarshalJSON(data []byte) error { | ||
if err := json.Unmarshal(data, &cb.Code); err == nil { | ||
return nil | ||
} | ||
ref := map[string]any{} | ||
if err := json.Unmarshal(data, &ref); err == nil { | ||
if path, ok := ref["$ref"]; ok { | ||
if pathStr, ok := path.(string); ok { | ||
cb.Ref = pathStr | ||
return nil | ||
} | ||
} | ||
} | ||
return fmt.Errorf("unknown code block type") | ||
} | ||
|
||
func (cb *CodeBlock) SetBaseDir(path string) { | ||
cb.BaseDir = path | ||
} | ||
|
||
func (cb *CodeBlock) String() string { | ||
if cb.Ref != "" { | ||
path := filepath.Join(cb.BaseDir, cb.Ref) | ||
data, err := os.ReadFile(path) | ||
if err == nil { | ||
cb.Code = string(data) | ||
} | ||
} | ||
return cb.Code | ||
} |
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