Skip to content
This repository has been archived by the owner on Dec 17, 2024. It is now read-only.

Commit

Permalink
refactor: Implement runCmd and update json structs
Browse files Browse the repository at this point in the history
  • Loading branch information
codablock committed Jan 8, 2024
1 parent 0b7de60 commit 555634c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
59 changes: 36 additions & 23 deletions jinja2_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func newPythonJinja2Renderer(j2 *Jinja2) (*pythonJinja2Renderer, error) {

func (j *pythonJinja2Renderer) Close() {
if j.stdin != nil {
args := jinja2Args{Cmd: "exit"}
args := jinja2Cmd{Cmd: "exit"}
_ = json.NewEncoder(j.stdin).Encode(args)

_ = j.stdin.Close()
Expand Down Expand Up @@ -133,20 +133,24 @@ func isMaybeTemplate(template string, isString bool) (bool, *string, error) {
return true, nil, nil
}

type jinja2Args struct {
type jinja2Cmd struct {
Cmd string `json:"cmd"`
Templates []string `json:"templates"`

Opts *jinja2Options `json:"opts"`
}

type jinja2Result struct {
type jinja2TemplateResult struct {
Result *string `json:"result,omitempty"`
Error *string `json:"error,omitempty"`
}

type jinja2CmdResult struct {
TemplateResults []jinja2TemplateResult `json:"templateResults,omitempty"`
}

func (j *pythonJinja2Renderer) renderHelper(jobs []*RenderJob, isString bool, opts []Jinja2Opt) error {
var jargs jinja2Args
var jargs jinja2Cmd
if isString {
jargs.Cmd = "render-strings"
} else {
Expand Down Expand Up @@ -191,14 +195,34 @@ func (j *pythonJinja2Renderer) renderHelper(jobs []*RenderJob, isString bool, op
return nil
}

cmdResult, err := j.runCmd(&jargs)
if err != nil {
return err
}

for i, item := range cmdResult.TemplateResults {
if item.Result != nil {
processedJobs[i].Result = item.Result
} else {
if item.Error == nil {
return fmt.Errorf("missing result and error from item at index %d", i)
}
processedJobs[i].Error = &Jinja2Error{*item.Error}
}
}

return nil
}

func (j *pythonJinja2Renderer) runCmd(jargs *jinja2Cmd) (*jinja2CmdResult, error) {
b, err := json.Marshal(jargs)
if err != nil {
j.Close()
return err
return nil, err
}
b = append(b, '\n')

if jargs.Opts.traceJsonSend != nil {
if jargs.Opts != nil && jargs.Opts.traceJsonSend != nil {
var m map[string]any
_ = json.Unmarshal(b, &m)
jargs.Opts.traceJsonSend(m)
Expand All @@ -207,43 +231,32 @@ func (j *pythonJinja2Renderer) renderHelper(jobs []*RenderJob, isString bool, op
_, err = j.stdin.Write(b)
if err != nil {
j.Close()
return err
return nil, err
}

line := bytes.NewBuffer(nil)
for true {
l, p, err := j.stdoutReader.ReadLine()
if err != nil {
return err
return nil, err
}
line.Write(l)
if !p {
break
}
}

if jargs.Opts.traceJsonReceive != nil {
if jargs.Opts != nil && jargs.Opts.traceJsonReceive != nil {
var m map[string]any
_ = json.Unmarshal(line.Bytes(), &m)
jargs.Opts.traceJsonReceive(m)
}

var result []jinja2Result
var result jinja2CmdResult
err = json.Unmarshal(line.Bytes(), &result)
if err != nil {
return err
}

for i, item := range result {
if item.Result != nil {
processedJobs[i].Result = item.Result
} else {
if item.Error == nil {
return fmt.Errorf("missing result and error from item at index %d", i)
}
processedJobs[i].Error = &Jinja2Error{*item.Error}
}
return nil, err
}

return nil
return &result, nil
}
23 changes: 14 additions & 9 deletions python_src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@

def main():
while True:
args = sys.stdin.readline()
if not args:
cmd = sys.stdin.readline()
if not cmd:
break
args = json.loads(args)
opts = args["opts"]
cmd = json.loads(cmd)
opts = cmd["opts"]

r = Jinja2Renderer(opts)

if args["cmd"] == "render-strings":
result = r.RenderStrings(args["templates"])
elif args["cmd"] == "render-files":
result = r.RenderFiles(args["templates"])
elif args["cmd"] == "exit":
result = {}
if cmd["cmd"] == "render-strings":
result = {
"templateResults": r.RenderStrings(cmd["templates"])
}
elif cmd["cmd"] == "render-files":
result = {
"templateResults": r.RenderFiles(cmd["templates"])
}
elif cmd["cmd"] == "exit":
break
else:
raise Exception("invalid cmd")
Expand Down

0 comments on commit 555634c

Please sign in to comment.