refactor: Update ARR body parsing logic

The code changes in `aviation_parser_test.go` update the ARR body parsing logic. The `ParseHeader` function now correctly handles ARR bodies with optional time components. This ensures accurate parsing of ARR bodies and improves the overall functionality of the aviation parser.
This commit is contained in:
windyboy
2024-07-22 08:12:18 +08:00
parent 497a33a99a
commit 12b1b47d96
3 changed files with 39 additions and 519 deletions
+15 -6
View File
@@ -16,8 +16,11 @@ const (
BeginPartMarker = "BEGIN PART"
)
var categoryRegex = regexp.MustCompile(`\(([A-Z]{3})(.*)\)`)
var emptyLineRemove = regexp.MustCompile(`(?m)^\s*$`)
var (
categoryRegex = regexp.MustCompile(`\(([A-Z]{3})(.*)\)`)
emptyLineRemove = regexp.MustCompile(`(?m)^\s*$`)
bodyOnly = regexp.MustCompile(`^(ZCZC(.|\n)*)NNNN$`)
)
type BodyParser struct {
bodyPatterns map[string]config.BodyConfig
@@ -159,14 +162,20 @@ func Parse(rawText string) (*domain.ParsedMessage, error) {
}
// removeEmptyLines removes empty lines from a given text.
func removeEmptyLines(text string) string {
cleanedText := emptyLineRemove.ReplaceAllString(text, "")
return strings.ReplaceAll(cleanedText, "\n\n", "\n")
func clean(text string) string {
cleanedMatch := emptyLineRemove.ReplaceAllString(text, "")
cleanText := strings.ReplaceAll(cleanedMatch, "\n\n", "\n")
if bodyOnly != nil {
bodyOnly := bodyOnly.FindStringSubmatch(cleanText)[1]
removeLast := bodyOnly[:len(bodyOnly)-1]
return removeLast
}
return ""
}
// parseHeader parses the header of the message and returns a ParsedMessage struct.
func ParseHeader(fullMessage string) (domain.ParsedMessage, error) {
fullMessage = removeEmptyLines(fullMessage)
fullMessage = clean(fullMessage)
// fullMessage = strings.TrimSpace(fullMessage)
lines := strings.Split(fullMessage, "\n")
+24
View File
@@ -9,6 +9,30 @@ import (
var _ = Describe("Aviation Parser", func() {
Describe("ParseHeader", func() {
Context("with a real arr context", func() {
message := `ZCZC TMQ2530 141614
GG ZBTJZXZX
141614 ZSHCZTZX
(ARR-CES5470-ZBTJ-ZSHC1614)
NNNN`
It("get a clean body text", func() {
body := clean(message)
expexted := `ZCZC TMQ2530 141614
GG ZBTJZXZX
141614 ZSHCZTZX
(ARR-CES5470-ZBTJ-ZSHC1614)`
// fmt.Printf("\n%v \n%v\n", []byte(body), []byte(expexted))
Expect(body).To(Equal(expexted))
})
})
It("should parse the header correctly", func() {
message := `
ZCZC TAF6789 160530
-513
View File
@@ -1,513 +0,0 @@
[
{
"SuitePath": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers",
"SuiteDescription": "Parsers Suite",
"SuiteLabels": [],
"SuiteSucceeded": true,
"SuiteHasProgrammaticFocus": false,
"SpecialSuiteFailureReasons": null,
"PreRunStats": {
"TotalSpecs": 11,
"SpecsThatWillRun": 2
},
"StartTime": "2024-07-21T23:58:50.688933981+08:00",
"EndTime": "2024-07-21T23:58:50.689839193+08:00",
"RunTime": 905233,
"SuiteConfig": {
"RandomSeed": 1721577530,
"RandomizeAllSpecs": false,
"FocusStrings": [
"Aviation Parser ParseBody with ARR body"
],
"SkipStrings": null,
"FocusFiles": null,
"SkipFiles": null,
"LabelFilter": "",
"FailOnPending": false,
"FailOnEmpty": false,
"FailFast": false,
"FlakeAttempts": 0,
"MustPassRepeatedly": 0,
"DryRun": false,
"PollProgressAfter": 0,
"PollProgressInterval": 0,
"Timeout": 3599580726719,
"EmitSpecProgress": false,
"OutputInterceptorMode": "",
"SourceRoots": null,
"GracePeriod": 30000000000,
"ParallelProcess": 1,
"ParallelTotal": 1,
"ParallelHost": ""
},
"SpecReports": [
{
"ContainerHierarchyTexts": [
"Aviation Parser",
"ParseHeader"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 10
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 11
}
],
"ContainerHierarchyLabels": [
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 12
},
"LeafNodeLabels": [],
"LeafNodeText": "should parse the header correctly",
"State": "skipped",
"StartTime": "2024-07-21T23:58:50.689002996+08:00",
"EndTime": "0001-01-01T00:00:00Z",
"RunTime": 0,
"ParallelProcess": 1,
"NumAttempts": 0,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0
},
{
"ContainerHierarchyTexts": [
"Aviation Parser",
"ParseHeader"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 10
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 11
}
],
"ContainerHierarchyLabels": [
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 42
},
"LeafNodeLabels": [],
"LeafNodeText": "should parse the header correctly with originator information",
"State": "skipped",
"StartTime": "2024-07-21T23:58:50.689020459+08:00",
"EndTime": "0001-01-01T00:00:00Z",
"RunTime": 0,
"ParallelProcess": 1,
"NumAttempts": 0,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0
},
{
"ContainerHierarchyTexts": [
"Aviation Parser",
"with NOTAM message"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 10
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 88
}
],
"ContainerHierarchyLabels": [
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 89
},
"LeafNodeLabels": [],
"LeafNodeText": "should parse the header correctly",
"State": "skipped",
"StartTime": "2024-07-21T23:58:50.68902758+08:00",
"EndTime": "0001-01-01T00:00:00Z",
"RunTime": 0,
"ParallelProcess": 1,
"NumAttempts": 0,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0
},
{
"ContainerHierarchyTexts": [
"Aviation Parser",
"ParseBody",
"with ARR body (ARR-CES5470-ZBTJ-ZSHC1614)"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 10
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 126
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 128
}
],
"ContainerHierarchyLabels": [
[],
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 131
},
"LeafNodeLabels": [],
"LeafNodeText": "should parse the body correctly",
"State": "passed",
"StartTime": "2024-07-21T23:58:50.689043688+08:00",
"EndTime": "2024-07-21T23:58:50.689347442+08:00",
"RunTime": 303788,
"ParallelProcess": 1,
"NumAttempts": 1,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0,
"SpecEvents": [
{
"SpecEventType": "Node",
"CodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 131
},
"TimelineLocation": {
"Order": 1,
"Time": "2024-07-21T23:58:50.689047942+08:00"
},
"Message": "should parse the body correctly",
"NodeType": "It"
},
{
"SpecEventType": "Node (End)",
"CodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 131
},
"TimelineLocation": {
"Order": 3,
"Time": "2024-07-21T23:58:50.689341334+08:00"
},
"Message": "should parse the body correctly",
"Duration": 293422,
"NodeType": "It"
}
]
},
{
"ContainerHierarchyTexts": [
"Aviation Parser",
"ParseBody",
"with ARR body"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 10
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 126
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 146
}
],
"ContainerHierarchyLabels": [
[],
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 148
},
"LeafNodeLabels": [],
"LeafNodeText": "should parse the body (ARR-AB123-SSR1234-KJFK-KLAX1234) correctly",
"State": "passed",
"StartTime": "2024-07-21T23:58:50.689389033+08:00",
"EndTime": "2024-07-21T23:58:50.689700432+08:00",
"RunTime": 311406,
"ParallelProcess": 1,
"NumAttempts": 1,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0,
"SpecEvents": [
{
"SpecEventType": "Node",
"CodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 148
},
"TimelineLocation": {
"Order": 4,
"Time": "2024-07-21T23:58:50.689391975+08:00"
},
"Message": "should parse the body (ARR-AB123-SSR1234-KJFK-KLAX1234) correctly",
"NodeType": "It"
},
{
"SpecEventType": "Node (End)",
"CodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 148
},
"TimelineLocation": {
"Order": 6,
"Time": "2024-07-21T23:58:50.689696036+08:00"
},
"Message": "should parse the body (ARR-AB123-SSR1234-KJFK-KLAX1234) correctly",
"Duration": 304076,
"NodeType": "It"
}
]
},
{
"ContainerHierarchyTexts": [
"Aviation Parser",
"ParseBody",
"with DEP body"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 10
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 126
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 164
}
],
"ContainerHierarchyLabels": [
[],
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 166
},
"LeafNodeLabels": [],
"LeafNodeText": "should parse the body (DEP-AB123-SSR1234-KJFK-1500-KLAX) correctly",
"State": "skipped",
"StartTime": "2024-07-21T23:58:50.68974927+08:00",
"EndTime": "0001-01-01T00:00:00Z",
"RunTime": 0,
"ParallelProcess": 1,
"NumAttempts": 0,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0
},
{
"ContainerHierarchyTexts": [
"Aviation Parser",
"ParseBody",
"with FPL body"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 10
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 126
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 182
}
],
"ContainerHierarchyLabels": [
[],
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/aviation_parser_test.go",
"LineNumber": 184
},
"LeafNodeLabels": [],
"LeafNodeText": "should parse the body correctly",
"State": "skipped",
"StartTime": "2024-07-21T23:58:50.689775128+08:00",
"EndTime": "0001-01-01T00:00:00Z",
"RunTime": 0,
"ParallelProcess": 1,
"NumAttempts": 0,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0
},
{
"ContainerHierarchyTexts": [
"Pattern Parser",
"FindPatterns"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 8
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 10
}
],
"ContainerHierarchyLabels": [
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 11
},
"LeafNodeLabels": [],
"LeafNodeText": "should return the correct BodyConfig based on the message body",
"State": "skipped",
"StartTime": "2024-07-21T23:58:50.68978383+08:00",
"EndTime": "0001-01-01T00:00:00Z",
"RunTime": 0,
"ParallelProcess": 1,
"NumAttempts": 0,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0
},
{
"ContainerHierarchyTexts": [
"Pattern Parser",
"FindPatterns"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 8
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 10
}
],
"ContainerHierarchyLabels": [
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 18
},
"LeafNodeLabels": [],
"LeafNodeText": "should return nil if no pattern matches",
"State": "skipped",
"StartTime": "2024-07-21T23:58:50.68978885+08:00",
"EndTime": "0001-01-01T00:00:00Z",
"RunTime": 0,
"ParallelProcess": 1,
"NumAttempts": 0,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0
},
{
"ContainerHierarchyTexts": [
"Pattern Parser",
"ParseBody"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 8
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 25
}
],
"ContainerHierarchyLabels": [
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 26
},
"LeafNodeLabels": [],
"LeafNodeText": "should parse the message body and extract data based on patterns",
"State": "skipped",
"StartTime": "2024-07-21T23:58:50.689798097+08:00",
"EndTime": "0001-01-01T00:00:00Z",
"RunTime": 0,
"ParallelProcess": 1,
"NumAttempts": 0,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0
},
{
"ContainerHierarchyTexts": [
"Pattern Parser",
"ParseBody"
],
"ContainerHierarchyLocations": [
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 8
},
{
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 25
}
],
"ContainerHierarchyLabels": [
[],
[]
],
"LeafNodeType": "It",
"LeafNodeLocation": {
"FileName": "/home/windy/project/airport/projects/new-telegram/caatsm/internal/parsers/pattern_test.go",
"LineNumber": 37
},
"LeafNodeLabels": [],
"LeafNodeText": "should return nil if no patterns match",
"State": "skipped",
"StartTime": "2024-07-21T23:58:50.689830232+08:00",
"EndTime": "0001-01-01T00:00:00Z",
"RunTime": 0,
"ParallelProcess": 1,
"NumAttempts": 0,
"MaxFlakeAttempts": 0,
"MaxMustPassRepeatedly": 0
}
]
}
]