From 7bcf80febb53f2e28c9f54fd4aa9cc0c690f5926 Mon Sep 17 00:00:00 2001 From: siddharth ravikumar Date: Sun, 29 May 2022 11:51:47 -0400 Subject: nws: remove "NWS" from types Also rename: - `Forecast` function to `GetForecast`. - `ForecastHourly` function to `GetForecastHourly`. --- nws/nws.go | 45 ++++++++++++++++++++++----------------------- nws/nws_test.go | 8 ++++---- 2 files changed, 26 insertions(+), 27 deletions(-) (limited to 'nws') diff --git a/nws/nws.go b/nws/nws.go index 5b68169..8c28f19 100644 --- a/nws/nws.go +++ b/nws/nws.go @@ -2,7 +2,6 @@ // SPDX-License-Identifier: ISC // Functions for accessing the National Weather Service API. -// TODO: remove NWS prefix from all types. package nws import ( @@ -22,7 +21,7 @@ type PointLocation struct { Properties PointLocationProperties } -type NWSPointProperties struct { +type PointProperties struct { GridId string GridX int GridY int @@ -31,11 +30,11 @@ type NWSPointProperties struct { RelativeLocation PointLocation } -type NWSPoint struct { - Properties NWSPointProperties +type Point struct { + Properties PointProperties } -type NWSForecastPeriod struct { +type ForecastPeriod struct { Number int Name string StartTime string @@ -50,29 +49,29 @@ type NWSForecastPeriod struct { DetailedForecast string } -type NWSForecastProperties struct { - Periods []NWSForecastPeriod +type ForecastProperties struct { + Periods []ForecastPeriod } -type NWSForecast struct { - Properties NWSForecastProperties +type Forecast struct { + Properties ForecastProperties } -type NWSError struct { +type Error struct { Title string Type string Status int Detail string } -func (e NWSError) Error() string { +func (e Error) Error() string { return fmt.Sprintf("%d: %s: %s", e.Status, e.Type, e.Detail) } // NWS `/points` endpoint. // -// TODO: return NWSError instead of error -func Points(lat, lng float32) (*NWSPoint, error) { +// TODO: return Error instead of error +func Points(lat, lng float32) (*Point, error) { url := fmt.Sprintf("https://api.weather.gov/points/%.4f,%.4f", lat, lng) resp, err := client.Get(url) if err != nil { @@ -87,7 +86,7 @@ func Points(lat, lng float32) (*NWSPoint, error) { // Check if the request failed. if resp.StatusCode != 200 { - perr := new(NWSError) + perr := new(Error) err := json.Unmarshal(body, perr) if err != nil { return nil, fmt.Errorf("points: json: %v", err) @@ -96,7 +95,7 @@ func Points(lat, lng float32) (*NWSPoint, error) { } // Unmarshal. - point := new(NWSPoint) + point := new(Point) err = json.Unmarshal(body, point) if err != nil { return nil, fmt.Errorf("points: decode: %v", err) @@ -112,8 +111,8 @@ func Points(lat, lng float32) (*NWSPoint, error) { // NWS forecast endpoint. // -// TODO: return NWSError instead of error. -func Forecast(point *NWSPoint) (*NWSForecast, error) { +// TODO: return Error instead of error. +func GetForecast(point *Point) (*Forecast, error) { if point == nil { return nil, fmt.Errorf("forecast: point nil") } @@ -135,7 +134,7 @@ func Forecast(point *NWSPoint) (*NWSForecast, error) { // Check if the request failed. if resp.StatusCode != 200 { - perr := new(NWSError) + perr := new(Error) err := json.Unmarshal(body, perr) if err != nil { return nil, fmt.Errorf("forecast: json: %v", err) @@ -144,7 +143,7 @@ func Forecast(point *NWSPoint) (*NWSForecast, error) { } // Unmarshal. - forecast := new(NWSForecast) + forecast := new(Forecast) err = json.Unmarshal(body, forecast) if err != nil { return nil, fmt.Errorf("forecast: decode: %v", err) @@ -157,8 +156,8 @@ func Forecast(point *NWSPoint) (*NWSForecast, error) { // NWS forecast hourly endpoint. // -// TODO: return NWSError instead of error -func ForecastHourly(point *NWSPoint) (*NWSForecast, error) { +// TODO: return Error instead of error +func GetForecastHourly(point *Point) (*Forecast, error) { if point == nil { return nil, fmt.Errorf("forecast hourly: point nil") } @@ -180,7 +179,7 @@ func ForecastHourly(point *NWSPoint) (*NWSForecast, error) { // Check if the request failed. if resp.StatusCode != 200 { - perr := new(NWSError) + perr := new(Error) err := json.Unmarshal(body, perr) if err != nil { return nil, fmt.Errorf("forecast hourly: json: %v", err) @@ -189,7 +188,7 @@ func ForecastHourly(point *NWSPoint) (*NWSForecast, error) { } // Unmarshal. - forecast := new(NWSForecast) + forecast := new(Forecast) err = json.Unmarshal(body, forecast) if err != nil { return nil, fmt.Errorf("forecast hourly: decode: %v", err) diff --git a/nws/nws_test.go b/nws/nws_test.go index 9dcd53e..ffa05c8 100644 --- a/nws/nws_test.go +++ b/nws/nws_test.go @@ -42,7 +42,7 @@ func TestPoints(t *testing.T) { } } -func TestForecast(t *testing.T) { +func TestGetForecast(t *testing.T) { // Get point. np, err := Points(41.115, -83.177) if err != nil { @@ -51,7 +51,7 @@ func TestForecast(t *testing.T) { } // Get forecast. - fc, err := Forecast(np) + fc, err := GetForecast(np) if err != nil { t.Errorf("error: %v", err) return @@ -96,7 +96,7 @@ func TestForecast(t *testing.T) { } } -func TestForecastHourly(t *testing.T) { +func TestGetForecastHourly(t *testing.T) { // Get point. np, err := Points(41.115, -83.177) if err != nil { @@ -105,7 +105,7 @@ func TestForecastHourly(t *testing.T) { } // Get forecast hourly. - fc, err := ForecastHourly(np) + fc, err := GetForecastHourly(np) if err != nil { t.Errorf("error: %v", err) return -- cgit v1.2.3