-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: handle include directives in parser
no longer necessary to read the file twice
- Loading branch information
Showing
19 changed files
with
102 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package ledger | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
) | ||
|
||
func TestIncludeSimple(t *testing.T) { | ||
trans, err := ParseLedgerFile("testdata/ledgerRoot.dat") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
bals := GetBalances(trans, []string{"Assets"}) | ||
if bals[0].Balance.StringRound() != "50" { | ||
t.Fatal(errors.New("should be 50")) | ||
} | ||
} | ||
|
||
func TestIncludeGlob(t *testing.T) { | ||
trans, err := ParseLedgerFile("testdata/ledgerRootGlob.dat") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
bals := GetBalances(trans, []string{"Assets"}) | ||
if bals[0].Balance.StringRound() != "80" { | ||
t.Fatal(errors.New("should be 80")) | ||
} | ||
} | ||
|
||
func TestIncludeUnbalanced(t *testing.T) { | ||
_, err := ParseLedgerFile("testdata/ledgerRootUnbalanced.dat") | ||
if err.Error() != "testdata/ledger-2021-05.dat:12: Unable to parse transaction: Unable to balance transaction: no empty account to place extra balance" { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,24 @@ | ||
package ledger | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
const ( | ||
markerPrefix = ";__ledger_file" | ||
markerSep = "*-*" | ||
) | ||
|
||
var includedFiles = make(map[string]bool) | ||
|
||
// NewLedgerReader reads a file and includes any files with include directives | ||
// and returns the whole combined ledger as a buffer for parsing. | ||
func NewLedgerReader(filename string) (*bytes.Buffer, error) { | ||
// | ||
// Deprecated: use ParseLedgerFile | ||
func NewLedgerReader(filename string) (io.Reader, error) { | ||
var buf bytes.Buffer | ||
|
||
err := includeFile(filename, &buf) | ||
return &buf, err | ||
} | ||
|
||
// includeFile reads filename into buf, adding special marker comments | ||
// when there are step changes in file location due to 'include' directive. | ||
func includeFile(filename string, buf *bytes.Buffer) error { | ||
if filename == "" { | ||
return errors.New("must specify filename") | ||
} | ||
filename = filepath.Clean(filename) | ||
lineNum := 0 | ||
|
||
// check for include cyles | ||
if includedFiles[filename] { | ||
return fmt.Errorf("include cycle: '%s'", filename) | ||
} | ||
includedFiles[filename] = true | ||
|
||
defer delete(includedFiles, filename) | ||
|
||
f, err := os.Open(filename) | ||
if err != nil { | ||
return err | ||
ifile, ierr := os.Open(filename) | ||
if ierr != nil { | ||
return &buf, ierr | ||
} | ||
defer f.Close() | ||
s := bufio.NewScanner(f) | ||
|
||
// mark the start of this file | ||
fmt.Fprintln(buf, marker(filename, lineNum)) | ||
|
||
for s.Scan() { | ||
line := s.Text() | ||
|
||
if prefix, incname, found := strings.Cut(line, " "); found && prefix == "include" { | ||
// Resolve filepaths | ||
includedPath := filepath.Join(filename, "..", incname) | ||
includedPaths, err := filepath.Glob(includedPath) | ||
|
||
// Include all resolved filepaths | ||
for i := 0; i < len(includedPaths) && err == nil; i++ { | ||
if !includedFiles[includedPaths[i]] { | ||
err = includeFile(includedPaths[i], buf) | ||
} | ||
} | ||
if err != nil { | ||
return fmt.Errorf("%s:%d: %s", filename, lineNum, err.Error()) | ||
} | ||
lineNum++ | ||
|
||
// mark the resumption point for this file | ||
fmt.Fprintln(buf, marker(filename, lineNum)) | ||
} else { | ||
fmt.Fprintln(buf, s.Text()) | ||
lineNum++ | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func marker(filename string, lineNum int) string { | ||
return strings.Join([]string{markerPrefix, filename, strconv.Itoa(lineNum)}, markerSep) | ||
} | ||
io.Copy(&buf, ifile) | ||
ifile.Close() | ||
|
||
func parseMarker(s string) (string, int) { | ||
v := strings.Split(s, markerSep) | ||
lineNum, _ := strconv.Atoi(v[2]) | ||
return v[1], lineNum | ||
return &buf, nil | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.