127 lines
2.7 KiB
Go
127 lines
2.7 KiB
Go
// Code generated by ogen, DO NOT EDIT.
|
|
|
|
package api
|
|
|
|
import (
|
|
"math/bits"
|
|
"strconv"
|
|
|
|
"github.com/go-faster/errors"
|
|
"github.com/go-faster/jx"
|
|
|
|
"github.com/ogen-go/ogen/validate"
|
|
)
|
|
|
|
// Encode implements json.Marshaler.
|
|
func (s *Error) Encode(e *jx.Encoder) {
|
|
e.ObjStart()
|
|
s.encodeFields(e)
|
|
e.ObjEnd()
|
|
}
|
|
|
|
// encodeFields encodes fields.
|
|
func (s *Error) encodeFields(e *jx.Encoder) {
|
|
{
|
|
e.FieldStart("code")
|
|
e.Int64(s.Code)
|
|
}
|
|
{
|
|
e.FieldStart("message")
|
|
e.Str(s.Message)
|
|
}
|
|
}
|
|
|
|
var jsonFieldsNameOfError = [2]string{
|
|
0: "code",
|
|
1: "message",
|
|
}
|
|
|
|
// Decode decodes Error from json.
|
|
func (s *Error) Decode(d *jx.Decoder) error {
|
|
if s == nil {
|
|
return errors.New("invalid: unable to decode Error to nil")
|
|
}
|
|
var requiredBitSet [1]uint8
|
|
|
|
if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
|
|
switch string(k) {
|
|
case "code":
|
|
requiredBitSet[0] |= 1 << 0
|
|
if err := func() error {
|
|
v, err := d.Int64()
|
|
s.Code = int64(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}(); err != nil {
|
|
return errors.Wrap(err, "decode field \"code\"")
|
|
}
|
|
case "message":
|
|
requiredBitSet[0] |= 1 << 1
|
|
if err := func() error {
|
|
v, err := d.Str()
|
|
s.Message = string(v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}(); err != nil {
|
|
return errors.Wrap(err, "decode field \"message\"")
|
|
}
|
|
default:
|
|
return d.Skip()
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return errors.Wrap(err, "decode Error")
|
|
}
|
|
// Validate required fields.
|
|
var failures []validate.FieldError
|
|
for i, mask := range [1]uint8{
|
|
0b00000011,
|
|
} {
|
|
if result := (requiredBitSet[i] & mask) ^ mask; result != 0 {
|
|
// Mask only required fields and check equality to mask using XOR.
|
|
//
|
|
// If XOR result is not zero, result is not equal to expected, so some fields are missed.
|
|
// Bits of fields which would be set are actually bits of missed fields.
|
|
missed := bits.OnesCount8(result)
|
|
for bitN := 0; bitN < missed; bitN++ {
|
|
bitIdx := bits.TrailingZeros8(result)
|
|
fieldIdx := i*8 + bitIdx
|
|
var name string
|
|
if fieldIdx < len(jsonFieldsNameOfError) {
|
|
name = jsonFieldsNameOfError[fieldIdx]
|
|
} else {
|
|
name = strconv.Itoa(fieldIdx)
|
|
}
|
|
failures = append(failures, validate.FieldError{
|
|
Name: name,
|
|
Error: validate.ErrFieldRequired,
|
|
})
|
|
// Reset bit.
|
|
result &^= 1 << bitIdx
|
|
}
|
|
}
|
|
}
|
|
if len(failures) > 0 {
|
|
return &validate.Error{Fields: failures}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON implements stdjson.Marshaler.
|
|
func (s *Error) MarshalJSON() ([]byte, error) {
|
|
e := jx.Encoder{}
|
|
s.Encode(&e)
|
|
return e.Bytes(), nil
|
|
}
|
|
|
|
// UnmarshalJSON implements stdjson.Unmarshaler.
|
|
func (s *Error) UnmarshalJSON(data []byte) error {
|
|
d := jx.DecodeBytes(data)
|
|
return s.Decode(d)
|
|
}
|