deep-copy

command module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2024 License: BSD-3-Clause Imports: 9 Imported by: 0

README

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

```go

package main

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

// @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
	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

Documentation

Overview

deep-copy is a tool for generating DeepCopy() functions for a given type.

Given a package directory, and a type name that appears in that package, a DeepCopy method will be generated, to create a deep copy of the type value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL