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

Commit 025d8bb

Browse files
author
Wojciech Jabłoński
committed
style fixes
1 parent 3ec26f1 commit 025d8bb

302 files changed

Lines changed: 52 additions & 62 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

uast/diff/apply.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"gopkg.in/bblfsh/sdk.v2/uast/nodes"
66
)
77

8-
func Apply(root nodes.Node, changelist Changelist) nodes.Node {
8+
func (changelist Changelist) Apply(root nodes.Node) nodes.Node {
99
nodeDict := make(map[ID]nodes.Node)
1010
nodes.WalkPreOrder(root, func(node nodes.Node) bool {
1111
nodeDict[nodes.UniqueKey(node)] = node
@@ -15,11 +15,11 @@ func Apply(root nodes.Node, changelist Changelist) nodes.Node {
1515
for _, change := range changelist {
1616
switch ch := change.(type) {
1717
case Create:
18-
//create a node and add to the dictionary
18+
// create a node and add to the dictionary
1919
nodeDict[nodes.UniqueKey(ch.Node)] = ch.Node
2020

2121
case Attach:
22-
//get src and chld from the dictionary, attach (modify src)
22+
// get src and chld from the dictionary, attach (modify src)
2323
parent, ok := nodeDict[ch.Parent]
2424
if !ok {
2525
panic("invalid attachment point")
@@ -44,7 +44,7 @@ func Apply(root nodes.Node, changelist Changelist) nodes.Node {
4444
}
4545

4646
case Deatach:
47-
//get the src from the dictionary, deatach (modify src)
47+
// get the src from the dictionary, deatach (modify src)
4848
parent := nodeDict[ch.Parent]
4949

5050
switch key := ch.Key.(type) {

uast/diff/changelist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ type changeBase struct {
1818
func (changeBase) isChange() {}
1919
func (ch changeBase) TransactionID() uint64 { return ch.txID }
2020

21-
// TODO: proper ID of a node somehow
2221
type ID nodes.Comparable
2322

2423
// key in a node, string for nodes.Object and int for nodes.Array
@@ -56,5 +55,6 @@ type Attach struct {
5655
type Deatach struct {
5756
changeBase
5857
Parent ID
59-
Key Key //or string, how to do alternative?
58+
Key Key // Currently deatach semantics are only defined for nodes.Object so the Key is
59+
// practically always a string
6060
}

uast/diff/changelist_test.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package diff
22

33
import (
4-
"fmt"
4+
"strings"
55
"os"
6+
"path/filepath"
67
"io/ioutil"
78
"testing"
89
"gopkg.in/bblfsh/sdk.v2/uast/yaml"
@@ -22,23 +23,26 @@ func readUAST(t testing.TB, path string) nodes.Node {
2223
}
2324

2425
func TestChangelist(t *testing.T) {
25-
fd, err := os.Open(fmt.Sprintf("%v/config.txt", dataDir))
26+
dir, err := os.Open(dataDir)
2627
require.NoError(t, err)
27-
var n int
28-
_, err = fmt.Fscanf(fd, "%d\n", &n)
28+
defer dir.Close()
29+
names, err := dir.Readdirnames(-1)
2930
require.NoError(t, err)
3031

31-
for i := 0; i < n; i++ {
32-
name := fmt.Sprintf("%v/testcase_%v", dataDir, i)
33-
t.Run(name, func(t *testing.T) {
34-
src_name := fmt.Sprintf("%v_src.uast", name)
35-
dst_name := fmt.Sprintf("%v_dst.uast", name)
36-
src := readUAST(t, src_name)
37-
dst := readUAST(t, dst_name)
38-
39-
changes := Changes(src, dst)
40-
newsrc := Apply(src, changes)
41-
require.True(t, nodes.Equal(newsrc, dst))
42-
})
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+
}
4347
}
4448
}

uast/diff/diff.go

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type permuteDecision struct {
2525
permutation []int
2626
}
2727

28-
//min is a convinience method for choosing the cheapest decision
28+
// min is a convinience method for choosing the cheapest decision
2929
func min(self, candidate decisionType) decisionType {
3030
if self.cost() > candidate.cost() {
3131
return candidate
@@ -34,10 +34,10 @@ func min(self, candidate decisionType) decisionType {
3434
}
3535
}
3636

37-
//type for cache
37+
// type for cache
3838
type keyType struct{ k1, k2 ID }
3939

40-
//cache for diff computation
40+
// cache for diff computation
4141
type cacheStorage struct {
4242
decisions map[keyType]decisionType
4343
counts map[ID]int
@@ -104,16 +104,12 @@ func (ds *cacheStorage) decideAction(src, dst nodes.Node) decisionType {
104104
// the code below iterates over each of the keys from src and dst exactly once, that's why
105105
// keys isn't reset between iterations.
106106
keys := make(map[string]bool)
107-
iterate := func(keyset nodes.Object) {
108-
for key := range keyset {
109-
if in := keys[key]; !in {
110-
keys[key] = true
111-
cost += ds.decideAction(src[key], dst[key]).cost()
112-
}
107+
for _, key := range append(src.Keys(), dst.Keys()...) {
108+
if in := keys[key]; !in {
109+
keys[key] = true
110+
cost += ds.decideAction(src[key], dst[key]).cost()
113111
}
114112
}
115-
iterate(src)
116-
iterate(dst)
117113

118114
if cost == 0 {
119115
bestDecision = min(bestDecision, sameDecision{basicDecision{cost}})
@@ -133,9 +129,7 @@ func (ds *cacheStorage) decideAction(src, dst nodes.Node) decisionType {
133129
bestDecision = min(bestDecision, sameDecision{basicDecision{cost}})
134130
break
135131
}
136-
}
137-
138-
if len(src) != len(dst) {
132+
} else {
139133
cost = 2
140134
}
141135

@@ -223,28 +217,24 @@ func (ds *cacheStorage) generateDifference(src, dst nodes.Node, parentID ID, par
223217
case matchDecision:
224218
src, dst := src.(nodes.Object), dst.(nodes.Object)
225219
keys := make(map[string]bool)
226-
iterate := func(keyset nodes.Object) {
227-
for key := range keyset {
228-
if in := keys[key]; !in {
229-
keys[key] = true
230-
if _, ok := dst[key]; !ok {
231-
ds.push(Deatach{Parent: nodes.UniqueKey(src), Key: String(key)})
232-
} else if _, ok := src[key]; !ok {
233-
ds.createRec(dst[key])
234-
ds.push(Attach{
235-
Parent: nodes.UniqueKey(src),
236-
Key: String(key),
237-
Child: nodes.UniqueKey(dst[key]),
238-
})
239-
} else {
240-
ds.generateDifference(
241-
src[key], dst[key], nodes.UniqueKey(src), String(key))
242-
}
220+
for _, key := range append(src.Keys(), dst.Keys()...) {
221+
if in := keys[key]; !in {
222+
keys[key] = true
223+
if _, ok := dst[key]; !ok {
224+
ds.push(Deatach{Parent: nodes.UniqueKey(src), Key: String(key)})
225+
} else if _, ok := src[key]; !ok {
226+
ds.createRec(dst[key])
227+
ds.push(Attach{
228+
Parent: nodes.UniqueKey(src),
229+
Key: String(key),
230+
Child: nodes.UniqueKey(dst[key]),
231+
})
232+
} else {
233+
ds.generateDifference(
234+
src[key], dst[key], nodes.UniqueKey(src), String(key))
243235
}
244236
}
245237
}
246-
iterate(src)
247-
iterate(dst)
248238

249239
case permuteDecision:
250240
src, dst := src.(nodes.Array), dst.(nodes.Array)

0 commit comments

Comments
 (0)