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
+35 -14
View File
@@ -4,6 +4,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time"
) )
type Telegram struct { type Telegram struct {
@@ -13,17 +14,21 @@ type Telegram struct {
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
@@ -36,8 +41,14 @@ func Parse(text string) (*Telegram, error) {
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
} }
+50
View File
@@ -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)
}