Skip to content

Commit

Permalink
[ENHANCEMENT] efficient deepcopy
Browse files Browse the repository at this point in the history
Signed-off-by: Akshay Iyyadurai Balasundaram <[email protected]>
  • Loading branch information
ibakshay committed Nov 27, 2024
1 parent 728b4be commit 4e2fa72
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
21 changes: 13 additions & 8 deletions api/v1alpha1/perses_config.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package v1alpha1

import (
"github.com/barkimedes/go-deepcopy"
"fmt"

"github.com/brunoga/deep"
"github.com/perses/perses/pkg/model/api/config"
)

type PersesConfig struct {
config.Config `json:",inline"`
}

func (in *PersesConfig) DeepCopyInto(out *PersesConfig) {
temp, err := deepcopy.Anything(in)

if err != nil {
panic(err)
}

*out = *(temp.(*PersesConfig))
func (in *PersesConfig) DeepCopyInto(out *PersesConfig) {
if in == nil {
return
}

copied, err := deep.Copy(in)
if err != nil {
panic(fmt.Errorf("failed to deep copy PersesConfig: %w", err))
}
*out = *copied
}
25 changes: 15 additions & 10 deletions api/v1alpha1/perses_dashboard.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package v1alpha1

import (
"encoding/json"
"fmt"

"github.com/brunoga/deep"
persesv1 "github.com/perses/perses/pkg/model/api/v1"
)

type Dashboard struct {
persesv1.DashboardSpec `json:",inline"`
}

// DeepCopyInto is a manually implemented deep copy function and this is required because:
// 1. The embedded persesv1.DashboardSpec from the Perses project doesn't implement DeepCopyInto
// 2. controller-gen can't automatically generate DeepCopy methods for types it doesn't own


func (in *Dashboard) DeepCopyInto(out *Dashboard) {
*out = *in
// Create a deep copy of the embedded DashboardSpec
outSpec := persesv1.DashboardSpec{}
bytes, _ := json.Marshal(in.DashboardSpec)
_ = json.Unmarshal(bytes, &outSpec)
out.DashboardSpec = outSpec
if in == nil {
return
}

copied, err := deep.Copy(in)
if err != nil {
panic(fmt.Errorf("failed to deep copy Dashboard: %w", err))
}
*out = *copied
}


20 changes: 12 additions & 8 deletions api/v1alpha1/perses_datasource.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package v1alpha1

import (
"github.com/barkimedes/go-deepcopy"
"fmt"

"github.com/brunoga/deep"
persesv1 "github.com/perses/perses/pkg/model/api/v1"
)

Expand All @@ -10,11 +12,13 @@ type Datasource struct {
}

func (in *Datasource) DeepCopyInto(out *Datasource) {
temp, err := deepcopy.Anything(in)

if err != nil {
panic(err)
}

*out = *(temp.(*Datasource))
if in == nil {
return
}

copied, err := deep.Copy(in)
if err != nil {
panic(fmt.Errorf("failed to deep copy Datasource: %w", err))
}
*out = *copied
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/brunoga/deep v1.2.4 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df h1:GSoSVRLo
github.com/barkimedes/go-deepcopy v0.0.0-20220514131651-17c30cfc62df/go.mod h1:hiVxq5OP2bUGBRNS3Z/bt/reCLFNbdcST6gISi1fiOM=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/brunoga/deep v1.2.4 h1:Aj9E9oUbE+ccbyh35VC/NHlzzjfIVU69BXu2mt2LmL8=
github.com/brunoga/deep v1.2.4/go.mod h1:GDV6dnXqn80ezsLSZ5Wlv1PdKAWAO4L5PnKYtv2dgaI=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
2 changes: 1 addition & 1 deletion internal/perses/common/perses_client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (f *PersesClientFactoryWithURL) CreateClient(config persesv1alpha1.Perses)
return nil, err
}
restClient, err := clientConfig.NewRESTClient(clientConfig.RestConfigClient{
URL: &common.URL{URL: parsedURL.URL},
URL: parsedURL,
})

if err != nil {
Expand Down

0 comments on commit 4e2fa72

Please sign in to comment.