Skip to content

Commit d798ea2

Browse files
committed
refactor
1 parent e941c9f commit d798ea2

File tree

1 file changed

+18
-11
lines changed
  • internal/devconfig/configfile

1 file changed

+18
-11
lines changed

internal/devconfig/configfile/ast.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -426,24 +426,31 @@ func (c *configAST) beforeComment(path ...any) []byte {
426426
)
427427
}
428428

429-
func (c *configAST) setEnv(env map[string]string) {
429+
func (c *configAST) createMemberIfMissing(key string) *hujson.ObjectMember {
430+
i := c.memberIndex(c.root.Value.(*hujson.Object), key)
431+
if i == -1 {
432+
c.root.Value.(*hujson.Object).Members = append(c.root.Value.(*hujson.Object).Members, hujson.ObjectMember{
433+
Name: hujson.Value{Value: hujson.String(key)},
434+
})
435+
i = len(c.root.Value.(*hujson.Object).Members) - 1
436+
}
437+
return &c.root.Value.(*hujson.Object).Members[i]
438+
}
439+
440+
func mapToObjectMembers(env map[string]string) []hujson.ObjectMember {
430441
members := make([]hujson.ObjectMember, 0, len(env))
431442
for k, v := range env {
432443
members = append(members, hujson.ObjectMember{
433444
Name: hujson.Value{Value: hujson.String(k)},
434445
Value: hujson.Value{Value: hujson.String(v)},
435446
})
436447
}
437-
i := c.memberIndex(c.root.Value.(*hujson.Object), "env")
438-
if i == -1 {
439-
c.root.Value.(*hujson.Object).Members = append(c.root.Value.(*hujson.Object).Members, hujson.ObjectMember{
440-
Name: hujson.Value{Value: hujson.String("env")},
441-
Value: hujson.Value{Value: &hujson.Object{Members: members}},
442-
})
443-
} else {
444-
c.root.Value.(*hujson.Object).Members[i].Value.Value = &hujson.Object{
445-
Members: members,
446-
}
448+
return members
449+
}
450+
451+
func (c *configAST) setEnv(env map[string]string) {
452+
c.createMemberIfMissing("env").Value.Value = &hujson.Object{
453+
Members: mapToObjectMembers(env),
447454
}
448455
c.root.Format()
449456
}

0 commit comments

Comments
 (0)