Skip to content

Commit

Permalink
better In processing, as a cue.Value with Unify; templates rendering …
Browse files Browse the repository at this point in the history
…correctly

Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Dec 27, 2021
1 parent cfe3593 commit ee68b37
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 22 deletions.
12 changes: 10 additions & 2 deletions lib/gen/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"

"cuelang.org/go/cue"
"github.com/epiclabs-io/diff3"
"github.com/sergi/go-diff/diffmatchpatch"

Expand All @@ -16,7 +17,8 @@ import (

type File struct {
// Input Data, local to this file
In map[string]interface{}
// In map[string]interface{}
In cue.Value

// The full path under the output location
// empty implies don't generate, even though it may endup in the list
Expand Down Expand Up @@ -220,7 +222,13 @@ func (F *File) UnifyContent() (write bool, err error) {
func (F *File) RenderTemplate() error {
var err error

F.RenderContent, err = F.TemplateInstance.Render(F.In)
In := make(map[string]interface{})
err = F.In.Decode(&In)
if err != nil {
return err
}

F.RenderContent, err = F.TemplateInstance.Render(In)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion lib/gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ type Generator struct {
Outdir string

// "Global" input, merged with out replacing onto the files
In map[string]interface{}
// In map[string]interface{}
In cue.Value

// The list fo files for hof to generate, in cue values
Out []*File
Expand Down
32 changes: 29 additions & 3 deletions lib/gen/loadcue.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func (G *Generator) loadIn() error {
return val.Err()
}

G.In = make(map[string]interface{})
return val.Decode(&G.In)
G.In = val
return nil
}

func (G *Generator) loadTemplates() error {
Expand Down Expand Up @@ -180,7 +180,33 @@ func (G *Generator) loadOut() error {
}

G.Out = make([]*File, 0)
return val.Decode(&G.Out)
err := val.Decode(&G.Out)
if err != nil {
return err
}

// need this extra work to load In into a cue.Value
L, err := val.List()
if err != nil {
return err
}
i := 0
for L.Next() {
v := L.Value()
in := v.LookupPath(cue.ParsePath("In"))

// If In exists
if in.Err() == nil {
// unify with G.In
G.Out[i].In = in.Unify(G.In)
} else {
// else, just use G.In
G.Out[i].In = G.In
}
i++
}

return nil
}

func (G *Generator) loadPackageName() error {
Expand Down
10 changes: 1 addition & 9 deletions lib/gen/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,10 @@ func (R *Runtime) LoadGenerators() []error {

func (R *Runtime) RunGenerators() []error {
var errs []error
// var err error

/*
R.Shadow, err = LoadShadow("", R.verbose)
if err != nil {
errs = append(errs, err)
return errs
}
*/

// Load shadow, can this be done in parallel with the last step?
// Don't do in parallel yet, Cue is slow and hungry for memory @ v0.0.16
// CUE v0.4.0- is not concurrency safe, maybe v0.4.1 will introduce?
for _, G := range R.Generators {
gerrs := R.RunGenerator(G)
if len(gerrs) > 0 {
Expand Down
3 changes: 3 additions & 0 deletions test/templates/gen.cue
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ TestGen: #TestGen & {
LHS: "{%"
RHS: "%}"
}
In: {
extra: "foobar"
}
},

// Named things
Expand Down
2 changes: 1 addition & 1 deletion test/templates/output/altdelim.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Val.a = '<no value>'
Val.a = 'a'
2 changes: 1 addition & 1 deletion test/templates/output/default.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Val.a = '<no value>'
Val.a = 'a'
2 changes: 1 addition & 1 deletion test/templates/output/named-things.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
embedded template is '<no value>'
embedded template is 'a'
4 changes: 2 additions & 2 deletions test/templates/output/template-altfile.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Hi, I'm a template file on disk '<no value>'
... and I'm a partial Hi, I'm a partial file on disk '<no value>'
Hi, I'm a template file on disk 'a'
... and I'm a partial Hi, I'm a partial file on disk 'b'


---
Expand Down
4 changes: 2 additions & 2 deletions test/templates/output/template-file.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Hi, I'm a template file on disk '<no value>'
... and I'm a partial Hi, I'm a partial file on disk '<no value>'
Hi, I'm a template file on disk 'a'
... and I'm a partial Hi, I'm a partial file on disk 'b'


---
Expand Down

0 comments on commit ee68b37

Please sign in to comment.