This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
messages.go
127 lines (116 loc) · 2.84 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
121
122
123
124
125
126
127
package gherkin
import (
"fmt"
"github.com/cucumber/messages-go/v15"
gio "github.com/gogo/protobuf/io"
"io"
"io/ioutil"
"math"
"strings"
)
func Messages(
paths []string,
sourceStream io.Reader,
language string,
includeSource bool,
includeGherkinDocument bool,
includePickles bool,
writer gio.WriteCloser,
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 writer != nil {
err = writer.WriteMsg(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{
Message: &messages.Envelope_Source{
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{
Message: &messages.Envelope_GherkinDocument{
GherkinDocument: doc,
},
})
}
if includePickles {
for _, pickle := range Pickles(*doc, source.Uri, newId) {
result, err = handleMessage(result, &messages.Envelope{
Message: &messages.Envelope_Pickle{
Pickle: pickle,
},
})
}
}
return nil
}
if len(paths) == 0 {
reader := gio.NewDelimitedReader(sourceStream, math.MaxInt32)
for {
envelope := &messages.Envelope{}
err := reader.ReadMsg(envelope)
if err == io.EOF {
break
}
switch t := envelope.Message.(type) {
case *messages.Envelope_Source:
processSource(t.Source)
}
}
} 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{
Message: &messages.Envelope_ParseError{
ParseError: &messages.ParseError{
Message: a.Error(),
Source: &messages.SourceReference{
Reference: &messages.SourceReference_Uri{
Uri: uri,
},
Location: &messages.Location{
Line: uint32(a.loc.Line),
Column: uint32(a.loc.Column),
},
},
}},
}
}