Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
glassonion1 committed Aug 17, 2024
1 parent a8ac995 commit dad2095
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
)

// It is a common, ordinary struct
type FromModel struct {
ID string `copier:"Id"`
Name string
CreatedAt time.Time
UpdatedAt *time.Time
}
func ExampleDeepCopy_Struct() {
// It is a common, ordinary struct
type FromModel struct {
ID string `copier:"Id"`
Name string
CreatedAt time.Time
UpdatedAt *time.Time
}

// It is like a protobuf struct on gRPC
type ToModel struct {
Id string
Name string
CreatedAt *timestamppb.Timestamp
UpdatedAt *timestamppb.Timestamp
}
// It is like a protobuf struct on gRPC
type ToModel struct {
Id string
Name string
CreatedAt *timestamppb.Timestamp
UpdatedAt *timestamppb.Timestamp
}

func ExampleDeepCopy() {
now := time.Date(2020, 6, 1, 0, 0, 0, 0, time.UTC)
from := FromModel{
ID: "xxxx",
Expand All @@ -42,6 +42,35 @@ func ExampleDeepCopy() {
// Output: ToModel object: &{xxxx R2D2 seconds:1590969600 seconds:1590969600}
}

func ExampleDeepCopy_Slice() {
type FromModel struct {
ID string
Name string
}
type ToModel struct {
ID string
Name string
}
from := []FromModel{
{
ID: "xxxx1",
Name: "R2D2",
},
{
ID: "xxxx2",
Name: "C3PO",
},
}
to := &[]ToModel{}
err := xgo.DeepCopy(from, to)
if err != nil {
// handles error
}
fmt.Println("ToModel object:", to)

// Output: ToModel object: &[{xxxx1 R2D2} {xxxx2 C3PO}]
}

func ExampleContains() {
// slice of int32
containsInt32 := xgo.Contains[int32]([]int32{1, 2, 3, 4, 5}, 3)
Expand Down

0 comments on commit dad2095

Please sign in to comment.