diff --git a/lib/gen/file.go b/lib/gen/file.go index f19b31197..d64e7f765 100644 --- a/lib/gen/file.go +++ b/lib/gen/file.go @@ -8,6 +8,7 @@ import ( "os" "strings" + "cuelang.org/go/cue" "github.com/epiclabs-io/diff3" "github.com/sergi/go-diff/diffmatchpatch" @@ -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 @@ -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 } diff --git a/lib/gen/generator.go b/lib/gen/generator.go index 0de6f6e35..f4e135f11 100644 --- a/lib/gen/generator.go +++ b/lib/gen/generator.go @@ -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 diff --git a/lib/gen/loadcue.go b/lib/gen/loadcue.go index aeace62f9..520482325 100644 --- a/lib/gen/loadcue.go +++ b/lib/gen/loadcue.go @@ -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 { @@ -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 { diff --git a/lib/gen/runtime.go b/lib/gen/runtime.go index 515ee6a86..6d20d331c 100644 --- a/lib/gen/runtime.go +++ b/lib/gen/runtime.go @@ -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 { diff --git a/test/templates/gen.cue b/test/templates/gen.cue index 9de3ca06c..741d26cb5 100644 --- a/test/templates/gen.cue +++ b/test/templates/gen.cue @@ -57,6 +57,9 @@ TestGen: #TestGen & { LHS: "{%" RHS: "%}" } + In: { + extra: "foobar" + } }, // Named things diff --git a/test/templates/output/altdelim.txt b/test/templates/output/altdelim.txt index 356d7e0ce..ce5c17661 100644 --- a/test/templates/output/altdelim.txt +++ b/test/templates/output/altdelim.txt @@ -1 +1 @@ -Val.a = '' +Val.a = 'a' diff --git a/test/templates/output/default.txt b/test/templates/output/default.txt index 356d7e0ce..ce5c17661 100644 --- a/test/templates/output/default.txt +++ b/test/templates/output/default.txt @@ -1 +1 @@ -Val.a = '' +Val.a = 'a' diff --git a/test/templates/output/named-things.txt b/test/templates/output/named-things.txt index 351ae70eb..695249fee 100644 --- a/test/templates/output/named-things.txt +++ b/test/templates/output/named-things.txt @@ -1 +1 @@ -embedded template is '' \ No newline at end of file +embedded template is 'a' \ No newline at end of file diff --git a/test/templates/output/template-altfile.txt b/test/templates/output/template-altfile.txt index b25d539d5..effe59ec9 100644 --- a/test/templates/output/template-altfile.txt +++ b/test/templates/output/template-altfile.txt @@ -1,5 +1,5 @@ -Hi, I'm a template file on disk '' - ... and I'm a partial Hi, I'm a partial file on disk '' +Hi, I'm a template file on disk 'a' + ... and I'm a partial Hi, I'm a partial file on disk 'b' --- diff --git a/test/templates/output/template-file.txt b/test/templates/output/template-file.txt index 7b0b93d52..0524a638b 100644 --- a/test/templates/output/template-file.txt +++ b/test/templates/output/template-file.txt @@ -1,5 +1,5 @@ -Hi, I'm a template file on disk '' - ... and I'm a partial Hi, I'm a partial file on disk '' +Hi, I'm a template file on disk 'a' + ... and I'm a partial Hi, I'm a partial file on disk 'b' ---