short/internal/oas/oas_json_gen.go

303 lines
7 KiB
Go

// Code generated by ogen, DO NOT EDIT.
package oas
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 *CreateShortURLBadRequest) Encode(e *jx.Encoder) {
e.ObjStart()
s.encodeFields(e)
e.ObjEnd()
}
// encodeFields encodes fields.
func (s *CreateShortURLBadRequest) encodeFields(e *jx.Encoder) {
{
if s.Message.Set {
e.FieldStart("message")
s.Message.Encode(e)
}
}
}
var jsonFieldsNameOfCreateShortURLBadRequest = [1]string{
0: "message",
}
// Decode decodes CreateShortURLBadRequest from json.
func (s *CreateShortURLBadRequest) Decode(d *jx.Decoder) error {
if s == nil {
return errors.New("invalid: unable to decode CreateShortURLBadRequest to nil")
}
if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
switch string(k) {
case "message":
if err := func() error {
s.Message.Reset()
if err := s.Message.Decode(d); 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 CreateShortURLBadRequest")
}
return nil
}
// MarshalJSON implements stdjson.Marshaler.
func (s *CreateShortURLBadRequest) MarshalJSON() ([]byte, error) {
e := jx.Encoder{}
s.Encode(&e)
return e.Bytes(), nil
}
// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *CreateShortURLBadRequest) UnmarshalJSON(data []byte) error {
d := jx.DecodeBytes(data)
return s.Decode(d)
}
// Encode implements json.Marshaler.
func (s *CreateShortURLCreated) Encode(e *jx.Encoder) {
e.ObjStart()
s.encodeFields(e)
e.ObjEnd()
}
// encodeFields encodes fields.
func (s *CreateShortURLCreated) encodeFields(e *jx.Encoder) {
{
e.FieldStart("shorten")
e.Str(s.Shorten)
}
}
var jsonFieldsNameOfCreateShortURLCreated = [1]string{
0: "shorten",
}
// Decode decodes CreateShortURLCreated from json.
func (s *CreateShortURLCreated) Decode(d *jx.Decoder) error {
if s == nil {
return errors.New("invalid: unable to decode CreateShortURLCreated to nil")
}
var requiredBitSet [1]uint8
if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
switch string(k) {
case "shorten":
requiredBitSet[0] |= 1 << 0
if err := func() error {
v, err := d.Str()
s.Shorten = string(v)
if err != nil {
return err
}
return nil
}(); err != nil {
return errors.Wrap(err, "decode field \"shorten\"")
}
default:
return d.Skip()
}
return nil
}); err != nil {
return errors.Wrap(err, "decode CreateShortURLCreated")
}
// Validate required fields.
var failures []validate.FieldError
for i, mask := range [1]uint8{
0b00000001,
} {
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(jsonFieldsNameOfCreateShortURLCreated) {
name = jsonFieldsNameOfCreateShortURLCreated[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 *CreateShortURLCreated) MarshalJSON() ([]byte, error) {
e := jx.Encoder{}
s.Encode(&e)
return e.Bytes(), nil
}
// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *CreateShortURLCreated) UnmarshalJSON(data []byte) error {
d := jx.DecodeBytes(data)
return s.Decode(d)
}
// Encode implements json.Marshaler.
func (s *CreateShortURLReq) Encode(e *jx.Encoder) {
e.ObjStart()
s.encodeFields(e)
e.ObjEnd()
}
// encodeFields encodes fields.
func (s *CreateShortURLReq) encodeFields(e *jx.Encoder) {
{
e.FieldStart("url")
e.Str(s.URL)
}
}
var jsonFieldsNameOfCreateShortURLReq = [1]string{
0: "url",
}
// Decode decodes CreateShortURLReq from json.
func (s *CreateShortURLReq) Decode(d *jx.Decoder) error {
if s == nil {
return errors.New("invalid: unable to decode CreateShortURLReq to nil")
}
var requiredBitSet [1]uint8
if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error {
switch string(k) {
case "url":
requiredBitSet[0] |= 1 << 0
if err := func() error {
v, err := d.Str()
s.URL = string(v)
if err != nil {
return err
}
return nil
}(); err != nil {
return errors.Wrap(err, "decode field \"url\"")
}
default:
return d.Skip()
}
return nil
}); err != nil {
return errors.Wrap(err, "decode CreateShortURLReq")
}
// Validate required fields.
var failures []validate.FieldError
for i, mask := range [1]uint8{
0b00000001,
} {
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(jsonFieldsNameOfCreateShortURLReq) {
name = jsonFieldsNameOfCreateShortURLReq[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 *CreateShortURLReq) MarshalJSON() ([]byte, error) {
e := jx.Encoder{}
s.Encode(&e)
return e.Bytes(), nil
}
// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *CreateShortURLReq) UnmarshalJSON(data []byte) error {
d := jx.DecodeBytes(data)
return s.Decode(d)
}
// Encode encodes string as json.
func (o OptString) Encode(e *jx.Encoder) {
if !o.Set {
return
}
e.Str(string(o.Value))
}
// Decode decodes string from json.
func (o *OptString) Decode(d *jx.Decoder) error {
if o == nil {
return errors.New("invalid: unable to decode OptString to nil")
}
o.Set = true
v, err := d.Str()
if err != nil {
return err
}
o.Value = string(v)
return nil
}
// MarshalJSON implements stdjson.Marshaler.
func (s OptString) MarshalJSON() ([]byte, error) {
e := jx.Encoder{}
s.Encode(&e)
return e.Bytes(), nil
}
// UnmarshalJSON implements stdjson.Unmarshaler.
func (s *OptString) UnmarshalJSON(data []byte) error {
d := jx.DecodeBytes(data)
return s.Decode(d)
}