-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.go
118 lines (99 loc) · 2.09 KB
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package metago
import (
"bytes"
"fmt"
"io"
)
type Diff struct {
Chgs []Chg
}
func (d Diff) WriteIndented(w io.Writer, lev int) {
for i := 0; i < lev; i++ {
fmt.Fprintf(w, " ")
}
fmt.Fprintf(w, "Diff: \n")
lev++
for _, c := range d.Chgs {
c.WriteIndented(w, lev)
}
}
type Chg interface {
AttributeID() *AttrID
Schemaref() *Attrdef
WriteIndented(w io.Writer, lev int)
}
type BaseChg struct {
schemaref *Attrdef
}
func (d BaseChg) AttributeID() *AttrID {
return d.schemaref.ID
}
func (d BaseChg) Schemaref() *Attrdef {
return d.schemaref
}
func (d BaseChg) PersistenceClass() PersistenceClass {
return d.schemaref.Persistence
}
func (d BaseChg) WriteTo(w *Writer) error {
w.Write(d.schemaref.ID.Pkg[:])
w.WriteVarint(int64(d.schemaref.ID.Typ))
w.WriteVarint(int64(d.schemaref.ID.Attr))
return nil
}
//go:generate stringer -type=ChangeType
type ChangeType int
const (
ChangeTypeInsert ChangeType = iota
ChangeTypeDelete
ChangeTypeModify
)
type SliceChg struct {
BaseChg
Idx int
Typ ChangeType
Chgs []Chg
}
func (c SliceChg) WriteIndented(w io.Writer, lev int) {
for i := 0; i < lev; i++ {
fmt.Fprintf(w, " ")
}
fmt.Fprintf(w, "SliceChg (%s) -- %s -- Idx: %d\n", c.Typ, c.BaseChg.schemaref, c.Idx)
lev++
for _, c1 := range c.Chgs {
c1.WriteIndented(w, lev)
}
}
func NewSliceChg(sref *Attrdef, idx int, typ ChangeType, chgs []Chg) Chg {
return &SliceChg{BaseChg: BaseChg{schemaref: sref}, Idx: idx, Typ: typ, Chgs: chgs}
}
type StructChg struct {
BaseChg
Chg Diff
}
func (c StructChg) WriteIndented(w io.Writer, lev int) {
for i := 0; i < lev; i++ {
fmt.Fprintf(w, " ")
}
fmt.Fprintf(w, "StructChg: ")
c.Chg.WriteIndented(w, lev+1)
}
func NewStructChg(sref *Attrdef, chg Diff) Chg {
return &StructChg{BaseChg: BaseChg{schemaref: sref}, Chg: chg}
}
type DiffApplyError struct {
errChain []error
}
func (e DiffApplyError) Error() string {
buf := &bytes.Buffer{}
for i, err := range e.errChain {
if i != 0 {
fmt.Fprintln(buf, " Caused by:")
}
for j := 0; j < i; j++ {
fmt.Fprint(buf, " ")
}
fmt.Fprint(buf, err)
}
fmt.Fprintln(buf)
return buf.String()
}