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:
windyboy
2024-07-24 16:03:45 +08:00
parent ce4de56681
commit ee646d5a23
2 changed files with 23 additions and 18 deletions
+10 -9
View File
@@ -36,11 +36,12 @@ var mu sync.Mutex
type BodyParser struct {
bodyMu sync.Mutex
body string
bodyPatterns map[string]config.BodyConfig
}
func NewBodyParser() *BodyParser {
return &BodyParser{bodyPatterns: config.GetBodyPatterns()}
func NewBodyParser(body string) *BodyParser {
return &BodyParser{bodyPatterns: config.GetBodyPatterns(), body: body}
}
func (bp *BodyParser) GetBodyPatterns() map[string]config.BodyConfig {
@@ -51,24 +52,24 @@ func (bp *BodyParser) SetBodyPatterns(patterns map[string]config.BodyConfig) {
bp.bodyPatterns = patterns
}
func (bp *BodyParser) Parse(body string) (string, interface{}, error) {
func (bp *BodyParser) Parse() (string, interface{}, error) {
bp.bodyMu.Lock()
defer bp.bodyMu.Unlock()
body = strings.TrimSpace(body)
category := findCategory(body)
bp.body = strings.TrimSpace(bp.body)
category := findCategory(bp.body)
if category == "" {
return "", nil, fmt.Errorf("no category found in body text")
}
if patternConfig, exists := bp.bodyPatterns[category]; exists && patternConfig.Patterns != nil {
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)
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 {
@@ -151,8 +152,8 @@ func Parse(rawText string) (*domain.ParsedMessage, error) {
return nil, err
}
bodyParser := NewBodyParser()
category, bodyData, err := bodyParser.Parse(message.BodyAndFooter)
bodyParser := NewBodyParser(message.BodyAndFooter)
category, bodyData, err := bodyParser.Parse()
message.Category = category
message.ParsedAt = time.Now()
+13 -9
View File
@@ -103,9 +103,9 @@ NNNN`
Context("with ARR body (ARR-CES5470-ZBTJ-ZSHC1614)", func() {
body := "(ARR-CES5470-ZBTJ-ZSHC1614)"
parser := NewBodyParser()
parser := NewBodyParser(body)
It("should parse the body correctly", func() {
category, parsedBody, err := parser.Parse(body)
category, parsedBody, err := parser.Parse()
Expect(err).ToNot(HaveOccurred())
Expect(parsedBody).ToNot(BeNil())
Expect(category).To(Equal("ARR"))
@@ -121,10 +121,11 @@ NNNN`
})
Context("with ARR body", func() {
parser := NewBodyParser()
// parser := NewBodyParser(body)
It("should parse the body (ARR-AB123/A1234-KJFK-KLAX1234) correctly", func() {
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(parsedBody).ToNot(BeNil())
Expect(category).To(Equal("ARR"))
@@ -139,7 +140,8 @@ NNNN`
It("should parse the body (ARR-JAE7433/A0132-RKSI-ZBTJ1604) correctly", func() {
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(parsedBody).ToNot(BeNil())
Expect(category).To(Equal("ARR"))
@@ -155,10 +157,11 @@ NNNN`
})
Context("with DEP body", func() {
parser := NewBodyParser()
// parser := NewBodyParser()
It("should parse the body (DEP-CYZ9017/A5633-ZBTJ1638-ZSPD) correctly", func() {
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(parsedBody).ToNot(BeNil())
Expect(category).To(Equal("DEP"))
@@ -174,7 +177,7 @@ NNNN`
})
Context("with FPL body", func() {
parser := NewBodyParser()
// parser := NewBodyParser()
It("should parse the body correctly", func() {
body := `(FPL-CCA1532-IS
-A332/H
@@ -183,7 +186,8 @@ NNNN`
-K0859S1040 PIAKS G330 PIMOL A539 BTO W82 DOGAR
-ZBAA0153 ZBYN
-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(parsedBody).ToNot(BeNil())
Expect(category).To(Equal("FPL"))