From cf76638b8afce1f46f114a73b68b3119be3b876d Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Sat, 23 Sep 2023 18:16:18 -0700 Subject: [PATCH] Add report type --- convert.go | 11 +++++++++++ internal/parser/lexer.go | 3 ++- internal/parser/parser.go | 1 + taf.go | 4 ++++ taf_test.go | 1 + types.go | 13 +++++++++++++ 6 files changed, 32 insertions(+), 1 deletion(-) diff --git a/convert.go b/convert.go index 1f26639..06b1f20 100644 --- a/convert.go +++ b/convert.go @@ -1,5 +1,16 @@ package taf +func convertReportType(s string) ReportType { + switch s { + case "AMD": + return Amended + case "COR": + return Corrected + default: + return "" + } +} + func convertSkyConditionType(s string) SkyConditionType { switch s { case "FEW": diff --git a/internal/parser/lexer.go b/internal/parser/lexer.go index 7ae7e3b..717a20f 100644 --- a/internal/parser/lexer.go +++ b/internal/parser/lexer.go @@ -5,7 +5,8 @@ import ( ) var lex = lexer.MustSimple([]lexer.SimpleRule{ - {Name: "header", Pattern: `TAF (AMD|COR)? ?`}, + {Name: "header", Pattern: `TAF ?`}, + {Name: "Type", Pattern: "AMD|COR"}, {Name: "Remark", Pattern: `RMK[^\n]*`}, {Name: "Number", Pattern: `\d+`}, {Name: "Modifier", Pattern: `[+-]|VC`}, diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 575e3e4..e7012ad 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -6,6 +6,7 @@ import ( ) type AST struct { + Type *string `(@Type WS)?` Items []*Item `@@*` } diff --git a/taf.go b/taf.go index b562680..b1668fb 100644 --- a/taf.go +++ b/taf.go @@ -92,6 +92,10 @@ func DecodeWithOptions(r io.Reader, opts Options) (*Forecast, error) { fc := &Forecast{} out := reflect.ValueOf(fc).Elem() + if ast.Type != nil { + fc.ReportType = convertReportType(*ast.Type) + } + for _, item := range ast.Items { switch { case item.ID != nil: diff --git a/taf_test.go b/taf_test.go index f3c3eee..b3693c7 100644 --- a/taf_test.go +++ b/taf_test.go @@ -207,6 +207,7 @@ func TestZGSZ(t *testing.T) { TEMPO 2204/2208 TSRA SCT020 FEW023CB` expected := &Forecast{ + ReportType: Amended, Identifier: "ZGSZ", Airport: airports.Airport{ ICAO: "ZGSZ", diff --git a/types.go b/types.go index 905a53c..65a3863 100644 --- a/types.go +++ b/types.go @@ -9,6 +9,9 @@ import ( // Forecast represents a Terminal Aerodrome Forecast (TAF) weather report for a specific airport. type Forecast struct { + // ReportType represents the type of report this forecast describes. + ReportType ReportType `json:"report_type,omitempty"` + // Identifier holds the ICAO airport identifier for which this forecast was issued. Identifier string `json:"identifier,omitempty"` @@ -130,6 +133,16 @@ type Visibility struct { Unit units.Distance `json:"unit,omitempty"` } +// ReportType represents different types of reports. +type ReportType string + +const ( + // Amended represents a report issued when the previous report is no longer accurate. + Amended ReportType = "Amended" + // Corrected represents a correction to a previous report. + Corrected ReportType = "Corrected" +) + // SkyConditionType represents different types of sky conditions in the forecast. type SkyConditionType string