// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package proto

import (
	

	
	
)

// Merge merges src into dst, which must be a message with the same descriptor.
//
// Populated scalar fields in src are copied to dst, while populated
// singular messages in src are merged into dst by recursively calling Merge.
// The elements of every list field in src is appended to the corresponded
// list fields in dst. The entries of every map field in src is copied into
// the corresponding map field in dst, possibly replacing existing entries.
// The unknown fields of src are appended to the unknown fields of dst.
//
// It is semantically equivalent to unmarshaling the encoded form of src
// into dst with the UnmarshalOptions.Merge option specified.
func (,  Message) {
	// TODO: Should nil src be treated as semantically equivalent to a
	// untyped, read-only, empty message? What about a nil dst?

	,  := .ProtoReflect(), .ProtoReflect()
	if .Descriptor() != .Descriptor() {
		if ,  := .Descriptor().FullName(), .Descriptor().FullName();  !=  {
			panic(fmt.Sprintf("descriptor mismatch: %v != %v", , ))
		}
		panic("descriptor mismatch")
	}
	mergeOptions{}.mergeMessage(, )
}

// Clone returns a deep copy of m.
// If the top-level message is invalid, it returns an invalid message as well.
func ( Message) Message {
	// NOTE: Most usages of Clone assume the following properties:
	//	t := reflect.TypeOf(m)
	//	t == reflect.TypeOf(m.ProtoReflect().New().Interface())
	//	t == reflect.TypeOf(m.ProtoReflect().Type().Zero().Interface())
	//
	// Embedding protobuf messages breaks this since the parent type will have
	// a forwarded ProtoReflect method, but the Interface method will return
	// the underlying embedded message type.
	if  == nil {
		return nil
	}
	 := .ProtoReflect()
	if !.IsValid() {
		return .Type().Zero().Interface()
	}
	 := .New()
	mergeOptions{}.mergeMessage(, )
	return .Interface()
}

// mergeOptions provides a namespace for merge functions, and can be
// exported in the future if we add user-visible merge options.
type mergeOptions struct{}

func ( mergeOptions) (,  protoreflect.Message) {
	 := protoMethods()
	if  != nil && .Merge != nil {
		 := protoiface.MergeInput{
			Destination: ,
			Source:      ,
		}
		 := .Merge()
		if .Flags&protoiface.MergeComplete != 0 {
			return
		}
	}

	if !.IsValid() {
		panic(fmt.Sprintf("cannot merge into invalid %v message", .Descriptor().FullName()))
	}

	.Range(func( protoreflect.FieldDescriptor,  protoreflect.Value) bool {
		switch {
		case .IsList():
			.mergeList(.Mutable().List(), .List(), )
		case .IsMap():
			.mergeMap(.Mutable().Map(), .Map(), .MapValue())
		case .Message() != nil:
			.(.Mutable().Message(), .Message())
		case .Kind() == protoreflect.BytesKind:
			.Set(, .cloneBytes())
		default:
			.Set(, )
		}
		return true
	})

	if len(.GetUnknown()) > 0 {
		.SetUnknown(append(.GetUnknown(), .GetUnknown()...))
	}
}

func ( mergeOptions) (,  protoreflect.List,  protoreflect.FieldDescriptor) {
	// Merge semantics appends to the end of the existing list.
	for ,  := 0, .Len();  < ; ++ {
		switch  := .Get(); {
		case .Message() != nil:
			 := .NewElement()
			.mergeMessage(.Message(), .Message())
			.Append()
		case .Kind() == protoreflect.BytesKind:
			.Append(.cloneBytes())
		default:
			.Append()
		}
	}
}

func ( mergeOptions) (,  protoreflect.Map,  protoreflect.FieldDescriptor) {
	// Merge semantics replaces, rather than merges into existing entries.
	.Range(func( protoreflect.MapKey,  protoreflect.Value) bool {
		switch {
		case .Message() != nil:
			 := .NewValue()
			.mergeMessage(.Message(), .Message())
			.Set(, )
		case .Kind() == protoreflect.BytesKind:
			.Set(, .cloneBytes())
		default:
			.Set(, )
		}
		return true
	})
}

func ( mergeOptions) ( protoreflect.Value) protoreflect.Value {
	return protoreflect.ValueOfBytes(append([]byte{}, .Bytes()...))
}