This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathserializer.go
116 lines (95 loc) · 3.2 KB
/
serializer.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
package orient
import (
"bytes"
"fmt"
"gopkg.in/istreamdata/orientgo.v2/obinary/rw"
"io"
)
// ErrTypeSerialization represent serialization/deserialization error
type ErrTypeSerialization struct {
Val interface{}
Serializer interface{}
}
func (e ErrTypeSerialization) Error() string {
return fmt.Sprintf("Serializer (%T)%v has no support for type %T", e.Serializer, e.Serializer, e.Val)
}
// CustomSerializable is an interface for objects that can be sent on wire
type CustomSerializable interface {
Classer
Serializable
}
// Classer is an interface for object that have analogs in OrientDB Java code
type Classer interface {
// GetClassName return a Java class name for an object
GetClassName() string
}
var (
recordFormats = map[string]func() RecordSerializer{
binaryFormatName: func() RecordSerializer { return &BinaryRecordFormat{} },
}
recordFormatDefault = binaryFormatName
)
// Serializable is an interface for objects that can be serialized to stream
type Serializable interface {
ToStream(w io.Writer) error
}
// Deserializable is an interface for objects that can be deserialized from stream
type Deserializable interface {
FromStream(r io.Reader) error
}
// GlobalPropertyFunc is a function for getting global properties by id
type GlobalPropertyFunc func(id int) (OGlobalProperty, bool)
// RecordSerializer is an interface for serializing records to byte streams
type RecordSerializer interface {
// String, in case of RecordSerializer must return it's class name, as it will be sent to server
String() string
ToStream(w io.Writer, rec ORecord) error
FromStream(data []byte) (ORecord, error)
SetGlobalPropertyFunc(fnc GlobalPropertyFunc)
}
// RegisterRecordFormat registers RecordSerializer with a given class name
func RegisterRecordFormat(name string, fnc func() RecordSerializer) {
recordFormats[name] = fnc
}
// SetDefaultRecordFormat sets default record serializer
func SetDefaultRecordFormat(name string) {
recordFormatDefault = name
}
// GetRecordFormat returns record serializer by class name
func GetRecordFormat(name string) RecordSerializer {
f := recordFormats[name]
if f == nil {
panic(fmt.Errorf("unknown record format: %s", name))
}
return f()
}
// GetDefaultRecordSerializer returns default record serializer
func GetDefaultRecordSerializer() RecordSerializer {
return GetRecordFormat(recordFormatDefault)
}
// DocumentSerializable is an interface for objects that can be converted to Document
type DocumentSerializable interface {
ToDocument() (*Document, error)
}
// DocumentDeserializable is an interface for objects that can be filled from Document
type DocumentDeserializable interface {
FromDocument(*Document) error
}
var _ MapSerializable = (*Document)(nil)
// MapSerializable is an interface for objects that can be converted to map[string]interface{}
type MapSerializable interface {
ToMap() (map[string]interface{}, error)
}
// SerializeAnyStreamable serializes a given object
func SerializeAnyStreamable(o CustomSerializable) ([]byte, error) {
buf := bytes.NewBuffer(nil)
bw := rw.NewWriter(buf)
bw.WriteString(o.GetClassName())
if err := o.ToStream(bw); err != nil {
return nil, err
}
if err := bw.Err(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}