Skip to content

Commit 0c8f91c

Browse files
committed
Setup lazy expanderfunc to avoid projectfile set callback
1 parent 09269dd commit 0c8f91c

File tree

6 files changed

+43
-122
lines changed

6 files changed

+43
-122
lines changed

pkg/project/expander.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@ func makeEntryMapMap(structure reflect.Value) map[string]map[string]entry {
291291
return m
292292
}
293293

294+
func makeLazyExpanderFuncFromPtrToStruct(val reflect.Value) ExpanderFunc {
295+
return func(v, name, meta string, isFunc bool, ctx *Expansion) (string, error) {
296+
iface := val.Interface()
297+
if u, ok := iface.(interface{ Update(*Project) }); ok {
298+
u.Update(ctx.Project)
299+
}
300+
301+
if val.Kind() == reflect.Ptr {
302+
val = val.Elem()
303+
}
304+
fn := makeExpanderFuncFromMap(makeEntryMapMap(val))
305+
306+
return fn(v, name, meta, isFunc, ctx)
307+
}
308+
}
309+
294310
func makeExpanderFuncFromMap(m map[string]map[string]entry) ExpanderFunc {
295311
return func(v, name, meta string, isFunc bool, ctx *Expansion) (string, error) {
296312
if isFunc && meta == "()" {

pkg/project/project.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ func (p *Project) SetCommit(commitID string) error {
5858
return p.Source().SetCommit(commitID, p.IsHeadless())
5959
}
6060

61-
func (p *Project) SetUpdateCallback(fn func()) {
62-
if p.projectfile == nil {
63-
return
64-
}
65-
p.projectfile.SetUpdateCallback(fn)
66-
}
67-
6861
// Constants returns a reference to projectfile.Constants
6962
func (p *Project) Constants() []*Constant {
7063
constrained, err := constraints.FilterUnconstrained(pConditional, p.projectfile.Constants.AsConstrainedEntities())

pkg/project/registry.go

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,55 +31,7 @@ func init() {
3131
}
3232
}
3333

34-
func RegisterTopLevelStruct(name string, val interface{}) error {
35-
v := reflect.ValueOf(val)
36-
if v.Kind() == reflect.Ptr {
37-
v = v.Elem()
38-
}
39-
40-
m := makeEntryMapMap(v)
41-
name = strings.ToLower(name)
42-
err := RegisterExpander(name, makeExpanderFuncFromMap(m))
43-
if err != nil {
44-
return locale.WrapError(
45-
err, "project_expand_register_expander_map",
46-
"Cannot register expander (map)",
47-
)
48-
}
49-
50-
return nil
51-
}
52-
53-
func RegisterTopLevelFunc(name string, val interface{}) error {
54-
v := reflect.ValueOf(val)
55-
if v.Kind() == reflect.Ptr {
56-
v = v.Elem()
57-
}
58-
59-
name = strings.ToLower(name)
60-
err := RegisterExpander(name, makeExpanderFuncFromFunc(v))
61-
if err != nil {
62-
return locale.WrapError(
63-
err, "project_expand_register_expander_func",
64-
"Cannot register expander (func)",
65-
)
66-
}
67-
68-
return nil
69-
}
70-
71-
func RegisterTopLevelStringer(name string, val interface{}) error {
72-
v := reflect.ValueOf(val)
73-
if v.Kind() == reflect.Ptr {
74-
v = v.Elem()
75-
}
76-
77-
topLevelLookup[strings.ToLower(name)] = fmt.Sprintf("%v", v.Interface())
78-
79-
return nil
80-
}
81-
82-
/*func RegisterStruct(val interface{}) error {
34+
func RegisterStruct(val interface{}) error {
8335
v := reflect.ValueOf(val)
8436
// deref if needed
8537
if v.Kind() == reflect.Ptr {
@@ -95,6 +47,7 @@ func RegisterTopLevelStringer(name string, val interface{}) error {
9547
}
9648

9749
d1Val := v.FieldByIndex(f.Index)
50+
d1ValOrig := d1Val
9851
if d1Val.Kind() == reflect.Ptr {
9952
d1Val = d1Val.Elem()
10053
}
@@ -105,9 +58,8 @@ func RegisterTopLevelStringer(name string, val interface{}) error {
10558
switch d1Val.Type().Kind() {
10659
// Convert type (to map-map) to express advanced control like tag handling.
10760
case reflect.Struct:
108-
m := makeEntryMapMap(d1Val)
10961
name := strings.ToLower(f.Name)
110-
err := RegisterExpander(name, makeExpanderFuncFromMap(m))
62+
err := RegisterExpander(name, makeLazyExpanderFuncFromPtrToStruct(d1ValOrig))
11163
if err != nil {
11264
return locale.WrapError(
11365
err, "project_expand_register_expander_map",
@@ -133,7 +85,7 @@ func RegisterTopLevelStringer(name string, val interface{}) error {
13385
}
13486

13587
return nil
136-
}*/
88+
}
13789

13890
// RegisterExpander registers an Expander Func for some given handler value. The handler value
13991
// must not effectively be a blank string and the Func must be defined. It is definitely possible

pkg/projectfile/projectfile.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ type Project struct {
104104
parsedURL projectURL // parsed url data
105105
parsedBranch string
106106
parsedVersion string
107-
108-
updateCallback func()
109107
}
110108

111109
// Build covers the build map, which can go under languages or packages
@@ -684,21 +682,8 @@ func (p *Project) save(cfg ConfigGetter, path string) error {
684682
return nil
685683
}
686684

687-
func (p *Project) runUpdateCallback() {
688-
if p.updateCallback == nil {
689-
return
690-
}
691-
p.updateCallback()
692-
}
693-
694-
func (p *Project) SetUpdateCallback(fn func()) {
695-
p.updateCallback = fn
696-
}
697-
698685
// SetNamespace updates the namespace in the project file
699686
func (p *Project) SetNamespace(owner, project string) error {
700-
defer p.runUpdateCallback()
701-
702687
pf := NewProjectField()
703688
if err := pf.LoadProject(p.Project); err != nil {
704689
return errs.Wrap(err, "Could not load activestate.yaml")
@@ -720,8 +705,6 @@ func (p *Project) SetNamespace(owner, project string) error {
720705
// in-place so that line order is preserved.
721706
// If headless is true, the project is defined by a commit-id only
722707
func (p *Project) SetCommit(commitID string, headless bool) error {
723-
defer p.runUpdateCallback()
724-
725708
pf := NewProjectField()
726709
if err := pf.LoadProject(p.Project); err != nil {
727710
return errs.Wrap(err, "Could not load activestate.yaml")
@@ -739,8 +722,6 @@ func (p *Project) SetCommit(commitID string, headless bool) error {
739722
// SetBranch sets the branch within the current project file. This is done
740723
// in-place so that line order is preserved.
741724
func (p *Project) SetBranch(branch string) error {
742-
defer p.runUpdateCallback()
743-
744725
pf := NewProjectField()
745726

746727
if err := pf.LoadProject(p.Project); err != nil {
@@ -762,8 +743,6 @@ func (p *Project) SetBranch(branch string) error {
762743

763744
// SetPath sets the path of the project file and should generally only be used by tests
764745
func (p *Project) SetPath(path string) {
765-
defer p.runUpdateCallback()
766-
767746
p.path = path
768747
}
769748

pkg/projectfile/vars/vars.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,11 @@ import (
2121
"path/filepath"
2222

2323
"github.com/ActiveState/cli/internal/multilog"
24-
"github.com/ActiveState/cli/internal/rtutils/p"
2524
"github.com/ActiveState/cli/pkg/platform/authentication"
25+
"github.com/ActiveState/cli/pkg/project"
2626
"github.com/ActiveState/cli/pkg/sysinfo"
2727
)
2828

29-
type projectDataProvider interface {
30-
Owner() string
31-
Name() string
32-
NamespaceString() string
33-
CommitID() string
34-
BranchName() string
35-
Path() string
36-
URL() string
37-
}
38-
3929
type Project struct {
4030
Namespace string `expand:",asFunc"`
4131
Name string `expand:",asFunc"`
@@ -49,26 +39,24 @@ type Project struct {
4939
NamespacePrefix string
5040
}
5141

52-
func NewProject(pj projectDataProvider) *Project {
53-
var (
54-
project = &Project{}
55-
)
56-
if !p.IsNil(pj) {
57-
project.Namespace = pj.NamespaceString()
58-
project.Name = pj.Name()
59-
project.Owner = pj.Owner()
60-
project.Url = pj.URL()
61-
project.Commit = pj.CommitID()
62-
project.Branch = pj.BranchName()
63-
project.Path = pj.Path()
64-
if project.Path != "" {
65-
project.Path = filepath.Dir(project.Path)
66-
}
42+
func NewProject(pj *project.Project) *Project {
43+
p := &Project{}
44+
p.Update(pj)
45+
return p
46+
}
6747

68-
project.NamespacePrefix = pj.NamespaceString()
48+
func (p *Project) Update(pj *project.Project) {
49+
p.Namespace = pj.NamespaceString()
50+
p.Name = pj.Name()
51+
p.Owner = pj.Owner()
52+
p.Url = pj.URL()
53+
p.Commit = pj.CommitID()
54+
p.Branch = pj.BranchName()
55+
p.Path = pj.Path()
56+
if p.Path != "" {
57+
p.Path = filepath.Dir(p.Path)
6958
}
70-
71-
return project
59+
p.NamespacePrefix = pj.NamespaceString()
7260
}
7361

7462
type OSVersion struct {
@@ -128,14 +116,14 @@ type Vars struct {
128116
Mixin func() *Mixin
129117
}
130118

131-
func New(auth *authentication.Auth, project *Project, subshellName string) *Vars {
119+
func New(auth *authentication.Auth, pj *project.Project, subshellName string) *Vars {
132120
osVersion, err := sysinfo.OSVersion()
133121
if err != nil {
134122
multilog.Error("Could not detect OSVersion: %v", err)
135123
}
136124

137125
return &Vars{
138-
Project: project,
126+
Project: NewProject(pj),
139127
OS: NewOS(osVersion),
140128
Shell: subshellName,
141129
Mixin: func() *Mixin { return NewMixin(auth) },

pkg/projget/projget.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,10 @@ func newProject(out output.Outputer, auth *authentication.Auth, shell string, pj
4040
return nil, err
4141
}
4242

43-
registerProjectVars := func() {
44-
projVars := vars.New(auth, vars.NewProject(pj), shell)
45-
conditional := constraints.NewPrimeConditional(projVars)
46-
project.RegisterConditional(conditional)
47-
_ = project.RegisterTopLevelStruct("project", projVars.Project)
48-
_ = project.RegisterTopLevelFunc("mixin", projVars.Mixin)
49-
_ = project.RegisterTopLevelStringer("shell", projVars.Shell)
50-
}
51-
52-
pj.SetUpdateCallback(registerProjectVars)
53-
registerProjectVars()
43+
projVars := vars.New(auth, pj, shell)
44+
conditional := constraints.NewPrimeConditional(projVars)
45+
project.RegisterConditional(conditional)
46+
_ = project.RegisterStruct(projVars)
5447

5548
return pj, nil
5649
}

0 commit comments

Comments
 (0)