Skip to content

Commit 72150b1

Browse files
authored
Compact Serialization Sample [API-1347] (#949)
* Added the compact serialization sample * typo
1 parent 50214e1 commit 72150b1

File tree

1 file changed

+100
-0
lines changed
  • examples/serialization/compact

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License")
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"reflect"
23+
24+
"github.com/hazelcast/hazelcast-go-client"
25+
"github.com/hazelcast/hazelcast-go-client/serialization"
26+
)
27+
28+
// Person is a simple type that contains information about a person.
29+
type Person struct {
30+
Name string
31+
Age int32
32+
}
33+
34+
func (p Person) String() string {
35+
return fmt.Sprintf("Name: %s, Age: %d", p.Name, p.Age)
36+
}
37+
38+
// PersonSerializer serializes a Person value using compact serialization.
39+
type PersonSerializer struct{}
40+
41+
// Type returns the target type: Person
42+
func (s PersonSerializer) Type() reflect.Type {
43+
return reflect.TypeOf(Person{})
44+
}
45+
46+
// TypeName returns an identifier for the serialized type.
47+
func (s PersonSerializer) TypeName() string {
48+
return "Person"
49+
}
50+
51+
// Read reads a Person value from compact serialized data.
52+
func (s PersonSerializer) Read(r serialization.CompactReader) interface{} {
53+
var name string
54+
p := r.ReadString("name")
55+
if p != nil {
56+
name = *p
57+
}
58+
return Person{
59+
Name: name,
60+
Age: r.ReadInt32("age"),
61+
}
62+
}
63+
64+
// Write writes a Person value as compact serialized data.
65+
func (s PersonSerializer) Write(w serialization.CompactWriter, value interface{}) {
66+
v := value.(Person)
67+
w.WriteString("name", &v.Name)
68+
w.WriteInt32("age", v.Age)
69+
}
70+
71+
func main() {
72+
ctx := context.Background()
73+
// create the configuration and set the compact serializers.
74+
var cfg hazelcast.Config
75+
cfg.Serialization.Compact.SetSerializers(PersonSerializer{})
76+
// start the client with the configuration.
77+
client, err := hazelcast.StartNewClientWithConfig(ctx, cfg)
78+
if err != nil {
79+
panic(fmt.Errorf("starting the client: %w", err))
80+
}
81+
// get the sample map, so we can call Get and Set operations on it.
82+
m, err := client.GetMap(ctx, "people")
83+
if err != nil {
84+
panic(fmt.Errorf("getting the map: %w", err))
85+
}
86+
person := Person{
87+
Name: "Jane",
88+
Age: 25,
89+
}
90+
// Set the person value in the map.
91+
if err := m.Set(ctx, "jane", person); err != nil {
92+
panic(fmt.Errorf("setting the value: %w", err))
93+
}
94+
// Get the value back from the map.
95+
value, err := m.Get(ctx, "jane")
96+
if err != nil {
97+
panic(fmt.Errorf("getting the value: %w", err))
98+
}
99+
fmt.Println("read the value back: ", value)
100+
}

0 commit comments

Comments
 (0)