Giter Site home page Giter Site logo

go-cpy's Introduction

Package for copying Go values

This package provides a generic way of deep copying Go objects. It is designed with performance in mind and is suitable for production use.

This is not an official Google product.

WARNING: This package's API is currently unstable and may change without warning. If this matters to you, you should wait until version 1.0 is released before using it.

Install

go get -u github.com/google/go-cpy/cpy

go-cpy's People

Contributors

neild avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

go-cpy's Issues

Document requirement for IgnoreAllUnexported

cpy.New currently requires that the user pass cpy.IgnoreAllUnexported. Document this requirement, or change it.

We require that the user specify this option because it isn't clear what the correct default handling of unexported fields is. (See TODO in the code detailing various possibilities.) If we're confident that there is no natural default and the user will always be required to pick a behavior, perhaps we should promote this option to the function signature:

type Mode int

const (
  IgnoreAllUnexported Mode = iota
)

func New(mode Mode, opts ...Option) *Machine { ... }

Decide on use of type parameters

We could use type parameters in the cpy API in a few places.

func Func[T any](fn func(T) T) Option { ... }

Func could be parameterized on the copied type, ensuring that the copy function has the right number of parameters and that the input and output match. However, we cannot fully express the constraints on T using 1.18 type parameters: I do not believe there is any way to indicate that T may be an interface type but may not be a channel.

func Shallow[T any]() Option { ... }

Shallow could also be type parameterized, passing the type to shallow copy rather than a prototype value of that type. The limitations on expressing the constraints on T still apply.

Unlike Func, we cannot infer a type parameter for a call to Shallow, so we would need to provide functions with different names for pre- and post- generics versions of Go.

func Copy[T any](c *Copier, v T) T { ... }

We cannot parameterize the Copier.Copy method, but we could provide a Copy function that avoids the need to use a type assertion to recover the type of the new value.

Unlike the changes to Func and Shallow, this does arguably make the package API simpler to use:

b := copier.Copy(a).(T)  // need to type assert back to the type of a
b := cpy.Copy(copier, a) // no type assertion needed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.