// 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"
)

type EntityRef struct {
	SchemaUrl       string
	Type            string
	IdKeys          []string
	DescriptionKeys []string
}

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

func NewEntityRef() *EntityRef {
	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		return &EntityRef{}
	}
	return protoPoolEntityRef.Get().(*EntityRef)
}

func DeleteEntityRef(orig *EntityRef, nullable bool) {
	if orig == nil {
		return
	}

	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		orig.Reset()
		return
	}

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

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

	if src == nil {
		return nil
	}

	if dest == nil {
		dest = NewEntityRef()
	}
	dest.SchemaUrl = src.SchemaUrl
	dest.Type = src.Type
	dest.IdKeys = append(dest.IdKeys[:0], src.IdKeys...)

	dest.DescriptionKeys = append(dest.DescriptionKeys[:0], src.DescriptionKeys...)

	return dest
}

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

func CopyEntityRefPtrSlice(dest, src []*EntityRef) []*EntityRef {
	var newDest []*EntityRef
	if cap(dest) < len(src) {
		newDest = make([]*EntityRef, 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] = NewEntityRef()
		}
	} 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++ {
			DeleteEntityRef(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] = NewEntityRef()
		}
	}
	for i := range src {
		CopyEntityRef(newDest[i], src[i])
	}
	return newDest
}

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

// MarshalJSON marshals all properties from the current struct to the destination stream.
func (orig *EntityRef) MarshalJSON(dest *json.Stream) {
	dest.WriteObjectStart()
	if orig.SchemaUrl != "" {
		dest.WriteObjectField("schemaUrl")
		dest.WriteString(orig.SchemaUrl)
	}
	if orig.Type != "" {
		dest.WriteObjectField("type")
		dest.WriteString(orig.Type)
	}
	if len(orig.IdKeys) > 0 {
		dest.WriteObjectField("idKeys")
		dest.WriteArrayStart()
		dest.WriteString(orig.IdKeys[0])
		for i := 1; i < len(orig.IdKeys); i++ {
			dest.WriteMore()
			dest.WriteString(orig.IdKeys[i])
		}
		dest.WriteArrayEnd()
	}

	if len(orig.DescriptionKeys) > 0 {
		dest.WriteObjectField("descriptionKeys")
		dest.WriteArrayStart()
		dest.WriteString(orig.DescriptionKeys[0])
		for i := 1; i < len(orig.DescriptionKeys); i++ {
			dest.WriteMore()
			dest.WriteString(orig.DescriptionKeys[i])
		}
		dest.WriteArrayEnd()
	}

	dest.WriteObjectEnd()
}

// UnmarshalJSON unmarshals all properties from the current struct from the source iterator.
func (orig *EntityRef) UnmarshalJSON(iter *json.Iterator) {
	for f := iter.ReadObject(); f != ""; f = iter.ReadObject() {
		switch f {
		case "schemaUrl", "schema_url":
			orig.SchemaUrl = iter.ReadString()
		case "type":
			orig.Type = iter.ReadString()
		case "idKeys", "id_keys":
			for iter.ReadArray() {
				orig.IdKeys = append(orig.IdKeys, iter.ReadString())
			}

		case "descriptionKeys", "description_keys":
			for iter.ReadArray() {
				orig.DescriptionKeys = append(orig.DescriptionKeys, iter.ReadString())
			}

		default:
			iter.Skip()
		}
	}
}

func (orig *EntityRef) SizeProto() int {
	var n int
	var l int
	_ = l

	l = len(orig.SchemaUrl)
	if l > 0 {
		n += 1 + proto.Sov(uint64(l)) + l
	}

	l = len(orig.Type)
	if l > 0 {
		n += 1 + proto.Sov(uint64(l)) + l
	}
	for _, s := range orig.IdKeys {
		l = len(s)
		n += 1 + proto.Sov(uint64(l)) + l
	}
	for _, s := range orig.DescriptionKeys {
		l = len(s)
		n += 1 + proto.Sov(uint64(l)) + l
	}
	return n
}

func (orig *EntityRef) MarshalProto(buf []byte) int {
	pos := len(buf)
	var l int
	_ = l
	l = len(orig.SchemaUrl)
	if l > 0 {
		pos -= l
		copy(buf[pos:], orig.SchemaUrl)
		pos = proto.EncodeVarint(buf, pos, uint64(l))
		pos--
		buf[pos] = 0xa
	}
	l = len(orig.Type)
	if l > 0 {
		pos -= l
		copy(buf[pos:], orig.Type)
		pos = proto.EncodeVarint(buf, pos, uint64(l))
		pos--
		buf[pos] = 0x12
	}
	for i := len(orig.IdKeys) - 1; i >= 0; i-- {
		l = len(orig.IdKeys[i])
		pos -= l
		copy(buf[pos:], orig.IdKeys[i])
		pos = proto.EncodeVarint(buf, pos, uint64(l))
		pos--
		buf[pos] = 0x1a
	}
	for i := len(orig.DescriptionKeys) - 1; i >= 0; i-- {
		l = len(orig.DescriptionKeys[i])
		pos -= l
		copy(buf[pos:], orig.DescriptionKeys[i])
		pos = proto.EncodeVarint(buf, pos, uint64(l))
		pos--
		buf[pos] = 0x22
	}
	return len(buf) - pos
}

func (orig *EntityRef) 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 SchemaUrl", wireType)
			}
			var length int
			length, pos, err = proto.ConsumeLen(buf, pos)
			if err != nil {
				return err
			}
			startPos := pos - length
			orig.SchemaUrl = string(buf[startPos:pos])

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

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

		case 4:
			if wireType != proto.WireTypeLen {
				return fmt.Errorf("proto: wrong wireType = %d for field DescriptionKeys", wireType)
			}
			var length int
			length, pos, err = proto.ConsumeLen(buf, pos)
			if err != nil {
				return err
			}
			startPos := pos - length
			orig.DescriptionKeys = append(orig.DescriptionKeys, string(buf[startPos:pos]))
		default:
			pos, err = proto.ConsumeUnknown(buf, pos, wireType)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func GenTestEntityRef() *EntityRef {
	orig := NewEntityRef()
	orig.SchemaUrl = "test_schemaurl"
	orig.Type = "test_type"
	orig.IdKeys = []string{"", "test_idkeys"}
	orig.DescriptionKeys = []string{"", "test_descriptionkeys"}
	return orig
}

func GenTestEntityRefPtrSlice() []*EntityRef {
	orig := make([]*EntityRef, 5)
	orig[0] = NewEntityRef()
	orig[1] = GenTestEntityRef()
	orig[2] = NewEntityRef()
	orig[3] = GenTestEntityRef()
	orig[4] = NewEntityRef()
	return orig
}

func GenTestEntityRefSlice() []EntityRef {
	orig := make([]EntityRef, 5)
	orig[1] = *GenTestEntityRef()
	orig[3] = *GenTestEntityRef()
	return orig
}
