Skip to content

Commit

Permalink
package/di: (breaking) hoisted params now always come before non-hois…
Browse files Browse the repository at this point in the history
…ted params.
  • Loading branch information
matthewmueller committed Jul 2, 2022
1 parent 5e86083 commit 3e2a5a9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
12 changes: 11 additions & 1 deletion package/di/di_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,10 @@ func TestHoistFull(t *testing.T) {
value: "log",
Env: &web.Env{value: "env"},
},
Workflow: &web.Workflow{Log: &web.Log{
value: "log",
Env: &web.Env{value: "env"},
}},
}
`,
Files: map[string]string{
Expand All @@ -1195,9 +1199,10 @@ func TestHoistFull(t *testing.T) {
env := web.NewEnv()
log := web.NewLog(env)
pg := &web.Postgres{log}
wf := &web.Workflow{log}
// request and dependencies that don't rely on request
// get hoisted up.
actual := genweb.Load(log, pg, request)
actual := genweb.Load(log, pg, wf, request)
fmt.Fprintf(os.Stdout, "%s\n", valast.String(actual))
}
`,
Expand Down Expand Up @@ -1230,6 +1235,10 @@ func TestHoistFull(t *testing.T) {
type Request struct {}
type Workflow struct {
Log *Log
}
type Session struct {
*Request
DB *Postgres
Expand All @@ -1239,6 +1248,7 @@ func TestHoistFull(t *testing.T) {
type Web struct {
*Session
*Log
*Workflow
}
`,
},
Expand Down
15 changes: 14 additions & 1 deletion package/di/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package di

import (
"fmt"
"sort"
"strings"

"github.com/livebud/bud/internal/imports"
Expand Down Expand Up @@ -57,7 +58,7 @@ func (n *Node) Generate(imports *imports.Set, fnName, target string) *Provider {
Name: fnName,
Target: target,
Imports: g.Imports.List(),
Externals: g.Externals,
Externals: sortExternals(g.Externals),
Code: g.Code.String(),
Results: outputs,
externalMap: externalMap(g.Externals),
Expand Down Expand Up @@ -246,6 +247,18 @@ func toTypeName(dataType string) string {
return strings.TrimLeft(last, "[]*")
}

// Sort the variables by name so the order is always consistent.
func sortExternals(externals []*External) []*External {
sort.Slice(externals, func(i, j int) bool {
// Hoisted params always come before non-hoisted params
if externals[i].Hoisted != externals[j].Hoisted {
return externals[i].Hoisted && !externals[j].Hoisted
}
return externals[i].Variable.Name < externals[j].Variable.Name
})
return externals
}

// Turn the results into a map for faster provider lookup
func externalMap(exts []*External) map[string]string {
m := make(map[string]string, len(exts))
Expand Down
21 changes: 11 additions & 10 deletions package/di/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package di

import (
"fmt"
"sort"
"strings"

"github.com/livebud/bud/internal/imports"
Expand Down Expand Up @@ -62,21 +61,23 @@ func (p *Provider) Function() string {
return c.String()
}

// Sort the variables by name so the order is always consistent.
func sortByName(externals []*External) []*External {
sort.Slice(externals, func(i, j int) bool {
return externals[i].Variable.Name < externals[j].Variable.Name
})
return externals
}

func (p *Provider) Params() (params Params) {
for _, external := range sortByName(p.Externals) {
for _, external := range p.Externals {
params = append(params, external.Variable.Name+" "+external.FullType)
}
return params
}

// Hoisted returns a list of hoisted externals
func (p *Provider) Hoisted() (externals []*External) {
for _, external := range p.Externals {
if external.Hoisted {
externals = append(externals, external)
}
}
return externals
}

type Params []string

func (params Params) String() string {
Expand Down

0 comments on commit 3e2a5a9

Please sign in to comment.