// 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 (
	"fmt"
	"sync"

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

// Traces is the top-level struct that is propagated through the traces pipeline.
// Use NewTraces to create new instance, zero-initialized instance is not valid for use.
type ExportTraceServiceRequest struct {
	ResourceSpans []*ResourceSpans
}

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

func NewExportTraceServiceRequest() *ExportTraceServiceRequest {
	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		return &ExportTraceServiceRequest{}
	}
	return protoPoolExportTraceServiceRequest.Get().(*ExportTraceServiceRequest)
}

func DeleteExportTraceServiceRequest(orig *ExportTraceServiceRequest, nullable bool) {
	if orig == nil {
		return
	}

	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		orig.Reset()
		return
	}
	for i := range orig.ResourceSpans {
		DeleteResourceSpans(orig.ResourceSpans[i], true)
	}
	orig.Reset()
	if nullable {
		protoPoolExportTraceServiceRequest.Put(orig)
	}
}

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

	if src == nil {
		return nil
	}

	if dest == nil {
		dest = NewExportTraceServiceRequest()
	}
	dest.ResourceSpans = CopyResourceSpansPtrSlice(dest.ResourceSpans, src.ResourceSpans)

	return dest
}

func CopyExportTraceServiceRequestSlice(dest, src []ExportTraceServiceRequest) []ExportTraceServiceRequest {
	var newDest []ExportTraceServiceRequest
	if cap(dest) < len(src) {
		newDest = make([]ExportTraceServiceRequest, 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++ {
			DeleteExportTraceServiceRequest(&dest[i], false)
		}
	}
	for i := range src {
		CopyExportTraceServiceRequest(&newDest[i], &src[i])
	}
	return newDest
}

func CopyExportTraceServiceRequestPtrSlice(dest, src []*ExportTraceServiceRequest) []*ExportTraceServiceRequest {
	var newDest []*ExportTraceServiceRequest
	if cap(dest) < len(src) {
		newDest = make([]*ExportTraceServiceRequest, 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] = NewExportTraceServiceRequest()
		}
	} 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++ {
			DeleteExportTraceServiceRequest(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] = NewExportTraceServiceRequest()
		}
	}
	for i := range src {
		CopyExportTraceServiceRequest(newDest[i], src[i])
	}
	return newDest
}

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

// MarshalJSON marshals all properties from the current struct to the destination stream.
func (orig *ExportTraceServiceRequest) MarshalJSON(dest *json.Stream) {
	dest.WriteObjectStart()
	if len(orig.ResourceSpans) > 0 {
		dest.WriteObjectField("resourceSpans")
		dest.WriteArrayStart()
		orig.ResourceSpans[0].MarshalJSON(dest)
		for i := 1; i < len(orig.ResourceSpans); i++ {
			dest.WriteMore()
			orig.ResourceSpans[i].MarshalJSON(dest)
		}
		dest.WriteArrayEnd()
	}
	dest.WriteObjectEnd()
}

// UnmarshalJSON unmarshals all properties from the current struct from the source iterator.
func (orig *ExportTraceServiceRequest) UnmarshalJSON(iter *json.Iterator) {
	for f := iter.ReadObject(); f != ""; f = iter.ReadObject() {
		switch f {
		case "resourceSpans", "resource_spans":
			for iter.ReadArray() {
				orig.ResourceSpans = append(orig.ResourceSpans, NewResourceSpans())
				orig.ResourceSpans[len(orig.ResourceSpans)-1].UnmarshalJSON(iter)
			}

		default:
			iter.HandleUnknownField(f)
		}
	}
}

func (orig *ExportTraceServiceRequest) SizeProto() int {
	var n int
	var l int
	_ = l
	for i := range orig.ResourceSpans {
		l = orig.ResourceSpans[i].SizeProto()
		n += 1 + proto.Sov(uint64(l)) + l
	}
	return n
}

func (orig *ExportTraceServiceRequest) MarshalProto(buf []byte) int {
	pos := len(buf)
	var l int
	_ = l
	for i := len(orig.ResourceSpans) - 1; i >= 0; i-- {
		l = orig.ResourceSpans[i].MarshalProto(buf[:pos])
		pos -= l
		pos = proto.EncodeVarint(buf, pos, uint64(l))
		pos--
		buf[pos] = 0xa
	}
	return len(buf) - pos
}

func (orig *ExportTraceServiceRequest) 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 1:
			if wireType != proto.WireTypeLen {
				return fmt.Errorf("proto: wrong wireType = %d for field ResourceSpans", wireType)
			}
			var length int
			length, pos, err = proto.ConsumeLen(buf, pos)
			if err != nil {
				return err
			}
			startPos := pos - length
			orig.ResourceSpans = append(orig.ResourceSpans, NewResourceSpans())
			err = orig.ResourceSpans[len(orig.ResourceSpans)-1].UnmarshalProto(buf[startPos:pos])
			if err != nil {
				return err
			}
		default:
			pos, err = proto.ConsumeUnknown(buf, pos, wireType)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func GenTestExportTraceServiceRequest() *ExportTraceServiceRequest {
	orig := NewExportTraceServiceRequest()
	orig.ResourceSpans = []*ResourceSpans{{}, GenTestResourceSpans()}
	return orig
}

func GenTestExportTraceServiceRequestPtrSlice() []*ExportTraceServiceRequest {
	orig := make([]*ExportTraceServiceRequest, 5)
	orig[0] = NewExportTraceServiceRequest()
	orig[1] = GenTestExportTraceServiceRequest()
	orig[2] = NewExportTraceServiceRequest()
	orig[3] = GenTestExportTraceServiceRequest()
	orig[4] = NewExportTraceServiceRequest()
	return orig
}

func GenTestExportTraceServiceRequestSlice() []ExportTraceServiceRequest {
	orig := make([]ExportTraceServiceRequest, 5)
	orig[1] = *GenTestExportTraceServiceRequest()
	orig[3] = *GenTestExportTraceServiceRequest()
	return orig
}
