github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/swarmkit/api/deepcopy/copy.go (about)

     1  package deepcopy
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/gogo/protobuf/types"
     8  )
     9  
    10  // CopierFrom can be implemented if an object knows how to copy another into itself.
    11  type CopierFrom interface {
    12  	// Copy takes the fields from src and copies them into the target object.
    13  	//
    14  	// Calling this method with a nil receiver or a nil src may panic.
    15  	CopyFrom(src interface{})
    16  }
    17  
    18  // Copy copies src into dst. dst and src must have the same type.
    19  //
    20  // If the type has a copy function defined, it will be used.
    21  //
    22  // Default implementations for builtin types and well known protobuf types may
    23  // be provided.
    24  //
    25  // If the copy cannot be performed, this function will panic. Make sure to test
    26  // types that use this function.
    27  func Copy(dst, src interface{}) {
    28  	switch dst := dst.(type) {
    29  	case *types.Any:
    30  		src := src.(*types.Any)
    31  		dst.TypeUrl = src.TypeUrl
    32  		if src.Value != nil {
    33  			dst.Value = make([]byte, len(src.Value))
    34  			copy(dst.Value, src.Value)
    35  		} else {
    36  			dst.Value = nil
    37  		}
    38  	case *types.Duration:
    39  		src := src.(*types.Duration)
    40  		*dst = *src
    41  	case *time.Duration:
    42  		src := src.(*time.Duration)
    43  		*dst = *src
    44  	case *types.Timestamp:
    45  		src := src.(*types.Timestamp)
    46  		*dst = *src
    47  	case *types.BoolValue:
    48  		src := src.(*types.BoolValue)
    49  		*dst = *src
    50  	case *types.Int64Value:
    51  		src := src.(*types.Int64Value)
    52  		*dst = *src
    53  	case CopierFrom:
    54  		dst.CopyFrom(src)
    55  	default:
    56  		panic(fmt.Sprintf("Copy for %T not implemented", dst))
    57  	}
    58  
    59  }