add basic parse method

This commit is contained in:
zhiqiang feng
2021-01-04 15:33:14 +08:00
parent 220b1d269d
commit 38e9dc5f03
2 changed files with 100 additions and 29 deletions
+45 -24
View File
@@ -4,26 +4,31 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
) )
type Telegram struct { type Telegram struct {
Text string Text string
Complete bool Complete bool
Parsed bool Parsed bool
Fields []string Fields []string
Sequence int Sequence int
Type string Type string
FlopValue []string
FlightNumber string
RegisterNumber string
Departure string
DepartureTime time.Time
Destination string
DestinationTime time.Time
DateOfFlight time.Time
ParseErrors []error
} }
const ( const (
Start = "ZCZC" Start = "ZCZC"
End = "NNNN" End = "NNNN"
TmqRegex = `TMQ(?P<tmq>\d{4})` TmqRegex = `TMQ(?P<tmq>\d{4})`
FlopRegex = `^\((?P<flop>\w{3})-.*`
TypePLN = "PLN"
) )
//Parse //Parse
@@ -33,11 +38,17 @@ func Parse(text string) (*Telegram, error) {
telegram.Fields = strings.Split(strings.Replace(text, "\r\n", "\n", -1), "\n") telegram.Fields = strings.Split(strings.Replace(text, "\r\n", "\n", -1), "\n")
telegram.Fields = clean(telegram.Fields) telegram.Fields = clean(telegram.Fields)
seq := getFields(TmqRegex, telegram.Fields[0]) seq := getFields(TmqRegex, telegram.Fields[0])
telegram.Sequence, _ = strconv.Atoi( seq["tmq"]) telegram.Sequence, _ = strconv.Atoi(seq["tmq"])
if isPln(*telegram) { if isPln(*telegram) {
telegram.Type = TypePLN telegram.Type = TypePLN
} else if t := getFields(FlopRegex, telegram.Fields[3]); t != nil { } else if flopValues := getFields(FlopRegex, telegram.Text); len(flopValues) > 0 {
telegram.Type = t["flop"] telegram.Type = flopValues["flop"]
r := regexp.MustCompile(FlopRegex)
data := r.FindString(telegram.Text)
switch telegram.Type {
case TypeArrival:
parseArr(telegram, data)
}
} }
return telegram, nil return telegram, nil
} }
@@ -55,8 +66,7 @@ func clean(fields []string) []string {
return values[:len(values)-1] return values[:len(values)-1]
} }
func getFields(regEx string, field string) (fields map[string]string) {
func getFields(regEx, field string) (fields map[string]string) {
var compRegEx = regexp.MustCompile(regEx) var compRegEx = regexp.MustCompile(regEx)
match := compRegEx.FindStringSubmatch(field) match := compRegEx.FindStringSubmatch(field)
@@ -70,11 +80,22 @@ func getFields(regEx, field string) (fields map[string]string) {
return return
} }
func isPln(telegram Telegram) bool { func getFlopValues(s string) []string {
for _, value := range telegram.Fields { // get rid of ()
if strings.HasPrefix(value, TypePLN) { s = strings.TrimSpace(s)
return true s = s[1 : len(s)-1]
} return strings.Split(s, "-")
}
func parseFlight(s string) (flightNumber string, registerNumber string) {
if l := strings.Index(s, "/"); l > 0 {
return s[0:l], s[l+1:]
} }
return false return s, ""
}
func getAirportAndTime(s string) (airport string, arrivalTime time.Time) {
fields := getFields(DestinationRegex, s)
arrivalTime, _ = time.Parse("1504", fields["time"])
return fields["dest"], arrivalTime
} }
+54 -4
View File
@@ -5,15 +5,15 @@ import (
"testing" "testing"
) )
func TestBasicFlop(t *testing.T) { func TestBasicFlop(t *testing.T) {
ast := assert.New(t) ast := assert.New(t)
s :=`ZCZC TMQ2526 141605 s := `ZCZC TMQ2526 141605
FF ZBTJZPZX FF ZBTJZPZX
141604 ZBACZQZX 141604 ZBACZQZX
(ARR-JAE7433/A0132-RKSI-ZBTJ1604) (ARR-JAE7433/A0132-RKSI-ZBTJ1604)
NNNN` NNNN`
tele, err := Parse(s) tele, err := Parse(s)
ast.Nil(err, "Should be no error") ast.Nil(err, "Should be no error")
ast.Equal(tele.Complete, true, "Should be complete telegram") ast.Equal(tele.Complete, true, "Should be complete telegram")
ast.Equal(4, len(tele.Fields), "get 4 lines") ast.Equal(4, len(tele.Fields), "get 4 lines")
ast.Equal("ZCZC TMQ2526 141605", tele.Fields[0]) ast.Equal("ZCZC TMQ2526 141605", tele.Fields[0])
@@ -42,7 +42,7 @@ SI:A/C CHG
= =
NNNN` NNNN`
tele, err := Parse(s) tele, err := Parse(s)
ast.Nil(err, "Should be no error") ast.Nil(err, "Should be no error")
ast.Equal(tele.Complete, true, "Should be complete telegram") ast.Equal(tele.Complete, true, "Should be complete telegram")
ast.Equal(15, len(tele.Fields), "get 15 lines") ast.Equal(15, len(tele.Fields), "get 15 lines")
ast.Equal("ZCZC TMQ2886 142347", tele.Fields[0]) ast.Equal("ZCZC TMQ2886 142347", tele.Fields[0])
@@ -50,3 +50,53 @@ NNNN`
ast.Equal(true, tele.Type == "PLN", "This is a plan") ast.Equal(true, tele.Type == "PLN", "This is a plan")
} }
func TestGetValues(t *testing.T) {
ast := assert.New(t)
text := "ZBTJ1604"
values := getFields(DestinationRegex, text)
ast.Equal(2, len(values))
ast.Equal("ZBTJ", values["dest"])
ast.Equal("1604", values["time"])
}
func TestParseDestAndTime(t *testing.T) {
ast := assert.New(t)
text := `ZBTJ1604`
dest, destTime := getAirportAndTime(text)
ast.Equal("ZBTJ", dest)
ast.Equal(16, destTime.Hour())
ast.Equal(4, destTime.Minute())
text = `ZBTJ`
dest, _ = getAirportAndTime(text)
ast.Equal("ZBTJ", dest)
}
func TestGetFlopValues(t *testing.T) {
ast := assert.New(t)
text := `(ARR-JAE7433/A0132-RKSI-ZBTJ1604-ZBTJ)`
values := getFlopValues(text)
ast.Equal(5, len(values))
ast.Equal("ARR", values[0])
ast.Equal("JAE7433/A0132", values[1])
ast.Equal("RKSI", values[2])
ast.Equal("ZBTJ1604", values[3])
ast.Equal("ZBTJ", values[4])
}
func TestGetFlight(t *testing.T) {
ast := assert.New(t)
text := `JAE7433/A0132`
fn, rn := parseFlight(text)
ast.Equal("JAE7433", fn)
ast.Equal("A0132", rn)
text = "JAE1234"
fn, rn = parseFlight(text)
ast.Equal("JAE1234", fn)
ast.Equal("", rn)
}