Skip to content

Commit 8e1368b

Browse files
authored
Parsing large files/xml (#149)
* bug: parsing large files/xml * tweak: more test cases
1 parent b8e001c commit 8e1368b

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

formatter/file.go

-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package formatter
22

33
import (
4-
"bufio"
5-
"errors"
64
"io"
75
"os"
86
)
@@ -20,21 +18,6 @@ type InputFileConfig struct {
2018
Source io.ReadCloser
2119
}
2220

23-
// ReadContents reads content from stdin or provided file-path
24-
func (i *InputFileConfig) ReadContents() ([]byte, error) {
25-
var err error
26-
var content []byte
27-
if i.Source == nil {
28-
return nil, errors.New("no reading source is defined")
29-
}
30-
scanner := bufio.NewScanner(i.Source)
31-
for scanner.Scan() {
32-
content = append(content, scanner.Bytes()...)
33-
}
34-
err = scanner.Err()
35-
return content, err
36-
}
37-
3821
// ExistsOpen tries to open a file for reading, returning an error if it fails
3922
func (i *InputFileConfig) ExistsOpen() error {
4023
f, err := os.Open(i.Path)

formatter/workflow.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,15 @@ func (w *MainWorkflow) Execute() (err error) {
8686

8787
// parse reads & unmarshalles the input file into NMAPRun struct
8888
func (w *MainWorkflow) parse() (run NMAPRun, err error) {
89-
input, err := w.Config.InputFileConfig.ReadContents()
90-
if err != nil {
91-
return
89+
if w.Config.InputFileConfig.Source == nil {
90+
return run, fmt.Errorf("no input file is defined")
9291
}
93-
if err = xml.Unmarshal(input, &run); err != nil {
92+
d := xml.NewDecoder(w.Config.InputFileConfig.Source)
93+
_, err = d.Token()
94+
if err != nil {
9495
return
9596
}
96-
return run, nil
97+
98+
err = d.Decode(&run)
99+
return
97100
}

formatter/workflow_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ func TestMainWorkflow_parse(t *testing.T) {
5050
<nmaprun></nmaprun>`,
5151
fileName: "main_workflow_parse_3_test",
5252
},
53+
{
54+
name: "Bad XML file",
55+
w: &MainWorkflow{
56+
Config: &Config{},
57+
},
58+
wantNMAPRun: NMAPRun{},
59+
wantErr: true,
60+
fileContent: "<?x< version=",
61+
fileName: "main_workflow_parse_4_test_wrong_xml",
62+
},
5363
{
5464
name: "XML file with some matching output",
5565
w: &MainWorkflow{

0 commit comments

Comments
 (0)