Skip to content

Commit

Permalink
fix: func map unordered inconsistency
Browse files Browse the repository at this point in the history
  • Loading branch information
kiliantyler authored and JanDeDobbeleer committed Apr 10, 2024
1 parent 1e4a117 commit 045e4a8
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/config/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type Aliae struct {
Scripts shell.Scripts `yaml:"script"`
}

type FuncMap []StringFunc
type StringFunc struct {
Name string
F func([]string) (string, error)
}

func customUnmarshaler(a *Aliae, b []byte) error {
data, err := includeUnmarshaler(b)
if err != nil {
Expand All @@ -37,23 +43,29 @@ func customUnmarshaler(a *Aliae, b []byte) error {
func includeUnmarshaler(b []byte) ([]byte, error) {
s := strings.Split(string(b), "\n")

includeFuncMap := map[string]func([]string) (string, error){
"!include_dir": getDirFiles,
"!include": getFile,
includeFuncMap := FuncMap{
{
Name: "!include_dir",
F: getDirFiles,
},
{
Name: "!include",
F: getFile,
},
}

for i, line := range s {
for key, f := range includeFuncMap {
if !strings.HasPrefix(line, key) {
for _, f := range includeFuncMap {
if !strings.HasPrefix(line, f.Name) {
continue
}

parts := strings.Fields(line)
if len(parts) < 2 {
return nil, fmt.Errorf("invalid %s directive: \n%s", key, line)
return nil, fmt.Errorf("invalid %s directive: \n%s", f.Name, line)
}

data, err := f(parts[1:])
data, err := f.F(parts[1:])
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 045e4a8

Please sign in to comment.