From 7f5b092c6405515863bae92c54235ad87523f4c7 Mon Sep 17 00:00:00 2001 From: Alex Savin Date: Tue, 8 Jul 2025 22:38:09 -0400 Subject: [PATCH] Refactor JSON unmarshalling in CustomTime types to simplify quote handling and improve null value checks --- mysubaru.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/mysubaru.go b/mysubaru.go index b9104c5..3b13dac 100644 --- a/mysubaru.go +++ b/mysubaru.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "log/slog" - "strings" "time" ) @@ -497,12 +496,12 @@ type CustomTime1 struct { func (ct *CustomTime1) UnmarshalJSON(b []byte) (err error) { // Use the correct layout string for the desired format const layout = "2006-01-02T15:04:05" - s := strings.Trim(string(b), "\\\"") // Remove surrounding quotes - if s == "null" { + // s := strings.Trim(string(b), "\\\"") // Remove surrounding quotes + if string(b) == "null" { ct.Time = time.Time{} return nil } - ct.Time, err = time.Parse(layout, string(b)) + ct.Time, err = time.Parse(layout, string(b)) // Parse the string with your custom layout return } @@ -515,11 +514,11 @@ type CustomTime2 struct { func (ct *CustomTime2) UnmarshalJSON(b []byte) (err error) { // Use the correct layout string for the desired format const layout = "2006-01-02T15:04:05-0700" - s := strings.Trim(string(b), "\\\"") // Remove surrounding quotes - if s == "null" { + // s := strings.Trim(string(b), "\\\"") // Remove surrounding quotes + if string(b) == "null" { ct.Time = time.Time{} return nil } - ct.Time, err = time.Parse(layout, s) // Parse the string with your custom layout + ct.Time, err = time.Parse(layout, string(b)) // Parse the string with your custom layout return }