-
Notifications
You must be signed in to change notification settings - Fork 0
/
gob.go
116 lines (110 loc) · 3.04 KB
/
gob.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
// Copyright © 2011-12 Qtrac Ltd.
//
// This program or package and any associated files are licensed under the
// Apache License, Version 2.0 (the "License"); you may not use these files
// except in compliance with the License. You can get a copy of the License
// at: http://www.apache.org/licenses/LICENSE-2.0.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
// "bytes"
"encoding/gob"
"errors"
"fmt"
"io"
// "time"
)
/*
type GobInvoice struct {
Id int
CustomerId int
DepartmentId string
Raised int64
Due int64
Paid bool
Note string
Items []*Item
}
func (invoice *Invoice) GobEncode() ([]byte, error) {
gobInvoice := GobInvoice{
invoice.Id,
invoice.CustomerId,
invoice.DepartmentId,
invoice.Raised.Unix(),
invoice.Due.Unix(),
invoice.Paid,
invoice.Note,
invoice.Items,
}
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
err := encoder.Encode(gobInvoice)
return buffer.Bytes(), err
}
func (invoice *Invoice) GobDecode(data []byte) error {
var gobInvoice GobInvoice
buffer := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buffer)
if err := decoder.Decode(&gobInvoice); err != nil {
return err
}
raised := time.Unix(gobInvoice.Raised, 0)
due := time.Unix(gobInvoice.Due, 0)
*invoice = Invoice{
gobInvoice.Id,
gobInvoice.CustomerId,
gobInvoice.DepartmentId,
raised,
due,
gobInvoice.Paid,
gobInvoice.Note,
gobInvoice.Items,
}
return nil
}
*/
type GobMarshaler struct{}
func (GobMarshaler) MarshalInvoices(writer io.Writer,
invoices []*Invoice) error {
encoder := gob.NewEncoder(writer)
if err := encoder.Encode(magicNumber); err != nil {
return err
}
if err := encoder.Encode(fileVersion); err != nil {
return err
}
return encoder.Encode(invoices)
}
func (GobMarshaler) UnmarshalInvoices(reader io.Reader) ([]*Invoice,
error) {
decoder := gob.NewDecoder(reader)
var magic int
if err := decoder.Decode(&magic); err != nil {
return nil, err
}
if magic != magicNumber {
return nil, errors.New("cannot read non-invoices gob file")
}
var version int
if err := decoder.Decode(&version); err != nil {
return nil, err
}
if version > fileVersion {
return nil, fmt.Errorf("version %d is too new to read", version)
}
var invoices []*Invoice
if err := decoder.Decode(&invoices); err != nil {
return nil, err
}
if version < fileVersion {
if err := update(invoices); err != nil {
return nil, err
}
}
return invoices, nil
}