-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmus-dvs.go
77 lines (70 loc) · 1.98 KB
/
mus-dvs.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
package dvs
import (
com "github.com/mus-format/common-go"
dts "github.com/mus-format/mus-dts-go"
)
// New creates a new DVS.
func New[V any](reg com.Registry) DVS[V] {
return DVS[V]{reg}
}
// DVS provides versioning support for the mus-go serializer.
type DVS[V any] struct {
reg com.Registry
}
// MakeBSAndMarshal makes bs, migrates v to the version specified by dtm, and
// then marshals dtm + resulting v version.
//
// In addition to the created byte slice and the number of used bytes, it can
// also return ErrUnknownDTM or ErrWrongTypeVersion.
func (dvs DVS[V]) MakeBSAndMarshal(dtm com.DTM, v V) (bs []byte, n int,
err error) {
mver, err := dvs.getMV(dtm)
if err != nil {
return
}
return mver.MigrateCurrentAndMakeBSAndMarshal(v)
}
// ReliablyMarshal migrates v to the version specified by dtm, and then reliably
// (if bs is too small creates a new one) marshals dtm + resulting v version.
//
// In addition to the received or created byte slice and the number of
// used bytes, it can also return ErrUnknownDTM or ErrWrongTypeVersion.
func (dvs DVS[V]) ReliablyMarshal(dtm com.DTM, v V, bs []byte) (abs []byte,
n int, err error) {
mver, err := dvs.getMV(dtm)
if err != nil {
return
}
return mver.MigrateCurrentAndReliablyMarshal(v, bs)
}
// Unmarshal unmarshals dtm + data, and then migrates data to the version
// specified by dtm.
//
// In addition to dtm and migrated dataand the number of used bytes, it can
// also return ErrUnknownDTM or ErrWrongTypeVersion.
func (dvs DVS[V]) Unmarshal(bs []byte) (dtm com.DTM, v V, n int, err error) {
dtm, n, err = dts.UnmarshalDTM(bs)
if err != nil {
return
}
mver, err := dvs.getMV(dtm)
if err != nil {
return
}
var n1 int
v, n1, err = mver.UnmarshalAndMigrateOld(bs[n:])
n += n1
return
}
func (dvs DVS[V]) getMV(dtm com.DTM) (mver MigrationVersion[V], err error) {
tver, err := dvs.reg.Get(dtm)
if err != nil {
return
}
mver, ok := tver.(MigrationVersion[V])
if !ok {
err = com.ErrWrongTypeVersion
return
}
return
}