feat: Add AFTN message parsing functionality

This commit adds functionality to parse AFTN messages in the `aftn_parser.go` file. The `Parse` method now correctly parses the message based on its type and extracts the relevant data. Additionally, the `FindPatterns` and `ParseBody` functions have been updated to remove the dependency on the `config` package and instead use the `config.GetBodyPatterns` function to retrieve the body patterns. This change improves the modularity and maintainability of the code.

Note: This commit message assumes that the `config` package has been refactored to use the `GetBodyPatterns` function.
This commit is contained in:
windyboy
2024-07-20 00:01:04 +08:00
parent dbbf6133be
commit 969bb57902
7 changed files with 150 additions and 227 deletions
+9 -10
View File
@@ -5,24 +5,23 @@ import (
"regexp"
)
var bodyTypePattern = regexp.MustCompile(`^\(([A-Z]{3})(.*\n?)+\)$`)
var (
bodyTypePattern = regexp.MustCompile(`^\(([A-Z]{3})(.*\n?)+\)$`)
)
// FindPatterns detects the message body type based on the configuration.
func FindPatterns(messageBody string, config *config.Config) *config.BodyConfig {
func FindPatterns(messageBody string) *config.BodyConfig {
if match := bodyTypePattern.FindStringSubmatch(messageBody); len(match) > 1 {
name := match[1]
for _, body := range config.Body {
if body.Name == name {
return &body
}
patters := config.GetBodyPatterns()
if body, found := patters[name]; found {
return &body
}
}
return nil
}
// ParseBody parses the message body and extracts the data based on the patterns defined in the configuration.
func ParseBody(messageBody string, config *config.Config) map[string]string {
if body := FindPatterns(messageBody, config); body != nil {
func ParseBody(messageBody string) map[string]string {
if body := FindPatterns(messageBody); body != nil {
for _, pattern := range body.Patterns {
if matches := pattern.Expression.FindStringSubmatch(messageBody); matches != nil {
result := make(map[string]string)