-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresponse.go
106 lines (94 loc) · 2.66 KB
/
response.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
package adstxt
import (
"encoding/json"
"fmt"
"strings"
"time"
)
// Records holds collection of Ads.txt records parsed from an Ads.txt file, in addition to
// errors found during Ads.txt file parsing
type Records struct {
DataRecords []*DataRecord `json:"dataRecords"`
Variables []*Variable `json:"variables"`
Warnings []*Warning `json:"warnings"`
Body []string `json:"body"` // Original Ads.txt file content
}
// Response to an Ads.txt request: collection of Data\Variable records parsed from Ads.txt file and
// file expiration date
type Response struct {
*Request
*Records
Expires time.Time `json:"expires"` // Ads.txt file expiration date
}
// parseRecords parse Ads.txt file content
func parseRecords(lines []string) *Records {
r := &Records{
DataRecords: []*DataRecord{},
Variables: []*Variable{},
Warnings: []*Warning{},
Body: lines,
}
// loop over Ads.txt file lines and parse each line into Ads.txt record
for index, l := range lines {
r.parseRecord(index+1, l)
}
return r
}
// parseRecord parse a single Ads.txt line into Data\Variable record
func (r *Records) parseRecord(index int, txt string) {
line := removeComment(txt)
// ignore comments and empty line
if len(line) == 0 || string(line) == commentDenote {
return
}
// parse line into Data\Variable record
if strings.Count(line, ",") >= 2 && strings.Count(line, "=") <= 5 {
dr, w := parseDataRecord(line)
if w != nil {
w.Index = index
w.Text = txt
r.Warnings = append(r.Warnings, w)
}
if dr != nil {
r.DataRecords = append(r.DataRecords, dr)
}
} else if strings.Index(line, "=") != -1 && strings.Count(line, "=") == 1 {
v, w := parseVariable(txt)
if w != nil {
w.Index = index
w.Text = txt
r.Warnings = append(r.Warnings, w)
} else {
r.Variables = append(r.Variables, v)
}
} else {
w := &Warning{Text: txt, Index: index, Level: HighSeverity, Message: "could not parse this line"}
r.Warnings = append(r.Warnings, w)
}
}
// custom "toString" method
func (r *Records) String() string {
str := []string{}
if len(r.Warnings) > 0 {
str = append(str, fmt.Sprintf("Warnings: [%d]", len(r.Warnings)))
for _, w := range r.Warnings {
j, _ := json.Marshal(w)
str = append(str, string(j))
}
}
if len(r.DataRecords) > 0 {
str = append(str, fmt.Sprintf("Data Records: [%d]", len(r.DataRecords)))
for _, r := range r.DataRecords {
j, _ := json.Marshal(r)
str = append(str, string(j))
}
}
if len(r.Variables) > 0 {
str = append(str, fmt.Sprintf("Variables: [%d]", len(r.Variables)))
for _, v := range r.Variables {
j, _ := json.Marshal(v)
str = append(str, string(j))
}
}
return strings.Join(str, "\n")
}