-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtype-message.go
117 lines (87 loc) · 1.94 KB
/
type-message.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
package main
import (
"io"
"strings"
"time"
)
type message struct {
FirstLine *line
HeaderLines []*line
BlankLine *line
BodyLines []*line
Duration time.Duration
}
func messageFromFile(context *context) (*message, error) {
firstLine, err := newLineFromFile(context)
if err != nil {
return nil, err
}
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
}
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
}
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() {
line.substitute(context)
}
}
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")
}