Skip to content

Commit

Permalink
Adding ability to use '$ref' to refer to external code files
Browse files Browse the repository at this point in the history
  • Loading branch information
kellrott committed Jan 12, 2024
1 parent bb5c66f commit 293114e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
4 changes: 4 additions & 0 deletions test/examples/code-ref/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

def update(x):
x["value"] = x["value"] + 1
return x
26 changes: 26 additions & 0 deletions test/examples/code-ref/pipeline.yaml
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: {}
45 changes: 45 additions & 0 deletions transform/code_block.go
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
}
11 changes: 6 additions & 5 deletions transform/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

type MapStep struct {
Method string `json:"method" jsonschema_description:"Name of function to call"`
Python string `json:"python" jsonschema_description:"Python code to be run"`
GPython string `json:"gpython" jsonschema_description:"Python code to be run using GPython"`
Method string `json:"method" jsonschema_description:"Name of function to call"`
Python string `json:"python" jsonschema_description:"Python code to be run"`
GPython *CodeBlock `json:"gpython" jsonschema_description:"Python code to be run using GPython"`
}

type mapProcess struct {
Expand All @@ -30,10 +30,11 @@ func (ms *MapStep) Init(task task.RuntimeTask) (Processor, error) {
log.Printf("Compile Error: %s", err)
}
return &mapProcess{ms, c}, nil
} else if ms.GPython != "" {
} else if ms.GPython != nil {
log.Printf("Init Map: %s", ms.GPython)
ms.GPython.SetBaseDir(task.BaseDir())
e := evaluate.GetEngine("gpython", task.WorkDir())
c, err := e.Compile(ms.GPython, ms.Method)
c, err := e.Compile(ms.GPython.String(), ms.Method)
if err != nil {
log.Printf("Compile Error: %s", err)
}
Expand Down

0 comments on commit 293114e

Please sign in to comment.