Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Go] add set serializer for fury go #963

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions go/fury/set.go
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
}