-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Go] add set serializer for fury go (#963)
add set serializer for fury go
- Loading branch information
1 parent
fa58a7e
commit df10f23
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2023 The Fury Authors | ||
// | ||
// 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 fury | ||
|
||
import "reflect" | ||
|
||
// GenericSet type. | ||
// TODO use golang generics; support more concrete key types | ||
type GenericSet map[interface{}]bool | ||
|
||
func (s GenericSet) Add(values ...interface{}) { | ||
for _, v := range values { | ||
s[v] = true | ||
} | ||
} | ||
|
||
type setSerializer struct { | ||
} | ||
|
||
func (s setSerializer) TypeId() TypeId { | ||
return FURY_SET | ||
} | ||
|
||
func (s setSerializer) Write(f *Fury, buf *ByteBuffer, value reflect.Value) error { | ||
mapData := value.Interface().(GenericSet) | ||
if err := f.writeLength(buf, len(mapData)); err != nil { | ||
return err | ||
} | ||
for k := range mapData { | ||
if err := f.WriteReferencable(buf, reflect.ValueOf(k)); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s setSerializer) Read(f *Fury, buf *ByteBuffer, type_ reflect.Type, value reflect.Value) error { | ||
if value.IsNil() { | ||
value.Set(reflect.ValueOf(GenericSet{})) | ||
} | ||
f.refResolver.Reference(value) | ||
genericSet := value.Interface().(GenericSet) | ||
length := f.readLength(buf) | ||
for i := 0; i < length; i++ { | ||
var mapKey interface{} | ||
if err := f.ReadReferencable(buf, reflect.ValueOf(&mapKey).Elem()); err != nil { | ||
return err | ||
} | ||
genericSet[mapKey] = true | ||
} | ||
return nil | ||
} |