Skip to content

Commit

Permalink
Setup lazy expanderfunc to avoid projectfile set callback
Browse files Browse the repository at this point in the history
  • Loading branch information
daved committed Jun 6, 2023
1 parent 09269dd commit c13b182
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 59 deletions.
16 changes: 16 additions & 0 deletions pkg/project/expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ func makeEntryMapMap(structure reflect.Value) map[string]map[string]entry {
return m
}

func makeLazyExpanderFuncFromPtrToStruct(val reflect.Value) ExpanderFunc {
return func(v, name, meta string, isFunc bool, ctx *Expansion) (string, error) {
iface := val.Interface()
if u, ok := iface.(interface{ Update(*Project) }); ok {
u.Update(ctx.Project)
}

if val.Kind() == reflect.Ptr {
val = val.Elem()
}
fn := makeExpanderFuncFromMap(makeEntryMapMap(val))

return fn(v, name, meta, isFunc, ctx)
}
}

func makeExpanderFuncFromMap(m map[string]map[string]entry) ExpanderFunc {
return func(v, name, meta string, isFunc bool, ctx *Expansion) (string, error) {
if isFunc && meta == "()" {
Expand Down
7 changes: 0 additions & 7 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ func (p *Project) SetCommit(commitID string) error {
return p.Source().SetCommit(commitID, p.IsHeadless())
}

func (p *Project) SetUpdateCallback(fn func()) {
if p.projectfile == nil {
return
}
p.projectfile.SetUpdateCallback(fn)
}

// Constants returns a reference to projectfile.Constants
func (p *Project) Constants() []*Constant {
constrained, err := constraints.FilterUnconstrained(pConditional, p.projectfile.Constants.AsConstrainedEntities())
Expand Down
56 changes: 4 additions & 52 deletions pkg/project/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,7 @@ func init() {
}
}

func RegisterTopLevelStruct(name string, val interface{}) error {
v := reflect.ValueOf(val)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}

m := makeEntryMapMap(v)
name = strings.ToLower(name)
err := RegisterExpander(name, makeExpanderFuncFromMap(m))
if err != nil {
return locale.WrapError(
err, "project_expand_register_expander_map",
"Cannot register expander (map)",
)
}

return nil
}

func RegisterTopLevelFunc(name string, val interface{}) error {
v := reflect.ValueOf(val)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}

name = strings.ToLower(name)
err := RegisterExpander(name, makeExpanderFuncFromFunc(v))
if err != nil {
return locale.WrapError(
err, "project_expand_register_expander_func",
"Cannot register expander (func)",
)
}

return nil
}

func RegisterTopLevelStringer(name string, val interface{}) error {
v := reflect.ValueOf(val)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}

topLevelLookup[strings.ToLower(name)] = fmt.Sprintf("%v", v.Interface())

return nil
}

/*func RegisterStruct(val interface{}) error {
func RegisterStruct(val interface{}) error {
v := reflect.ValueOf(val)
// deref if needed
if v.Kind() == reflect.Ptr {
Expand All @@ -95,6 +47,7 @@ func RegisterTopLevelStringer(name string, val interface{}) error {
}

d1Val := v.FieldByIndex(f.Index)
d1ValOrig := d1Val
if d1Val.Kind() == reflect.Ptr {
d1Val = d1Val.Elem()
}
Expand All @@ -105,9 +58,8 @@ func RegisterTopLevelStringer(name string, val interface{}) error {
switch d1Val.Type().Kind() {
// Convert type (to map-map) to express advanced control like tag handling.
case reflect.Struct:
m := makeEntryMapMap(d1Val)
name := strings.ToLower(f.Name)
err := RegisterExpander(name, makeExpanderFuncFromMap(m))
err := RegisterExpander(name, makeLazyExpanderFuncFromPtrToStruct(d1ValOrig))
if err != nil {
return locale.WrapError(
err, "project_expand_register_expander_map",
Expand All @@ -133,7 +85,7 @@ func RegisterTopLevelStringer(name string, val interface{}) error {
}

return nil
}*/
}

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

0 comments on commit c13b182

Please sign in to comment.