Unmarshalling sql.NullTime structures in Go

PHPz
Release: 2024-02-09 14:42:09
forward
1075 people have browsed it

在 Go 中解组 sql.NullTime 结构

php Editor Xiaoxin In the Go language, we often use the sql.NullTime structure to process the time field in the database. The NullTime structure can represent a nullable time value and is very suitable for handling null values in the database. In this article, we will explain how to unmarshal a sql.NullTime structure and how to correctly handle null situations that may exist within it. Whether you are a beginner or an experienced developer, this article will provide you with clear guidance to help you better understand and use the NullTime structure.

Question content

Given

type NullTime struct { Time time.Time Valid bool // Valid is true if Time is not NULL }
Copy after login

and

type PayinCount struct { DateShiftStart sql.NullTime `json:"dateShiftStart"` DateShiftEnd sql.NullTime `json:"dateShiftend"` }
Copy after login

When I process the following JSON

{ "dateShiftStart":"2023-10-16", "dateShiftEnd":"2023-10-23" }
Copy after login
Copy after login

and

var payinsCount PayinsCount err = json.Unmarshal(body, &payinsCount) if err != nil { sendErrorResponse(w, err.Error(), http.StatusBadRequest) return }
Copy after login
Copy after login

Where sendErrorResponse is the following auxiliary process

func sendErrorResponse(w http.ResponseWriter, err string, statusCode int) { messageStatusCode := MessageStatusCode{ Message: err, StatusCode: statusCode} w.WriteHeader(statusCode) json.NewEncoder(w).Encode(messageStatusCode) }
Copy after login

I received the following message

{ "message": "json: cannot unmarshal string into Go struct field PayinsCount.dateShiftStart of type sql.NullTime", "statusCode": 400 }
Copy after login

how to solve this problem?

Workaround

I ended up using the following. I added the following types.

type NullDate sql.NullTime
Copy after login

Then I changed the PayinsCount to use NullDate

type PayinsCount struct { DateShiftStart NullDate `json:"dateShiftStart,omitempty"` DateShiftEnd NullDate `json:"dateShiftend,omitempty"` }
Copy after login

Then I created

// UnmarshalJSON for NullDate func (nd *NullDate) UnmarshalJSON(b []byte) error { s := string(b) s = strings.ReplaceAll(s, "\"", "") x, err := time.Parse(time.DateOnly, s) if err != nil { nd.Valid = false return err } nd.Time = x nd.Valid = true return nil }
Copy after login

Now when I process the following JSON

{ "dateShiftStart":"2023-10-16", "dateShiftEnd":"2023-10-23" }
Copy after login
Copy after login

and

var payinsCount PayinsCount err = json.Unmarshal(body, &payinsCount) if err != nil { sendErrorResponse(w, err.Error(), http.StatusBadRequest) return }
Copy after login
Copy after login

It works. I ended up with a valid PayinsCount instance.

For completeness, here is the MarshalJSON function for NullDate

// MarshalJSON for NullDate func (nd NullDate) MarshalJSON() ([]byte, error) { if !nd.Valid { return []byte("null"), nil } val := fmt.Sprintf("\"%s\"", nd.Time.Format(time.DateOnly)) return []byte(val), nil }
Copy after login

Note the escaped double quotes - without them the encoding/json marshaling code processes the date string in 3 chunks and I get the following error

error(*encoding/json.SyntaxError) *{msg: "invalid character '-' after top-level value", Offset: 0}
Copy after login

The above is the detailed content of Unmarshalling sql.NullTime structures in Go. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!