Skip to content

Commit fd7c970

Browse files
authored
Add go docs for functions and types (#11)
1 parent f91fb04 commit fd7c970

9 files changed

+76
-7
lines changed

base_copier.go

+11
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ import (
44
"reflect"
55
)
66

7+
// copier base interface defines Copy function
78
type copier interface {
89
Copy(dst, src reflect.Value) error
910
}
1011

12+
// nopCopier no-op copier
1113
type nopCopier struct {
1214
}
1315

16+
// Copy implementation of Copy function for no-op copier
1417
func (c *nopCopier) Copy(dst, src reflect.Value) error {
1518
return nil
1619
}
1720

21+
// value2PtrCopier data structure of copier that copies from a value to a pointer
1822
type value2PtrCopier struct {
1923
ctx *Context
2024
copier copier
2125
}
2226

27+
// Copy implementation of Copy function for value-to-pointer copier
2328
func (c *value2PtrCopier) Copy(dst, src reflect.Value) error {
2429
if dst.IsNil() {
2530
dst.Set(reflect.New(dst.Type().Elem()))
@@ -33,11 +38,13 @@ func (c *value2PtrCopier) init(dstType, srcType reflect.Type) (err error) {
3338
return
3439
}
3540

41+
// ptr2ValueCopier data structure of copier that copies from a pointer to a value
3642
type ptr2ValueCopier struct {
3743
ctx *Context
3844
copier copier
3945
}
4046

47+
// Copy implementation of Copy function for pointer-to-value copier
4148
func (c *ptr2ValueCopier) Copy(dst, src reflect.Value) error {
4249
src = src.Elem()
4350
if !src.IsValid() {
@@ -52,11 +59,13 @@ func (c *ptr2ValueCopier) init(dstType, srcType reflect.Type) (err error) {
5259
return
5360
}
5461

62+
// ptr2PtrCopier data structure of copier that copies from a pointer to a pointer
5563
type ptr2PtrCopier struct {
5664
ctx *Context
5765
copier copier
5866
}
5967

68+
// Copy implementation of Copy function for pointer-to-pointer copier
6069
func (c *ptr2PtrCopier) Copy(dst, src reflect.Value) error {
6170
src = src.Elem()
6271
if !src.IsValid() {
@@ -75,6 +84,7 @@ func (c *ptr2PtrCopier) init(dstType, srcType reflect.Type) (err error) {
7584
return
7685
}
7786

87+
// directCopier copier that does copying by assigning `src` value to `dst` directly
7888
type directCopier struct {
7989
}
8090

@@ -83,6 +93,7 @@ func (c *directCopier) Copy(dst, src reflect.Value) error {
8393
return nil
8494
}
8595

96+
// convCopier copier that does copying with converting `src` value to `dst` type
8697
type convCopier struct {
8798
}
8899

build_copier.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync"
77
)
88

9+
// cacheKey key data structure of cached copiers
910
type cacheKey struct {
1011
dstType reflect.Type
1112
srcType reflect.Type
@@ -45,11 +46,15 @@ var (
4546
)
4647

4748
const (
48-
flagCopyBetweenPtrAndValue = 1
49+
// flagCopyBetweenPtrAndValue indicates copying will be performed between `pointers` and `values`
50+
flagCopyBetweenPtrAndValue = 1
51+
// flagCopyBetweenStructFieldAndMethod indicates copying will be performed between `struct fields` and `functions`
4952
flagCopyBetweenStructFieldAndMethod = 2
50-
flagIgnoreNonCopyableTypes = 3
53+
// flagIgnoreNonCopyableTypes indicates copying will skip copying non-copyable types without raising errors
54+
flagIgnoreNonCopyableTypes = 3
5155
)
5256

57+
// prepare prepares context for copiers
5358
func (ctx *Context) prepare() {
5459
if ctx.UseGlobalCache {
5560
ctx.copierCacheMap = copierCacheMap
@@ -72,6 +77,7 @@ func (ctx *Context) prepare() {
7277
}
7378
}
7479

80+
// createCacheKey creates and returns key for caching a copier
7581
func (ctx *Context) createCacheKey(dstType, srcType reflect.Type) *cacheKey {
7682
return &cacheKey{
7783
dstType: dstType,
@@ -80,6 +86,7 @@ func (ctx *Context) createCacheKey(dstType, srcType reflect.Type) *cacheKey {
8086
}
8187
}
8288

89+
// defaultContext creates a default context
8390
func defaultContext() *Context {
8491
return &Context{
8592
CopyBetweenPtrAndValue: true,

deepcopy.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import (
77
)
88

99
const (
10+
// DefaultTagName default tag name for the program to parse input struct tags
11+
// to build copier configuration.
1012
DefaultTagName = "copy"
1113
)
1214

15+
// Context copier context
1316
type Context struct {
1417
// CopyBetweenPtrAndValue allow or not copying between pointers and values (default is `true`)
1518
CopyBetweenPtrAndValue bool
@@ -29,30 +32,39 @@ type Context struct {
2932
flags uint8
3033
}
3134

35+
// Option configuration option function provided as extra arguments of copying function
3236
type Option func(ctx *Context)
3337

38+
// CopyBetweenPtrAndValue config function for setting flag `CopyBetweenPtrAndValue`
3439
func CopyBetweenPtrAndValue(flag bool) Option {
3540
return func(ctx *Context) {
3641
ctx.CopyBetweenPtrAndValue = flag
3742
}
3843
}
44+
45+
// CopyBetweenStructFieldAndMethod config function for setting flag `CopyBetweenStructFieldAndMethod`
3946
func CopyBetweenStructFieldAndMethod(flag bool) Option {
4047
return func(ctx *Context) {
4148
ctx.CopyBetweenStructFieldAndMethod = flag
4249
}
4350
}
51+
52+
// IgnoreNonCopyableTypes config function for setting flag `IgnoreNonCopyableTypes`
4453
func IgnoreNonCopyableTypes(flag bool) Option {
4554
return func(ctx *Context) {
4655
ctx.IgnoreNonCopyableTypes = flag
4756
}
4857
}
58+
59+
// UseGlobalCache config function for setting flag `UseGlobalCache`
4960
func UseGlobalCache(flag bool) Option {
5061
return func(ctx *Context) {
5162
ctx.UseGlobalCache = flag
5263
}
5364
}
5465

5566
// Copy performs deep copy from `src` to `dst`.
67+
//
5668
// `dst` must be a pointer to the output var, `src` can be either value or pointer.
5769
// In case you want to copy unexported struct fields within `src`, `src` must be a pointer.
5870
func Copy(dst, src any, options ...Option) (err error) {
@@ -98,7 +110,7 @@ func Copy(dst, src any, options ...Option) (err error) {
98110
return cp.Copy(dstVal, srcVal)
99111
}
100112

101-
// ClearCache clears global cache
113+
// ClearCache clears global cache of previously used copiers
102114
func ClearCache() {
103115
mu.Lock()
104116
copierCacheMap = map[cacheKey]copier{}

errors.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ import (
44
"errors"
55
)
66

7+
// Errors may be returned from Copy function
78
var (
8-
ErrTypeInvalid = errors.New("ErrTypeInvalid")
9-
ErrTypeNonCopyable = errors.New("ErrTypeNonCopyable")
10-
ErrValueInvalid = errors.New("ErrValueInvalid")
11-
ErrValueUnaddressable = errors.New("ErrValueUnaddressable")
9+
// ErrTypeInvalid returned when type of input var does not meet the requirement
10+
ErrTypeInvalid = errors.New("ErrTypeInvalid")
11+
// ErrTypeNonCopyable returned when the function can not perform copying between types
12+
ErrTypeNonCopyable = errors.New("ErrTypeNonCopyable")
13+
// ErrValueInvalid returned when input value does not meet the requirement
14+
ErrValueInvalid = errors.New("ErrValueInvalid")
15+
// ErrValueUnaddressable returned when value is `unaddressable` which is required
16+
// in some situations such as when accessing an unexported struct field.
17+
ErrValueUnaddressable = errors.New("ErrValueUnaddressable")
18+
// ErrFieldRequireCopying returned when a field is required to be copied
19+
// but no copying is done for it.
1220
ErrFieldRequireCopying = errors.New("ErrFieldRequireCopying")
1321
)

iface_copier.go

+4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"reflect"
55
)
66

7+
// fromIfaceCopier data structure of copier that copies from an interface
78
type fromIfaceCopier struct {
89
ctx *Context
910
}
1011

12+
// Copy implementation of Copy function for from-iface copier
1113
func (c *fromIfaceCopier) Copy(dst, src reflect.Value) error {
1214
for src.Kind() == reflect.Interface {
1315
src = src.Elem()
@@ -23,10 +25,12 @@ func (c *fromIfaceCopier) Copy(dst, src reflect.Value) error {
2325
return cp.Copy(dst, src)
2426
}
2527

28+
// toIfaceCopier data structure of copier that copies to an interface
2629
type toIfaceCopier struct {
2730
ctx *Context
2831
}
2932

33+
// Copy implementation of Copy function for to-iface copier
3034
func (c *toIfaceCopier) Copy(dst, src reflect.Value) error {
3135
for src.Kind() == reflect.Interface {
3236
src = src.Elem()

map_copier.go

+4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"reflect"
55
)
66

7+
// mapCopier data structure of copier that copies from a `map`
78
type mapCopier struct {
89
ctx *Context
910
keyCopier *mapItemCopier
1011
valueCopier *mapItemCopier
1112
}
1213

14+
// Copy implementation of Copy function for map copier
1315
func (c *mapCopier) Copy(dst, src reflect.Value) (err error) {
1416
if src.IsNil() {
1517
dst.Set(reflect.Zero(dst.Type())) // TODO: Go1.18 has no SetZero
@@ -81,11 +83,13 @@ func (c *mapCopier) init(dstType, srcType reflect.Type) error {
8183
return nil
8284
}
8385

86+
// mapItemCopier data structure of copier that copies from a map's key or value
8487
type mapItemCopier struct {
8588
dstType reflect.Type
8689
copier copier
8790
}
8891

92+
// Copy implementation of Copy function for map item copier
8993
func (c *mapItemCopier) Copy(src reflect.Value) (reflect.Value, error) {
9094
dst := reflect.New(c.dstType).Elem()
9195
err := c.copier.Copy(dst, src)

slice_copier.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"reflect"
55
)
66

7+
// sliceCopier data structure of copier that copies from a `slice`
78
type sliceCopier struct {
89
ctx *Context
910
itemCopier copier
1011
}
1112

13+
// Copy implementation of Copy function for slice copier
1214
func (c *sliceCopier) Copy(dst, src reflect.Value) error {
1315
srcLen := src.Len()
1416
if dst.Kind() == reflect.Slice { // Slice/Array -> Slice
@@ -76,17 +78,22 @@ func (c *sliceCopier) init(dstType, srcType reflect.Type) (err error) {
7678
return
7779
}
7880

81+
// sliceItemDirectCopier copier that copies from a slice item to a destination value directly
7982
type sliceItemDirectCopier struct {
8083
}
8184

85+
// Copy implementation of Copy function for slice item copier direct
8286
func (c *sliceItemDirectCopier) Copy(dst, src reflect.Value) error {
8387
dst.Set(src)
8488
return nil
8589
}
8690

91+
// sliceItemConvCopier copier that copies from a slice item to a destination value
92+
// with converting `src` value to `dst` type
8793
type sliceItemConvCopier struct {
8894
}
8995

96+
// Copy implementation of Copy function for slice item copier with-conversion
9097
func (c *sliceItemConvCopier) Copy(dst, src reflect.Value) error {
9198
dst.Set(src.Convert(dst.Type()))
9299
return nil

struct_copier.go

+14
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ var (
1111
errType = reflect.TypeOf((*error)(nil)).Elem()
1212
)
1313

14+
// structCopier data structure of copier that copies from a `struct`
1415
type structCopier struct {
1516
ctx *Context
1617
fieldCopiers []copier
1718
}
1819

20+
// Copy implementation of Copy function for struct copier
1921
func (c *structCopier) Copy(dst, src reflect.Value) error {
2022
for _, cp := range c.fieldCopiers {
2123
if err := cp.Copy(dst, src); err != nil {
@@ -201,44 +203,54 @@ func (c *structCopier) createCustomCopier(df, sf *reflect.StructField, cp copier
201203
}
202204
}
203205

206+
// structFieldDirectCopier data structure of copier that copies from
207+
// a src field to a dst field directly
204208
type structFieldDirectCopier struct {
205209
dstField int
206210
srcField int
207211
}
208212

213+
// Copy implementation of Copy function for struct field copier direct
209214
func (c *structFieldDirectCopier) Copy(dst, src reflect.Value) error {
210215
dst.Field(c.dstField).Set(src.Field(c.srcField))
211216
return nil
212217
}
213218

219+
// structFieldConvCopier data structure of copier that copies from
220+
// a src field to a dst field with type conversion
214221
type structFieldConvCopier struct {
215222
dstField int
216223
srcField int
217224
}
218225

226+
// Copy implementation of Copy function for struct field copier with type conversion
219227
func (c *structFieldConvCopier) Copy(dst, src reflect.Value) error {
220228
dstVal := dst.Field(c.dstField)
221229
dstVal.Set(src.Field(c.srcField).Convert(dstVal.Type()))
222230
return nil
223231
}
224232

233+
// structFieldCopier wrapping copier for copying struct field
225234
type structFieldCopier struct {
226235
copier copier
227236
dstField int
228237
srcField int
229238
}
230239

240+
// Copy implementation of Copy function for struct field copier
231241
func (c *structFieldCopier) Copy(dst, src reflect.Value) error {
232242
return c.copier.Copy(dst.Field(c.dstField), src.Field(c.srcField))
233243
}
234244

245+
// structFieldMethodCopier data structure of copier that copies between `fields` and `methods`
235246
type structFieldMethodCopier struct {
236247
dstMethod int
237248
dstMethodUnexported bool
238249
srcField int
239250
srcFieldUnexported bool
240251
}
241252

253+
// Copy implementation of Copy function for struct field copier between `fields` and `methods`
242254
func (c *structFieldMethodCopier) Copy(dst, src reflect.Value) error {
243255
src = src.Field(c.srcField)
244256
if c.srcFieldUnexported {
@@ -263,6 +275,7 @@ func (c *structFieldMethodCopier) Copy(dst, src reflect.Value) error {
263275
return err
264276
}
265277

278+
// structUnexportedFieldCopier data structure of copier that copies between unexported fields of struct
266279
type structUnexportedFieldCopier struct {
267280
copier copier
268281
dstField int
@@ -271,6 +284,7 @@ type structUnexportedFieldCopier struct {
271284
srcFieldUnexported bool
272285
}
273286

287+
// Copy implementation of Copy function for struct unexported field copier
274288
func (c *structUnexportedFieldCopier) Copy(dst, src reflect.Value) error {
275289
src = src.Field(c.srcField)
276290
if c.srcFieldUnexported {

tag.go

+2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"strings"
66
)
77

8+
// fieldDetail stores field copying detail parsed from a struct field
89
type fieldDetail struct {
910
field *reflect.StructField
1011
key string
1112
ignored bool
1213
required bool
1314
}
1415

16+
// parseTag parses struct tag for getting copying detail and configuration
1517
func parseTag(detail *fieldDetail) {
1618
tagValue, ok := detail.field.Tag.Lookup(DefaultTagName)
1719
detail.key = detail.field.Name

0 commit comments

Comments
 (0)