Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit eeffeb8

Browse files
author
Wojciech Jabłoński
committed
style changes
Signed-off-by: Wojciech Jabłoński <wj359634@students.mimuw.edu.pl>
1 parent f61f2da commit eeffeb8

4 files changed

Lines changed: 67 additions & 53 deletions

File tree

uast/diff/apply.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package diff
22

33
import (
44
"fmt"
5+
56
"gopkg.in/bblfsh/sdk.v2/uast/nodes"
67
)
78

9+
// Apply is a method that takes a tree (nodes.Node) and applies the current changelist to that
10+
// tree.
811
func (changelist Changelist) Apply(root nodes.Node) nodes.Node {
912
nodeDict := make(map[ID]nodes.Node)
1013
nodes.WalkPreOrder(root, func(node nodes.Node) bool {

uast/diff/changelist.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"gopkg.in/bblfsh/sdk.v2/uast/nodes"
55
)
66

7+
// Changelist is a list of changes, a result of tree difference. Applying all changes from a
8+
// changelist on a source tree will result in it being transformed into the destination tree.
79
type Changelist []Change
810

11+
// Change is a single operation performed
912
type Change interface {
1013
isChange()
1114
TransactionID() uint64
@@ -18,12 +21,16 @@ type changeBase struct {
1821
func (changeBase) isChange() {}
1922
func (ch changeBase) TransactionID() uint64 { return ch.txID }
2023

24+
// ID is a type representing node unique ID that can be compared in O(1)
2125
type ID nodes.Comparable
2226

23-
// key in a node, string for nodes.Object and int for nodes.Array
27+
// Key in a node, string for nodes.Object and int for nodes.Array
2428
type Key interface{ isKey() }
2529

30+
// String is a wrapped string type for the Key interface.
2631
type String string
32+
33+
// Int is a wrapped int type for the Key interface.
2734
type Int int
2835

2936
func (Int) isKey() {}
@@ -37,24 +44,24 @@ type Create struct {
3744
Node nodes.Node
3845
}
3946

40-
// delete a node by ID
47+
// Delete a node by ID
4148
type Delete struct {
4249
changeBase
4350
NodeID ID
4451
}
4552

46-
// attach a node as a child of another node with a given key
53+
// Attach a node as a child of another node with a given key
4754
type Attach struct {
4855
changeBase
4956
Parent ID
5057
Key Key
5158
Child ID
5259
}
5360

54-
// deatach a child from a node
61+
// Deatach a child from a node
5562
type Deatach struct {
5663
changeBase
5764
Parent ID
58-
Key Key // Currently deatach semantics are only defined for nodes.Object so the Key is
59-
// practically always a string
65+
Key Key // Currently deatach semantics are only defined for nodes.Object so the Key is
66+
// practically always a string
6067
}

uast/diff/changelist_test.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
11
package diff
22

33
import (
4-
"strings"
5-
"os"
6-
"path/filepath"
7-
"io/ioutil"
8-
"testing"
9-
"gopkg.in/bblfsh/sdk.v2/uast/yaml"
10-
"gopkg.in/bblfsh/sdk.v2/uast/nodes"
11-
"github.com/stretchr/testify/require"
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
"gopkg.in/bblfsh/sdk.v2/uast/nodes"
12+
uastyml "gopkg.in/bblfsh/sdk.v2/uast/yaml"
1213
)
1314

14-
1515
const dataDir = "./testdata"
1616

1717
func readUAST(t testing.TB, path string) nodes.Node {
18-
data, err := ioutil.ReadFile(path)
19-
require.NoError(t, err)
20-
nd, err := uastyml.Unmarshal(data)
21-
require.NoError(t, err)
22-
return nd
18+
data, err := ioutil.ReadFile(path)
19+
require.NoError(t, err)
20+
nd, err := uastyml.Unmarshal(data)
21+
require.NoError(t, err)
22+
return nd
2323
}
2424

2525
func TestChangelist(t *testing.T) {
26-
dir, err := os.Open(dataDir)
27-
require.NoError(t, err)
28-
defer dir.Close()
29-
names, err := dir.Readdirnames(-1)
30-
require.NoError(t, err)
31-
32-
for _, fname := range names {
33-
if strings.HasSuffix(fname, "_src.uast") {
34-
name := fname[:len(fname)-len("_src.uast")]
35-
36-
t.Run(name, func(t *testing.T) {
37-
src_name := filepath.Join(dataDir, name + "_src.uast")
38-
dst_name := filepath.Join(dataDir, name + "_dst.uast")
39-
src := readUAST(t, src_name)
40-
dst := readUAST(t, dst_name)
41-
42-
changes := Changes(src, dst)
43-
newsrc := changes.Apply(src)
44-
require.True(t, nodes.Equal(newsrc, dst))
45-
})
46-
}
47-
}
26+
dir, err := os.Open(dataDir)
27+
require.NoError(t, err)
28+
defer dir.Close()
29+
names, err := dir.Readdirnames(-1)
30+
require.NoError(t, err)
31+
32+
for _, fname := range names {
33+
if strings.HasSuffix(fname, "_src.uast") {
34+
name := fname[:len(fname)-len("_src.uast")]
35+
36+
t.Run(name, func(t *testing.T) {
37+
srcName := filepath.Join(dataDir, name+"_src.uast")
38+
dstName := filepath.Join(dataDir, name+"_dst.uast")
39+
src := readUAST(t, srcName)
40+
dst := readUAST(t, dstName)
41+
42+
changes := Changes(src, dst)
43+
newsrc := changes.Apply(src)
44+
require.True(t, nodes.Equal(newsrc, dst))
45+
})
46+
}
47+
}
4848
}

uast/diff/diff.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package diff
22

33
import (
44
"fmt"
5+
56
"github.com/heetch/lapjv"
67
"gopkg.in/bblfsh/sdk.v2/uast/nodes"
78
)
@@ -19,18 +20,17 @@ type permuteDecision struct {
1920
permutation []int
2021
}
2122

22-
func (d sameDecision) cost() int { return d.privateCost }
23-
func (d replaceDecision) cost() int { return d.privateCost }
24-
func (d matchDecision) cost() int { return d.privateCost }
25-
func (d permuteDecision) cost() int { return d.privateCost }
23+
func (d sameDecision) cost() int { return d.privateCost }
24+
func (d replaceDecision) cost() int { return d.privateCost }
25+
func (d matchDecision) cost() int { return d.privateCost }
26+
func (d permuteDecision) cost() int { return d.privateCost }
2627

2728
// min is a convinience method for choosing the cheapest decision
2829
func min(self, candidate decisionType) decisionType {
2930
if self.cost() > candidate.cost() {
3031
return candidate
31-
} else {
32-
return self
3332
}
33+
return self
3434
}
3535

3636
// type for cache
@@ -39,14 +39,14 @@ type keyType struct{ k1, k2 ID }
3939
// cache for diff computation
4040
type cacheStorage struct {
4141
decisions map[keyType]decisionType
42-
counts map[ID]int
42+
counts map[ID]int
4343
changes Changelist
4444
}
4545

4646
func makeCacheStorage() *cacheStorage {
4747
return &cacheStorage{
4848
decisions: make(map[keyType]decisionType),
49-
counts: make(map[ID]int),
49+
counts: make(map[ID]int),
5050
}
5151
}
5252

@@ -221,12 +221,12 @@ func (ds *cacheStorage) generateDifference(src, dst nodes.Node, parentID ID, par
221221
keys[key] = true
222222
if _, ok := dst[key]; !ok {
223223
ds.push(Deatach{Parent: nodes.UniqueKey(src), Key: String(key)})
224-
} else if _, ok := src[key]; !ok {
224+
} else if _, ok := src[key]; !ok {
225225
ds.createRec(dst[key])
226226
ds.push(Attach{
227227
Parent: nodes.UniqueKey(src),
228-
Key: String(key),
229-
Child: nodes.UniqueKey(dst[key]),
228+
Key: String(key),
229+
Child: nodes.UniqueKey(dst[key]),
230230
})
231231
} else {
232232
ds.generateDifference(
@@ -273,11 +273,15 @@ func (ds *cacheStorage) generateDifference(src, dst nodes.Node, parentID ID, par
273273
}
274274
}
275275

276+
// Cost is a function that takes two trees: src and dst and returns number of operations needed to
277+
// convert the former into the latter.
276278
func Cost(src, dst nodes.Node) int {
277279
ds := makeCacheStorage()
278280
return ds.decideAction(src, dst).cost()
279281
}
280282

283+
// Changes is a function that takes two trees: src and dst and returns a changelist containing
284+
// all operations required to convert the former into the latter.
281285
func Changes(src, dst nodes.Node) Changelist {
282286
ds := makeCacheStorage()
283287
ds.generateDifference(src, dst, nil, Int(0))

0 commit comments

Comments
 (0)