refactor: Update BodyParser constructor to accept body parameter
The code changes in `aviation_parser.go` modify the `NewBodyParser` constructor to accept a `body` parameter. This allows for the initialization of `BodyParser` instances with a specific body value, improving flexibility and testability. The `body` parameter is now stored in the `body` field of the `BodyParser` struct. This change enhances the overall functionality and maintainability of the code.
This commit is contained in:
@@ -36,11 +36,12 @@ var mu sync.Mutex
|
|||||||
|
|
||||||
type BodyParser struct {
|
type BodyParser struct {
|
||||||
bodyMu sync.Mutex
|
bodyMu sync.Mutex
|
||||||
|
body string
|
||||||
bodyPatterns map[string]config.BodyConfig
|
bodyPatterns map[string]config.BodyConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBodyParser() *BodyParser {
|
func NewBodyParser(body string) *BodyParser {
|
||||||
return &BodyParser{bodyPatterns: config.GetBodyPatterns()}
|
return &BodyParser{bodyPatterns: config.GetBodyPatterns(), body: body}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bp *BodyParser) GetBodyPatterns() map[string]config.BodyConfig {
|
func (bp *BodyParser) GetBodyPatterns() map[string]config.BodyConfig {
|
||||||
@@ -51,24 +52,24 @@ func (bp *BodyParser) SetBodyPatterns(patterns map[string]config.BodyConfig) {
|
|||||||
bp.bodyPatterns = patterns
|
bp.bodyPatterns = patterns
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bp *BodyParser) Parse(body string) (string, interface{}, error) {
|
func (bp *BodyParser) Parse() (string, interface{}, error) {
|
||||||
bp.bodyMu.Lock()
|
bp.bodyMu.Lock()
|
||||||
defer bp.bodyMu.Unlock()
|
defer bp.bodyMu.Unlock()
|
||||||
body = strings.TrimSpace(body)
|
bp.body = strings.TrimSpace(bp.body)
|
||||||
category := findCategory(body)
|
category := findCategory(bp.body)
|
||||||
if category == "" {
|
if category == "" {
|
||||||
return "", nil, fmt.Errorf("no category found in body text")
|
return "", nil, fmt.Errorf("no category found in body text")
|
||||||
}
|
}
|
||||||
|
|
||||||
if patternConfig, exists := bp.bodyPatterns[category]; exists && patternConfig.Patterns != nil {
|
if patternConfig, exists := bp.bodyPatterns[category]; exists && patternConfig.Patterns != nil {
|
||||||
for _, p := range patternConfig.Patterns {
|
for _, p := range patternConfig.Patterns {
|
||||||
if match := p.Expression.FindStringSubmatch(body); match != nil {
|
if match := p.Expression.FindStringSubmatch(bp.body); match != nil {
|
||||||
data := extractData(match, p.Expression)
|
data := extractData(match, p.Expression)
|
||||||
return bp.createBodyData(data)
|
return bp.createBodyData(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", nil, fmt.Errorf("no matching pattern found for body: %s", body)
|
return "", nil, fmt.Errorf("no matching pattern found for body: %s", bp.body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func findCategory(body string) string {
|
func findCategory(body string) string {
|
||||||
@@ -151,8 +152,8 @@ func Parse(rawText string) (*domain.ParsedMessage, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
bodyParser := NewBodyParser()
|
bodyParser := NewBodyParser(message.BodyAndFooter)
|
||||||
category, bodyData, err := bodyParser.Parse(message.BodyAndFooter)
|
category, bodyData, err := bodyParser.Parse()
|
||||||
message.Category = category
|
message.Category = category
|
||||||
message.ParsedAt = time.Now()
|
message.ParsedAt = time.Now()
|
||||||
|
|
||||||
|
|||||||
@@ -103,9 +103,9 @@ NNNN`
|
|||||||
|
|
||||||
Context("with ARR body (ARR-CES5470-ZBTJ-ZSHC1614)", func() {
|
Context("with ARR body (ARR-CES5470-ZBTJ-ZSHC1614)", func() {
|
||||||
body := "(ARR-CES5470-ZBTJ-ZSHC1614)"
|
body := "(ARR-CES5470-ZBTJ-ZSHC1614)"
|
||||||
parser := NewBodyParser()
|
parser := NewBodyParser(body)
|
||||||
It("should parse the body correctly", func() {
|
It("should parse the body correctly", func() {
|
||||||
category, parsedBody, err := parser.Parse(body)
|
category, parsedBody, err := parser.Parse()
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(parsedBody).ToNot(BeNil())
|
Expect(parsedBody).ToNot(BeNil())
|
||||||
Expect(category).To(Equal("ARR"))
|
Expect(category).To(Equal("ARR"))
|
||||||
@@ -121,10 +121,11 @@ NNNN`
|
|||||||
})
|
})
|
||||||
|
|
||||||
Context("with ARR body", func() {
|
Context("with ARR body", func() {
|
||||||
parser := NewBodyParser()
|
// parser := NewBodyParser(body)
|
||||||
It("should parse the body (ARR-AB123/A1234-KJFK-KLAX1234) correctly", func() {
|
It("should parse the body (ARR-AB123/A1234-KJFK-KLAX1234) correctly", func() {
|
||||||
body := " (ARR-AB123/A1234-KJFK-KLAX1234)"
|
body := " (ARR-AB123/A1234-KJFK-KLAX1234)"
|
||||||
category, parsedBody, err := parser.Parse(body)
|
parser := NewBodyParser(body)
|
||||||
|
category, parsedBody, err := parser.Parse()
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(parsedBody).ToNot(BeNil())
|
Expect(parsedBody).ToNot(BeNil())
|
||||||
Expect(category).To(Equal("ARR"))
|
Expect(category).To(Equal("ARR"))
|
||||||
@@ -139,7 +140,8 @@ NNNN`
|
|||||||
|
|
||||||
It("should parse the body (ARR-JAE7433/A0132-RKSI-ZBTJ1604) correctly", func() {
|
It("should parse the body (ARR-JAE7433/A0132-RKSI-ZBTJ1604) correctly", func() {
|
||||||
body := " (ARR-JAE7433/A0132-RKSI-ZBTJ1604)"
|
body := " (ARR-JAE7433/A0132-RKSI-ZBTJ1604)"
|
||||||
category, parsedBody, err := parser.Parse(body)
|
parser := NewBodyParser(body)
|
||||||
|
category, parsedBody, err := parser.Parse()
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(parsedBody).ToNot(BeNil())
|
Expect(parsedBody).ToNot(BeNil())
|
||||||
Expect(category).To(Equal("ARR"))
|
Expect(category).To(Equal("ARR"))
|
||||||
@@ -155,10 +157,11 @@ NNNN`
|
|||||||
})
|
})
|
||||||
|
|
||||||
Context("with DEP body", func() {
|
Context("with DEP body", func() {
|
||||||
parser := NewBodyParser()
|
// parser := NewBodyParser()
|
||||||
It("should parse the body (DEP-CYZ9017/A5633-ZBTJ1638-ZSPD) correctly", func() {
|
It("should parse the body (DEP-CYZ9017/A5633-ZBTJ1638-ZSPD) correctly", func() {
|
||||||
body := "(DEP-CYZ9017/A5633-ZBTJ1638-ZSPD)"
|
body := "(DEP-CYZ9017/A5633-ZBTJ1638-ZSPD)"
|
||||||
category, parsedBody, err := parser.Parse(body)
|
parser := NewBodyParser(body)
|
||||||
|
category, parsedBody, err := parser.Parse()
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(parsedBody).ToNot(BeNil())
|
Expect(parsedBody).ToNot(BeNil())
|
||||||
Expect(category).To(Equal("DEP"))
|
Expect(category).To(Equal("DEP"))
|
||||||
@@ -174,7 +177,7 @@ NNNN`
|
|||||||
})
|
})
|
||||||
|
|
||||||
Context("with FPL body", func() {
|
Context("with FPL body", func() {
|
||||||
parser := NewBodyParser()
|
// parser := NewBodyParser()
|
||||||
It("should parse the body correctly", func() {
|
It("should parse the body correctly", func() {
|
||||||
body := `(FPL-CCA1532-IS
|
body := `(FPL-CCA1532-IS
|
||||||
-A332/H
|
-A332/H
|
||||||
@@ -183,7 +186,8 @@ NNNN`
|
|||||||
-K0859S1040 PIAKS G330 PIMOL A539 BTO W82 DOGAR
|
-K0859S1040 PIAKS G330 PIMOL A539 BTO W82 DOGAR
|
||||||
-ZBAA0153 ZBYN
|
-ZBAA0153 ZBYN
|
||||||
-PBN/A1B2B3B4B5D1L1 NAV/ABAS REG/B6513 EET/ZBPE0112 SEL/KMAL PER/C RIF/FRT N640 ZBYN RMK/TCAS EQUIPPED)`
|
-PBN/A1B2B3B4B5D1L1 NAV/ABAS REG/B6513 EET/ZBPE0112 SEL/KMAL PER/C RIF/FRT N640 ZBYN RMK/TCAS EQUIPPED)`
|
||||||
category, parsedBody, err := parser.Parse(body)
|
parser := NewBodyParser(body)
|
||||||
|
category, parsedBody, err := parser.Parse()
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
Expect(parsedBody).ToNot(BeNil())
|
Expect(parsedBody).ToNot(BeNil())
|
||||||
Expect(category).To(Equal("FPL"))
|
Expect(category).To(Equal("FPL"))
|
||||||
|
|||||||
Reference in New Issue
Block a user