Skip to content

Commit

Permalink
find fields and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
seanenck committed Oct 28, 2024
1 parent c78cd7a commit 5a7bbc7
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions internal/dotfiles/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ type (
Writer io.Writer
}
templating struct {
re *regexp.Regexp
fields []string
re *regexp.Regexp
parameters []string
}
)

Expand Down Expand Up @@ -202,6 +202,14 @@ func (v variables) list() ([]dotfile, error) {
return results, nil
}

func getParameters(count int, base []string, fxn func(int) string) []string {
var vals []string
for i := 0; i < count; i++ {
vals = append(vals, fmt.Sprintf("$.Dotfiles.%s", fxn(i)))
}
return append(base, vals...)
}

func (v variables) forEach(fxn processFunction) error {
list, err := v.list()
if err != nil {
Expand All @@ -212,10 +220,13 @@ func (v variables) forEach(fxn processFunction) error {
return err
}
t := templating{re: r}
fields := reflect.ValueOf(v.Dotfiles)
for i := 0; i < fields.NumField(); i++ {
t.fields = append(t.fields, fmt.Sprintf("$.Dotfiles.%s", fields.Type().Field(i).Name))
}
fields := reflect.TypeOf(v.Dotfiles)
t.parameters = getParameters(fields.NumField(), t.parameters, func(idx int) string {
return fields.Field(idx).Name
})
t.parameters = getParameters(fields.NumMethod(), t.parameters, func(idx int) string {
return fields.Method(idx).Name
})
var results []chan result
for _, item := range list {
r := make(chan result)
Expand Down Expand Up @@ -256,7 +267,7 @@ func processFile(item dotfile, to string, t templating, c chan result, fxn proce
s := string(b)
is := false
if s != "" {
for _, f := range t.fields {
for _, f := range t.parameters {
if strings.Contains(s, f) {
is = true
break
Expand Down

0 comments on commit 5a7bbc7

Please sign in to comment.