Skip to content

Commit eca529e

Browse files
authored
Merge pull request #167 from compose-spec/remove-pre-parsing
Remove only full line comments to avoid variable interpolation on this ones
2 parents b2976a5 + 354db4d commit eca529e

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

loader/loader.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"path"
2424
"path/filepath"
2525
"reflect"
26+
"regexp"
2627
"sort"
2728
"strings"
2829
"time"
@@ -217,12 +218,8 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
217218

218219
func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
219220
if !opts.SkipInterpolation {
220-
withoutComments, err := removeYamlComments(b)
221-
if err != nil {
222-
return nil, err
223-
}
224-
225-
substituted, err := opts.Interpolate.Substitute(string(withoutComments), template.Mapping(opts.Interpolate.LookupValue))
221+
withoutFullLineComments := removeYamlComments(string(b))
222+
substituted, err := opts.Interpolate.Substitute(withoutFullLineComments, template.Mapping(opts.Interpolate.LookupValue))
226223
if err != nil {
227224
return nil, err
228225
}
@@ -232,18 +229,9 @@ func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
232229
return ParseYAML(b)
233230
}
234231

235-
// removeYamlComments drop all comments from the yaml file, so we don't try to apply string substitutions on irrelevant places
236-
func removeYamlComments(b []byte) ([]byte, error) {
237-
var cfg interface{}
238-
err := yaml.Unmarshal(b, &cfg)
239-
if err != nil {
240-
return nil, err
241-
}
242-
b, err = yaml.Marshal(cfg)
243-
if err != nil {
244-
return nil, err
245-
}
246-
return b, nil
232+
// removeYamlComments drop all full line comments from the yaml file, so we don't try to apply string substitutions on most of the irrelevant places
233+
func removeYamlComments(content string) string {
234+
return regexp.MustCompile(`(?m)^[ \t]*#.*\n*`).ReplaceAllString(content, "")
247235
}
248236

249237
func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} {

0 commit comments

Comments
 (0)