GithubHelp home page GithubHelp logo

xsean2020 / deep-copy Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 18 KB

This tool generates deep copy methods for Go structs based on specific annotations

License: BSD 3-Clause "New" or "Revised" License

Go 100.00%

deep-copy's Introduction

DeepCopy Generator for Go Structs

This tool generates deep copy methods for Go structs based on specific annotations.

Installation

Install the DeepCopy generator tool using the following command:

go get -u github.com/xsean2020/deep-copy

Usage:

Use the deep-copy command to generate deep copy methods with custom annotations:

go run -mod=mod github.com/xsean2020/deep-copy --root .

Annotations

  • @Copyable: Marks whether to generate deep copy methods.
  • @PtrRecv: Specifies whether the generated method should accept a pointer receiver.
  • @Exportedonly=true: Set to true to generate methods only for exported fields.
  • @Name: Specify the generated function name.

The generated deep copy methods will respect the specified annotations.

Example

package main

// @copyable
// @exportedonly  false
type YourStruct struct {
	// Struct fields definition
	Field1 string
	Field2 int
	field3 int //  be exported
}

// @copyable
// @ptrrecv false
// @name clone
// @exportedonly  true
type AnotherStruct struct {
	// Struct fields definition
	S  *YourStruct
	S2 int `deepcopy:"-"` // no export
}

Run Command:

go run -mod=mod github.com/xsean2020/deep-copy --root .
  • command out put *_gen_deepcopy.go:
// Code generated by ../deep-copy .; DO NOT EDIT.

package main

// DeepCopy generates a deep copy of *YourStruct
func (o *YourStruct) DeepCopy() *YourStruct {

	if o == nil {
		return nil
	}
	var cp YourStruct
	var val = *o
	cp.Field1 = val.Field1
	cp.Field2 = val.Field2
	cp.field3 = val.field3
	return &cp
}

// clone generates a deep copy of AnotherStruct
func (o AnotherStruct) clone() AnotherStruct {

	var cp AnotherStruct
	if o.S != nil {
		cp.S = o.S.DeepCopy()
	}
	return cp
}

Contribution

Feedback and contributions are welcome. Please refer to the contribution guidelines for more information.

License

This project is licensed under the BSD 3-Clause License

deep-copy's People

Contributors

xsean2020 avatar

Watchers

 avatar

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.