-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtype-message.go
More file actions
154 lines (118 loc) · 2.61 KB
/
type-message.go
File metadata and controls
154 lines (118 loc) · 2.61 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"fmt"
"io"
"strings"
"time"
)
type message struct {
FirstLine *line
HeaderLines []*line
BlankLine *line
BodyLines []*line
Duration time.Duration
}
func messagePrefixError(expected string, l *line) error {
return fmt.Errorf(
"expected %q prefix, got %q at line %d: %s",
expected,
l.IOPrefix,
l.LineNumber,
l.InputText,
)
}
func messageFromFile(
context *context,
expectedPrefix string,
) (*message, error) {
var firstLine *line
var err error
for {
firstLine, err = newLineFromFile(context)
if err != nil {
return nil, err
}
if !firstLine.isBlank() {
break
}
}
if firstLine.IOPrefix != expectedPrefix {
return nil, messagePrefixError(expectedPrefix, firstLine)
}
var headerLine *line
var headerLines []*line
var emptyLine *line
for {
headerLine, err = newLineFromFile(context)
if err != nil {
return nil, err
}
if headerLine.isEmpty() {
emptyLine = headerLine
break
}
if headerLine.IOPrefix != expectedPrefix {
return nil, messagePrefixError(expectedPrefix, headerLine)
}
headerLines = append(headerLines, headerLine)
}
var bodyLine *line
var bodyLines []*line
duration := time.Duration(0)
for {
bodyLine, err = newLineFromFile(context)
if err == io.EOF || bodyLine.isBlank() {
break
}
if err != nil {
return nil, err
}
if bodyLine.isSleep() {
duration, err = time.ParseDuration(bodyLine.Text)
if err != nil {
return nil, err
}
break
}
if bodyLine.IOPrefix != expectedPrefix {
return nil, messagePrefixError(expectedPrefix, bodyLine)
}
bodyLines = append(bodyLines, bodyLine)
}
return &message{
firstLine,
headerLines,
emptyLine,
bodyLines,
duration,
}, nil
}
func (message *message) allLines() []*line {
var allLines []*line
allLines = append(allLines, message.FirstLine)
allLines = append(allLines, message.HeaderLines...)
allLines = append(allLines, message.BlankLine)
allLines = append(allLines, message.BodyLines...)
return allLines
}
func (message *message) substitute(context *context) {
for _, line := range message.allLines() {
if errorHandler(context, line.substitute(context)) {
return
}
}
}
func (message *message) Header() string {
headerLineTexts := []string{}
for _, headerLine := range message.HeaderLines {
headerLineTexts = append(headerLineTexts, headerLine.Text)
}
return strings.Join(headerLineTexts, "\n")
}
func (message *message) Body() string {
bodyLineTexts := []string{}
for _, bodyLine := range message.BodyLines {
bodyLineTexts = append(bodyLineTexts, bodyLine.Text)
}
return strings.Join(bodyLineTexts, "\n")
}