forked from hazelcast/hazelcast-go-client
/
config.go
137 lines (112 loc) · 4.92 KB
/
config.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain 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 serialization
import (
"reflect"
"github.com/hazelcast/hazelcast-go-client/core"
)
// Config contains the serialization configuration of a Hazelcast instance.
type Config struct {
// isBigEndian is the byte order bool. If true, it means BigEndian, otherwise LittleEndian.
isBigEndian bool
// dataSerializableFactories is a map of factory IDs and corresponding IdentifiedDataSerializable factories.
dataSerializableFactories map[int32]IdentifiedDataSerializableFactory
// portableFactories is a map of factory IDs and corresponding Portable factories.
portableFactories map[int32]PortableFactory
// portableVersion will be used to differentiate two versions of the same struct that have changes on the struct,
// like adding/removing a field or changing a type of a field.
portableVersion int32
// customSerializers is a map of object types and corresponding custom serializers.
customSerializers map[reflect.Type]Serializer
// globalSerializer is the serializer that will be used if no other serializer is applicable.
globalSerializer Serializer
// classDefinitions contains ClassDefinitions for portable structs.
classDefinitions []ClassDefinition
}
// NewConfig returns a Config with default values.
func NewConfig() *Config {
return &Config{
isBigEndian: true,
dataSerializableFactories: make(map[int32]IdentifiedDataSerializableFactory),
portableFactories: make(map[int32]PortableFactory),
portableVersion: 0,
customSerializers: make(map[reflect.Type]Serializer),
}
}
// IsBigEndian returns isBigEndian bool value.
func (sc *Config) IsBigEndian() bool {
return sc.isBigEndian
}
// DataSerializableFactories returns a map of factory IDs and corresponding IdentifiedDataSerializable factories.
func (sc *Config) DataSerializableFactories() map[int32]IdentifiedDataSerializableFactory {
return sc.dataSerializableFactories
}
// PortableFactories returns a map of factory IDs and corresponding Portable factories.
func (sc *Config) PortableFactories() map[int32]PortableFactory {
return sc.portableFactories
}
// PortableVersion returns version of a portable struct.
func (sc *Config) PortableVersion() int32 {
return sc.portableVersion
}
// CustomSerializers returns a map of object types and corresponding custom serializers.
func (sc *Config) CustomSerializers() map[reflect.Type]Serializer {
return sc.customSerializers
}
// GlobalSerializer returns the global serializer.
func (sc *Config) GlobalSerializer() Serializer {
return sc.globalSerializer
}
// ClassDefinitions returns registered class definitions of portable structs.
func (sc *Config) ClassDefinitions() []ClassDefinition {
return sc.classDefinitions
}
// SetByteOrder sets the byte order. If true, it means BigEndian, otherwise LittleEndian.
func (sc *Config) SetByteOrder(isBigEndian bool) {
sc.isBigEndian = isBigEndian
}
// AddDataSerializableFactory adds an IdentifiedDataSerializableFactory for a given factory ID.
func (sc *Config) AddDataSerializableFactory(factoryID int32, f IdentifiedDataSerializableFactory) {
sc.dataSerializableFactories[factoryID] = f
}
// AddPortableFactory adds a PortableFactory for a given factory ID.
func (sc *Config) AddPortableFactory(factoryID int32, pf PortableFactory) {
sc.portableFactories[factoryID] = pf
}
// AddClassDefinition registers class definitions explicitly.
func (sc *Config) AddClassDefinition(classDefinition ...ClassDefinition) {
sc.classDefinitions = append(sc.classDefinitions, classDefinition...)
}
// SetPortableVersion sets the portable version.
func (sc *Config) SetPortableVersion(version int32) {
sc.portableVersion = version
}
// AddCustomSerializer adds a custom serializer for a given type. It can be an interface type or a struct type.
func (sc *Config) AddCustomSerializer(typ reflect.Type, serializer Serializer) error {
if serializer.ID() > 0 {
sc.customSerializers[typ] = serializer
} else {
return core.NewHazelcastSerializationError("custom serializer should have its typeId greater than or equal to 1", nil)
}
return nil
}
// SetGlobalSerializer sets the global serializer.
func (sc *Config) SetGlobalSerializer(serializer Serializer) error {
if serializer.ID() > 0 {
sc.globalSerializer = serializer
} else {
return core.NewHazelcastSerializationError("global serializer should have its typeId greater than or equal to 1", nil)
}
return nil
}