MCPcopy Create free account
hub / github.com/foxcpp/maddy / expandMacros

Method expandMacros

framework/cfgparser/imports.go:114–154  ·  view source on GitHub ↗
(node *Node)

Source from the content-addressed store, hash-verified

112}
113
114func (ctx *parseContext) expandMacros(node *Node) error {
115 if strings.HasPrefix(node.Name, "$(") && strings.HasSuffix(node.Name, ")") {
116 return ctx.Err("can't use macro argument as directive name")
117 }
118
119 newArgs := make([]string, 0, len(node.Args))
120 for _, arg := range node.Args {
121 if !strings.HasPrefix(arg, "$(") || !strings.HasSuffix(arg, ")") {
122 if strings.Contains(arg, "$(") && strings.Contains(arg, ")") {
123 var err error
124 arg, err = ctx.expandSingleValueMacro(arg)
125 if err != nil {
126 return err
127 }
128 }
129
130 newArgs = append(newArgs, arg)
131 continue
132 }
133
134 macroName := arg[2 : len(arg)-1]
135 replacement, ok := ctx.macros[macroName]
136 if !ok {
137 // Undefined macros are expanded to zero arguments.
138 continue
139 }
140
141 newArgs = append(newArgs, replacement...)
142 }
143 node.Args = newArgs
144
145 if node.Children != nil {
146 for i := range node.Children {
147 if err := ctx.expandMacros(&node.Children[i]); err != nil {
148 return err
149 }
150 }
151 }
152
153 return nil
154}
155
156var macroRe = regexp.MustCompile(`\$\(([^\$]+)\)`)
157

Callers 1

readNodesMethod · 0.95

Calls 2

ErrMethod · 0.80

Tested by

no test coverage detected