forked from cucumber/gherkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
120 lines (109 loc) · 2.58 KB
/
messages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package gherkin
import (
"encoding/json"
"fmt"
"github.com/cucumber/messages/go/v24"
"io"
"io/ioutil"
"strings"
)
func Messages(
paths []string,
decoder *json.Decoder,
language string,
includeSource bool,
includeGherkinDocument bool,
includePickles bool,
encoder *json.Encoder,
newId func() string,
) ([]messages.Envelope, error) {
var result []messages.Envelope
var err error
handleMessage := func(result []messages.Envelope, message *messages.Envelope) ([]messages.Envelope, error) {
if encoder != nil {
err = encoder.Encode(message)
return result, err
} else {
result = append(result, *message)
}
return result, err
}
processSource := func(source *messages.Source) error {
if includeSource {
result, err = handleMessage(result, &messages.Envelope{
Source: source,
})
}
doc, err := ParseGherkinDocumentForLanguage(strings.NewReader(source.Data), language, newId)
if errs, ok := err.(parseErrors); ok {
// expected parse errors
for _, err := range errs {
if pe, ok := err.(*parseError); ok {
result, err = handleMessage(result, pe.asMessage(source.Uri))
} else {
return fmt.Errorf("parse feature file: %s, unexpected error: %+v\n", source.Uri, err)
}
}
return nil
}
if includeGherkinDocument {
doc.Uri = source.Uri
result, err = handleMessage(result, &messages.Envelope{
GherkinDocument: doc,
})
}
if includePickles {
for _, pickle := range Pickles(*doc, source.Uri, newId) {
result, err = handleMessage(result, &messages.Envelope{
Pickle: pickle,
})
}
}
return nil
}
if len(paths) == 0 {
for {
envelope := &messages.Envelope{}
err := decoder.Decode(envelope)
//marshal, err := json.Marshal(envelope)
//fmt.Println(string(marshal))
if err == io.EOF {
break
}
if envelope.Source != nil {
err = processSource(envelope.Source)
if err != nil {
return result, err
}
}
}
} else {
for _, path := range paths {
in, err := ioutil.ReadFile(path)
if err != nil {
return result, fmt.Errorf("read feature file: %s - %+v", path, err)
}
source := &messages.Source{
Uri: path,
Data: string(in),
MediaType: "text/x.cucumber.gherkin+plain",
}
processSource(source)
}
}
return result, err
}
func (a *parseError) asMessage(uri string) *messages.Envelope {
return &messages.Envelope{
ParseError: &messages.ParseError{
Message: a.Error(),
Source: &messages.SourceReference{
Uri: uri,
Location: &messages.Location{
Line: int64(a.loc.Line),
Column: int64(a.loc.Column),
},
},
},
}
}