Skip to content

Commit 1a01c6d

Browse files
Adde migration guidelines
1 parent 7b7a65a commit 1a01c6d

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

MIGRATE_V1_TO_V2.md

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# How to Migrate from Deque V1 to V2
2+
3+
After have downloaded Deque v2 version into your project (`go get -u github.com/ef-ds/deque/v2`), below code changes are required in order to migrate to v2.
4+
5+
- Update the imports path from `github.com/ef-ds/deque` to `deque github.com/ef-ds/deque/v2`
6+
- Update the Deque's declaration from `var d deque.Deque` to `var d deque.Deque[data_type]` and/or `d := deque.New()` to `d := deque.New[data_type]()`
7+
- Remove any `interface{}` type casts (i.e. `var v, _ := d.Pop(); typeCast := v.(data_type)`)
8+
9+
Below are examples of migrated code:
10+
11+
Simple int as data type - V1 version:
12+
```go
13+
import "github.com/ef-ds/deque"
14+
15+
var d deque.Deque
16+
for i := 1; i <= 5; i++ {
17+
d.PushBack(i)
18+
}
19+
20+
for d.Len() > 0 {
21+
v, _ := d.PopFront()
22+
intV := v.(int)
23+
fmt.Print(intV)
24+
}
25+
```
26+
27+
Simple int as data type - V2 version:
28+
```go
29+
import deque "github.com/ef-ds/deque/v2"
30+
31+
var d deque.Deque[int]
32+
for i := 1; i <= 5; i++ {
33+
d.PushBack(i)
34+
}
35+
36+
for d.Len() > 0 {
37+
intV, _ := d.PopFront()
38+
fmt.Print(intV)
39+
}
40+
```
41+
42+
Custom struct as data type - V1 version:
43+
```go
44+
import "github.com/ef-ds/deque"
45+
46+
type myType struct {
47+
value int
48+
}
49+
50+
d := deque.New()
51+
for i := 1; i <= 5; i++ {
52+
d.PushBack(&myType{value: i})
53+
}
54+
55+
for d.Len() > 0 {
56+
v, _ := d.PopFront()
57+
myTypeV := v.(*myType)
58+
fmt.Print(myTypeV.value)
59+
}
60+
```
61+
62+
Custom struct as data type - V2 version:
63+
```go
64+
import deque "github.com/ef-ds/deque/v2"
65+
66+
type myType struct {
67+
value int
68+
}
69+
70+
d := deque.New[*myType]()
71+
for i := 1; i <= 15; i++ {
72+
d.PushBack(&myType{value: i})
73+
}
74+
75+
for d.Len() > 0 {
76+
v, _ := d.PopFront()
77+
fmt.Print(v.value)
78+
}
79+
```
80+
81+
Mixed data types used as data type - V1 version:
82+
```go
83+
import "github.com/ef-ds/deque"
84+
85+
d := deque.New()
86+
for i := 1; i <= 5; i++ {
87+
if i%2 == 0 {
88+
// Push value as int
89+
d.PushBack(i)
90+
} else {
91+
// Push value as string
92+
d.PushBack(strconv.Itoa(i))
93+
}
94+
}
95+
96+
for d.Len() > 0 {
97+
v, _ := d.PopFront()
98+
switch t := v.(type) {
99+
case int:
100+
fmt.Printf("Int value: %d", t)
101+
case string:
102+
fmt.Printf("String value: %s", t)
103+
default:
104+
fmt.Print("Unrecognized type")
105+
}
106+
}
107+
```
108+
109+
Mixed data types used as data type - V2 version:
110+
```go
111+
import deque "github.com/ef-ds/deque/v2"
112+
113+
d := deque.New[interface{}]()
114+
for i := 1; i <= 5; i++ {
115+
if i%2 == 0 {
116+
// Push value as int
117+
d.PushBack(i)
118+
} else {
119+
// Push value as string
120+
d.PushBack(strconv.Itoa(i))
121+
}
122+
}
123+
124+
for d.Len() > 0 {
125+
v, _ := d.PopFront()
126+
switch t := v.(type) {
127+
case int:
128+
fmt.Printf("Int value: %d", t)
129+
case string:
130+
fmt.Printf("String value: %s", t)
131+
default:
132+
fmt.Print("Unrecognized type")
133+
}
134+
}
135+
```

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Also refer to the [integration](integration_test.go) and [API](api_test.go) test
8686

8787
Starting with v2.0.0, Deque supports generics. Looking for a previous non-generic stable release? Check out version [1.0.4](https://github.com/ef-ds/deque/releases/tag/v1.0.4).
8888

89+
Looking to migrate from v1 to v2? Check [this](MIGRATE_V1_TO_V2.md) out!
90+
8991

9092
## Tests
9193
Besides having 100% code coverage, deque has an extensive set of [unit](unit_test.go), [integration](integration_test.go) and [API](api_test.go) tests covering all happy, sad and edge cases.

0 commit comments

Comments
 (0)