// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Code generated by "internal/cmd/pdatagen/main.go". DO NOT EDIT.
// To regenerate this file run "make genpdata".

package internal

import (
	"encoding/binary"
	"fmt"
	"sync"

	"go.opentelemetry.io/collector/pdata/internal/json"
	"go.opentelemetry.io/collector/pdata/internal/metadata"
	"go.opentelemetry.io/collector/pdata/internal/proto"
)

type ProfilesRequest struct {
	RequestContext *RequestContext
	ProfilesData   ProfilesData
	FormatVersion  uint32
}

var (
	protoPoolProfilesRequest = sync.Pool{
		New: func() any {
			return &ProfilesRequest{}
		},
	}
)

func NewProfilesRequest() *ProfilesRequest {
	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		return &ProfilesRequest{}
	}
	return protoPoolProfilesRequest.Get().(*ProfilesRequest)
}

func DeleteProfilesRequest(orig *ProfilesRequest, nullable bool) {
	if orig == nil {
		return
	}

	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		orig.Reset()
		return
	}
	DeleteRequestContext(orig.RequestContext, true)
	DeleteProfilesData(&orig.ProfilesData, false)

	orig.Reset()
	if nullable {
		protoPoolProfilesRequest.Put(orig)
	}
}

func CopyProfilesRequest(dest, src *ProfilesRequest) *ProfilesRequest {
	// If copying to same object, just return.
	if src == dest {
		return dest
	}

	if src == nil {
		return nil
	}

	if dest == nil {
		dest = NewProfilesRequest()
	}
	dest.RequestContext = CopyRequestContext(dest.RequestContext, src.RequestContext)

	CopyProfilesData(&dest.ProfilesData, &src.ProfilesData)

	dest.FormatVersion = src.FormatVersion

	return dest
}

func CopyProfilesRequestSlice(dest, src []ProfilesRequest) []ProfilesRequest {
	var newDest []ProfilesRequest
	if cap(dest) < len(src) {
		newDest = make([]ProfilesRequest, len(src))
	} else {
		newDest = dest[:len(src)]
		// Cleanup the rest of the elements so GC can free the memory.
		// This can happen when len(src) < len(dest) < cap(dest).
		for i := len(src); i < len(dest); i++ {
			DeleteProfilesRequest(&dest[i], false)
		}
	}
	for i := range src {
		CopyProfilesRequest(&newDest[i], &src[i])
	}
	return newDest
}

func CopyProfilesRequestPtrSlice(dest, src []*ProfilesRequest) []*ProfilesRequest {
	var newDest []*ProfilesRequest
	if cap(dest) < len(src) {
		newDest = make([]*ProfilesRequest, len(src))
		// Copy old pointers to re-use.
		copy(newDest, dest)
		// Add new pointers for missing elements from len(dest) to len(srt).
		for i := len(dest); i < len(src); i++ {
			newDest[i] = NewProfilesRequest()
		}
	} else {
		newDest = dest[:len(src)]
		// Cleanup the rest of the elements so GC can free the memory.
		// This can happen when len(src) < len(dest) < cap(dest).
		for i := len(src); i < len(dest); i++ {
			DeleteProfilesRequest(dest[i], true)
			dest[i] = nil
		}
		// Add new pointers for missing elements.
		// This can happen when len(dest) < len(src) < cap(dest).
		for i := len(dest); i < len(src); i++ {
			newDest[i] = NewProfilesRequest()
		}
	}
	for i := range src {
		CopyProfilesRequest(newDest[i], src[i])
	}
	return newDest
}

func (orig *ProfilesRequest) Reset() {
	*orig = ProfilesRequest{}
}

// MarshalJSON marshals all properties from the current struct to the destination stream.
func (orig *ProfilesRequest) MarshalJSON(dest *json.Stream) {
	dest.WriteObjectStart()
	if orig.RequestContext != nil {
		dest.WriteObjectField("requestContext")
		orig.RequestContext.MarshalJSON(dest)
	}
	dest.WriteObjectField("profilesData")
	orig.ProfilesData.MarshalJSON(dest)
	if orig.FormatVersion != uint32(0) {
		dest.WriteObjectField("formatVersion")
		dest.WriteUint32(orig.FormatVersion)
	}
	dest.WriteObjectEnd()
}

