diff --git a/jinja2_renderer.go b/jinja2_renderer.go index 470d164..1d29550 100644 --- a/jinja2_renderer.go +++ b/jinja2_renderer.go @@ -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() @@ -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 { @@ -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) @@ -207,14 +231,14 @@ 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 { @@ -222,28 +246,17 @@ func (j *pythonJinja2Renderer) renderHelper(jobs []*RenderJob, isString bool, op } } - 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 } diff --git a/python_src/main.py b/python_src/main.py index 6e54651..4a44db1 100644 --- a/python_src/main.py +++ b/python_src/main.py @@ -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")