Skip to content

Commit

Permalink
Merge pull request #29 from blackmichael/blackmichael/fix_include_pat…
Browse files Browse the repository at this point in the history
…h_resolution

Fixed included resource path resolution
  • Loading branch information
gurkankaymak authored Dec 5, 2022
2 parents e0e27a6 + d46daae commit b15a24d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
25 changes: 21 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
"text/scanner"
Expand Down Expand Up @@ -35,9 +36,23 @@ type parser struct {
scanner *scanner.Scanner
currentRune rune
lastConsumedWhitespaces string // used in concatenation not to lose whitespaces between values
filepath string
}

func newParser(src io.Reader) *parser {
s := newScanner(src)
currWd := "."

return &parser{scanner: s, filepath: currWd}
}

func newFileParser(src *os.File) *parser {
s := newScanner(src)

return &parser{scanner: s, filepath: src.Name()}
}

func newScanner(src io.Reader) *scanner.Scanner {
s := new(scanner.Scanner)
s.Init(src)
s.Whitespace ^= 1<<'\t' | 1<<' ' // do not skip tabs and spaces
Expand All @@ -46,7 +61,7 @@ func newParser(src io.Reader) *parser {
return ch == '_' || ch == '-' || unicode.IsLetter(ch) || unicode.IsDigit(ch) && i > 0
}

return &parser{scanner: s}
return s
}

// ParseString function parses the given hocon string, creates the configuration tree and
Expand All @@ -64,7 +79,7 @@ func ParseResource(path string) (*Config, error) {
return nil, fmt.Errorf("could not parse resource: %w", err)
}

return newParser(file).parse()
return newFileParser(file).parse()
}

func (p *parser) parse() (*Config, error) {
Expand Down Expand Up @@ -469,7 +484,9 @@ func (p *parser) parseIncludedResource() (includeObject Object, err error) {
return nil, err
}

file, err := os.Open(includeToken.path)
parsedFileParentDir := path.Dir(p.filepath)
includePath := path.Join(parsedFileParentDir, includeToken.path)
file, err := os.Open(includePath)
if err != nil {
if errors.Is(err, os.ErrNotExist) && !includeToken.required {
return Object{}, nil
Expand All @@ -478,7 +495,7 @@ func (p *parser) parseIncludedResource() (includeObject Object, err error) {
return nil, fmt.Errorf("could not parse resource: %w", err)
}

includeParser := newParser(file)
includeParser := newFileParser(file)

defer func() {
if closingErr := file.Close(); closingErr != nil {
Expand Down
6 changes: 3 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,11 +997,11 @@ func TestParseIncludedResource(t *testing.T) {
})

t.Run("parse the included resource and return the parsed object if there is no error", func(t *testing.T) {
parser := newParser(strings.NewReader(`include "testdata/a.conf"`))
advanceScanner(t, parser, `"testdata/a.conf"`)
parser := newParser(strings.NewReader(`include "testdata/x.conf"`))
advanceScanner(t, parser, `"testdata/x.conf"`)
got, err := parser.parseIncludedResource()
assertNoError(t, err)
assertDeepEqual(t, got, Object{"a": Int(1)})
assertDeepEqual(t, got, Object{"a": Int(1), "x": Int(7), "y": String("foo")})
})
}

Expand Down
2 changes: 2 additions & 0 deletions testdata/nested/y.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include required("../a.conf")
y:"foo"
2 changes: 2 additions & 0 deletions testdata/x.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include required("nested/y.conf")
x:7

0 comments on commit b15a24d

Please sign in to comment.