github.com/metaworking/channeld@v0.7.3/internal/testpb/merge.go (about)

     1  package testpb
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/metaworking/channeld/pkg/channeldpb"
     7  	"github.com/metaworking/channeld/pkg/common"
     8  	"google.golang.org/protobuf/proto"
     9  )
    10  
    11  // Implement [channeld.MergeableChannelData]
    12  func (dst *TestMergeMessage) Merge(src proto.Message, options *channeldpb.ChannelDataMergeOptions, spatialNotifier common.SpatialInfoChangedNotifier) error {
    13  	srcMsg, ok := src.(*TestMergeMessage)
    14  	if !ok {
    15  		return errors.New("src is not a TestMergeMessage")
    16  	}
    17  
    18  	if options.ShouldReplaceList {
    19  		// Make a deep copy
    20  		dst.List = append([]string{}, srcMsg.List...)
    21  	} else {
    22  		dst.List = append(dst.List, srcMsg.List...)
    23  	}
    24  
    25  	if options.ListSizeLimit > 0 {
    26  		if options.TruncateTop {
    27  			start := len(dst.List) - int(options.ListSizeLimit)
    28  			if start < 0 {
    29  				start = 0
    30  			}
    31  			dst.List = dst.List[start:]
    32  		} else {
    33  			dst.List = dst.List[:options.ListSizeLimit]
    34  		}
    35  	}
    36  
    37  	for k, v := range srcMsg.Kv {
    38  		if v.Removed {
    39  			delete(dst.Kv, k)
    40  		} else {
    41  			dst.Kv[k] = v
    42  		}
    43  	}
    44  	return nil
    45  }