// UnmarshalJSON unmarshals all properties from the current struct from the source iterator.
func (orig *ProfilesRequest) UnmarshalJSON(iter *json.Iterator) {
	for f := iter.ReadObject(); f != ""; f = iter.ReadObject() {
		switch f {
		case "requestContext", "request_context":
			orig.RequestContext = NewRequestContext()
			orig.RequestContext.UnmarshalJSON(iter)
		case "profilesData", "profiles_data":

			orig.ProfilesData.UnmarshalJSON(iter)
		case "formatVersion", "format_version":
			orig.FormatVersion = iter.ReadUint32()
		default:
			iter.HandleUnknownField(f)
		}
	}
}

func (orig *ProfilesRequest) SizeProto() int {
	var n int
	var l int
	_ = l
	if orig.RequestContext != nil {
		l = orig.RequestContext.SizeProto()
		n += 1 + proto.Sov(uint64(l)) + l
	}
	l = orig.ProfilesData.SizeProto()
	n += 1 + proto.Sov(uint64(l)) + l
	if orig.FormatVersion != uint32(0) {
		n += 5
	}
	return n
}

func (orig *ProfilesRequest) MarshalProto(buf []byte) int {
	pos := len(buf)
	var l int
	_ = l
	if orig.RequestContext != nil {
		l = orig.RequestContext.MarshalProto(buf[:pos])
		pos -= l
		pos = proto.EncodeVarint(buf, pos, uint64(l))
		pos--
		buf[pos] = 0x12
	}
	l = orig.ProfilesData.MarshalProto(buf[:pos])
	pos -= l
	pos = proto.EncodeVarint(buf, pos, uint64(l))
	pos--
	buf[pos] = 0x1a

	if orig.FormatVersion != uint32(0) {
		pos -= 4
		binary.LittleEndian.PutUint32(buf[pos:], uint32(orig.FormatVersion))
		pos--
		buf[pos] = 0xd
	}
	return len(buf) - pos
}

func (orig *ProfilesRequest) UnmarshalProto(buf []byte) error {
	var err error
	var fieldNum int32
	var wireType proto.WireType

	l := len(buf)
	pos := 0
	for pos < l {
		// If in a group parsing, move to the next tag.
		fieldNum, wireType, pos, err = proto.ConsumeTag(buf, pos)
		if err != nil {
			return err
		}
		switch fieldNum {

		case 2:
			if wireType != proto.WireTypeLen {
				return fmt.Errorf("proto: wrong wireType = %d for field RequestContext", wireType)
			}
			var length int
			length, pos, err = proto.ConsumeLen(buf, pos)
			if err != nil {
				return err
			}
			startPos := pos - length

			orig.RequestContext = NewRequestContext()
			err = orig.RequestContext.UnmarshalProto(buf[startPos:pos])
			if err != nil {
				return err
			}

		case 3:
			if wireType != proto.WireTypeLen {
				return fmt.Errorf("proto: wrong wireType = %d for field ProfilesData", wireType)
			}
			var length int
			length, pos, err = proto.ConsumeLen(buf, pos)
			if err != nil {
				return err
			}
			startPos := pos - length

			err = orig.ProfilesData.UnmarshalProto(buf[startPos:pos])
			if err != nil {
				return err
			}

		case 1:
			if wireType != proto.WireTypeI32 {
				return fmt.Errorf("proto: wrong wireType = %d for field FormatVersion", wireType)
			}
			var num uint32
			num, pos, err = proto.ConsumeI32(buf, pos)
			if err != nil {
				return err
			}

			orig.FormatVersion = uint32(num)
		default:
			pos, err = proto.ConsumeUnknown(buf, pos, wireType)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func GenTestProfilesRequest() *ProfilesRequest {
	orig := NewProfilesRequest()
	orig.RequestContext = GenTestRequestContext()
	orig.ProfilesData = *GenTestProfilesData()
	orig.FormatVersion = uint32(13)
	return orig
}

func GenTestProfilesRequestPtrSlice() []*ProfilesRequest {
	orig := make([]*ProfilesRequest, 5)
	orig[0] = NewProfilesRequest()
	orig[1] = GenTestProfilesRequest()
	orig[2] = NewProfilesRequest()
	orig[3] = GenTestProfilesRequest()
	orig[4] = NewProfilesRequest()
	return orig
}

func GenTestProfilesRequestSlice() []ProfilesRequest {
	orig := make([]ProfilesRequest, 5)
	orig[1] = *GenTestProfilesRequest()
	orig[3] = *GenTestProfilesRequest()
	return orig
}
