Skip to content

Commit

Permalink
Move rename logic into separate library
Browse files Browse the repository at this point in the history
Use `github.com/gonvenience/idem` instead of code in `dyff`.
  • Loading branch information
HeavyWombat committed Feb 1, 2025
1 parent 372cae6 commit 5673bf0
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 742 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.0

require (
github.com/gonvenience/bunt v1.4.0
github.com/gonvenience/idem v0.0.1
github.com/gonvenience/neat v1.3.15
github.com/gonvenience/term v1.0.3
github.com/gonvenience/text v1.0.8
Expand All @@ -29,7 +30,7 @@ require (
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad // indirect
github.com/google/pprof v0.0.0-20250128161936-077ca0a936bf // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/gonvenience/bunt v1.4.0 h1:xRAANCgSmQwGoHIyWg80yFgomTiblBayUUSBBPjDHK4=
github.com/gonvenience/bunt v1.4.0/go.mod h1:J9S2b1ZmUKdvybPxhq0hhrIvAwxcUXJjerudNa2Fhdw=
github.com/gonvenience/idem v0.0.1 h1:SMqv4DI2ErBOsoohVyqBOeCCLrag6N7iClwJbmZBTYo=
github.com/gonvenience/idem v0.0.1/go.mod h1:Br/o1pWtrbiPTuZrn7nPV6TJQL0qbaoGNmJZ8+A7MyU=
github.com/gonvenience/neat v1.3.15 h1:qRMZzVP/HtLsQLKZGW8NGZIXdH1TMHsPjMJe2tvzDqk=
github.com/gonvenience/neat v1.3.15/go.mod h1:TMdu+WVzfRU46GvcMAYALdvlWxiUxP1yqpZn/hKUhP8=
github.com/gonvenience/term v1.0.3 h1:8MleXhMCCzLeWXmiEA/IQ/ZoaKBL5mHrmRVJsU1nWtI=
Expand All @@ -22,8 +24,8 @@ github.com/gonvenience/ytbx v1.4.6 h1:sXf0/kCBEAbrOBsj8aRpDvdRRkVl/3UZmNLKy4oFY+
github.com/gonvenience/ytbx v1.4.6/go.mod h1:LHhrtuB5ghXlU+l1NJJR3Wt1ZnpbQScqyshpXisYplE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad h1:a6HEuzUHeKH6hwfN/ZoQgRgVIWFJljSWa/zetS2WTvg=
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/google/pprof v0.0.0-20250128161936-077ca0a936bf h1:BvBLUD2hkvLI3dJTJMiopAq8/wp43AAZKTP7qdpptbU=
github.com/google/pprof v0.0.0-20250128161936-077ca0a936bf/go.mod h1:vavhavw2zAxS5dIdcRluK6cSGGPlZynqzFM8NdvU144=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
41 changes: 41 additions & 0 deletions pkg/dyff/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright © 2025 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package dyff

func mapItemsToSlice[E any, S ~[]E, T any](slice S, fn func(e E) T) []T {
ret := make([]T, len(slice))
for i, e := range slice {
ret[i] = fn(e)
}
return ret
}

func reject[E comparable, S ~[]E](slice S, elt E) (ret S, ok bool) {
ret = make(S, 0, len(slice))
for _, e := range slice {
if elt == e {
ok = true
} else {
ret = append(ret, e)
}
}
return
}
63 changes: 36 additions & 27 deletions pkg/dyff/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ package dyff

import (
"fmt"
"github.com/homeport/dyff/pkg/dyff/rename"
"sort"
"strings"

"github.com/gonvenience/bunt"
"github.com/gonvenience/idem"
"github.com/gonvenience/text"
"github.com/gonvenience/ytbx"

"github.com/mitchellh/hashstructure"
yamlv3 "gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -316,64 +317,72 @@ func (compare *compare) documentNodes(from, to ytbx.InputFile) ([]Diff, error) {
}
}

// Detect content names by heuristic method
detector := newDocumentChanges(
mapSlice(removals, func(d doc) *renameCandidate {
return &renameCandidate{
path: &ytbx.Path{Root: &from, DocumentIdx: d.idx},
doc: d.node,
}
candidateName := func(mappingNode *yamlv3.Node) string {
name, _ := k8sItem.Name(mappingNode)
return name
}

changes := idem.NewDocumentChanges(
mapItemsToSlice(removals, func(d doc) *idem.RenameCandidate {
return idem.NewRenameCandidate(
candidateName(d.node),
&ytbx.Path{Root: &from, DocumentIdx: d.idx},
d.node,
)
}),
mapSlice(additions, func(d doc) *renameCandidate {
return &renameCandidate{
path: &ytbx.Path{Root: &to, DocumentIdx: d.idx},
doc: d.node,
}
mapItemsToSlice(additions, func(d doc) *idem.RenameCandidate {
return idem.NewRenameCandidate(
candidateName(d.node),
&ytbx.Path{Root: &to, DocumentIdx: d.idx},
d.node,
)
}),
)
err = rename.DetectRenames(detector, nil)
if err != nil {

if err := idem.DetectRenames(changes, nil); err != nil {
return nil, err
}

// Push rename detection results
for _, modified := range detector.modifiedPairs {
for _, modified := range changes.ModifiedPairs() {
diffs, err := compare.objects(
*modified.to.path,
followAlias(modified.from.doc),
followAlias(modified.to.doc),
*modified.To.Path,
followAlias(modified.From.Doc),
followAlias(modified.To.Doc),
)
if err != nil {
return nil, err
}
result = append(result, diffs...)

// Exclude from order change calculation
fromNames, _ = reject(fromNames, modified.from.Name())
toNames, _ = reject(toNames, modified.to.Name())
fromNames, _ = reject(fromNames, modified.From.Name())
toNames, _ = reject(toNames, modified.To.Name())
}
for _, removal := range detector.deleted {

for _, removal := range changes.DeletedList {
result = append(result, Diff{
Path: removal.path,
Path: removal.Path,
Details: []Detail{{
Kind: REMOVAL,
From: &yamlv3.Node{
Kind: yamlv3.DocumentNode,
Content: []*yamlv3.Node{removal.doc},
Content: []*yamlv3.Node{removal.Doc},
},
To: nil,
}},
})
}
for _, addition := range detector.added {

for _, addition := range changes.AddedList {
result = append(result, Diff{
Path: addition.path,
Path: addition.Path,
Details: []Detail{{
Kind: ADDITION,
From: nil,
To: &yamlv3.Node{
Kind: yamlv3.DocumentNode,
Content: []*yamlv3.Node{addition.doc},
Content: []*yamlv3.Node{addition.Doc},
},
}},
})
Expand Down
Loading

0 comments on commit 5673bf0

Please sign in to comment.