Enhance JSON unmarshalling for CustomTime types to handle null values and improve string parsing
All checks were successful
Golan Testing / testing (1.24.x, ubuntu-latest) (push) Successful in 25s

This commit is contained in:
2025-07-08 22:21:02 -04:00
parent 0b2ed38ca3
commit a455733f8b

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"log/slog" "log/slog"
"strings"
"time" "time"
) )
@ -496,6 +497,11 @@ type CustomTime1 struct {
func (ct *CustomTime1) UnmarshalJSON(b []byte) (err error) { func (ct *CustomTime1) UnmarshalJSON(b []byte) (err error) {
// Use the correct layout string for the desired format // Use the correct layout string for the desired format
const layout = "2006-01-02T15:04:05" const layout = "2006-01-02T15:04:05"
s := strings.Trim(string(b), `"`) // Remove surrounding quotes
if s == "null" {
ct.Time = time.Time{}
return nil
}
ct.Time, err = time.Parse(layout, string(b)) ct.Time, err = time.Parse(layout, string(b))
return return
} }
@ -507,7 +513,13 @@ type CustomTime2 struct {
// UnmarshalJSON implements the json.Unmarshaler interface // UnmarshalJSON implements the json.Unmarshaler interface
func (ct *CustomTime2) UnmarshalJSON(b []byte) (err error) { 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" const layout = "2006-01-02T15:04:05-0700"
ct.Time, err = time.Parse(layout, string(b)) // Parse the string using the custom layout s := strings.Trim(string(b), `"`) // Remove surrounding quotes
if s == "null" {
ct.Time = time.Time{}
return nil
}
ct.Time, err = time.Parse(layout, s) // Parse the string with your custom layout
return return
} }