// 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 *CreatePostBadRequest) Encode(e *jx.Encoder) { e.ObjStart() s.encodeFields(e) e.ObjEnd() } // encodeFields encodes fields. func (s *CreatePostBadRequest) encodeFields(e *jx.Encoder) { { if s.Message.Set { e.FieldStart("message") s.Message.Encode(e) } } } var jsonFieldsNameOfCreatePostBadRequest = [1]string{ 0: "message", } // Decode decodes CreatePostBadRequest from json. func (s *CreatePostBadRequest) Decode(d *jx.Decoder) error { if s == nil { return errors.New("invalid: unable to decode CreatePostBadRequest 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 CreatePostBadRequest") } return nil } // MarshalJSON implements stdjson.Marshaler. func (s *CreatePostBadRequest) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. func (s *CreatePostBadRequest) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } // Encode implements json.Marshaler. func (s *CreatePostCreated) Encode(e *jx.Encoder) { e.ObjStart() s.encodeFields(e) e.ObjEnd() } // encodeFields encodes fields. func (s *CreatePostCreated) encodeFields(e *jx.Encoder) { { if s.Shorten.Set { e.FieldStart("shorten") s.Shorten.Encode(e) } } } var jsonFieldsNameOfCreatePostCreated = [1]string{ 0: "shorten", } // Decode decodes CreatePostCreated from json. func (s *CreatePostCreated) Decode(d *jx.Decoder) error { if s == nil { return errors.New("invalid: unable to decode CreatePostCreated to nil") } if err := d.ObjBytes(func(d *jx.Decoder, k []byte) error { switch string(k) { case "shorten": if err := func() error { s.Shorten.Reset() if err := s.Shorten.Decode(d); 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 CreatePostCreated") } return nil } // MarshalJSON implements stdjson.Marshaler. func (s *CreatePostCreated) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. func (s *CreatePostCreated) UnmarshalJSON(data []byte) error { d := jx.DecodeBytes(data) return s.Decode(d) } // Encode implements json.Marshaler. func (s *CreatePostReq) Encode(e *jx.Encoder) { e.ObjStart() s.encodeFields(e) e.ObjEnd() } // encodeFields encodes fields. func (s *CreatePostReq) encodeFields(e *jx.Encoder) { { e.FieldStart("url") e.Str(s.URL) } } var jsonFieldsNameOfCreatePostReq = [1]string{ 0: "url", } // Decode decodes CreatePostReq from json. func (s *CreatePostReq) Decode(d *jx.Decoder) error { if s == nil { return errors.New("invalid: unable to decode CreatePostReq 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 CreatePostReq") } // 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(jsonFieldsNameOfCreatePostReq) { name = jsonFieldsNameOfCreatePostReq[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 *CreatePostReq) MarshalJSON() ([]byte, error) { e := jx.Encoder{} s.Encode(&e) return e.Bytes(), nil } // UnmarshalJSON implements stdjson.Unmarshaler. func (s *CreatePostReq) 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) }