34 lines
625 B
Go
34 lines
625 B
Go
package dto
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
)
|
|
|
|
// NullInt allows PATCH semantics:
|
|
// - field omitted => Set=false (no change)
|
|
// - value null => Set=true, Valid=false (set DB NULL)
|
|
// - value number => Set=true, Valid=true, Value=<number>
|
|
type NullInt struct {
|
|
Set bool
|
|
Valid bool
|
|
Value int
|
|
}
|
|
|
|
func (n *NullInt) UnmarshalJSON(data []byte) error {
|
|
n.Set = true
|
|
trimmed := bytes.TrimSpace(data)
|
|
if bytes.Equal(trimmed, []byte("null")) {
|
|
n.Valid = false
|
|
n.Value = 0
|
|
return nil
|
|
}
|
|
var v int
|
|
if err := json.Unmarshal(trimmed, &v); err != nil {
|
|
return err
|
|
}
|
|
n.Valid = true
|
|
n.Value = v
|
|
return nil
|
|
}
|