init push
This commit is contained in:
33
internal/transport/http/dto/null_int.go
Normal file
33
internal/transport/http/dto/null_int.go
Normal file
@@ -0,0 +1,33 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user