From 2ffee3c9d265bc869b3ad66a6c26086723b5f933 Mon Sep 17 00:00:00 2001 From: Shawn Date: Fri, 6 Oct 2023 00:38:46 +0800 Subject: [PATCH] [Go] rename Reference to Ref (#975) rename Reference to Ref --- go/fury/fury.go | 8 ++-- go/fury/reference.go | 96 +++++++++++++++++++-------------------- go/fury/reference_test.go | 26 +++++------ go/fury/slice.go | 6 +-- 4 files changed, 68 insertions(+), 68 deletions(-) diff --git a/go/fury/fury.go b/go/fury/fury.go index d394711f25..a5bd915b1e 100644 --- a/go/fury/fury.go +++ b/go/fury/fury.go @@ -24,7 +24,7 @@ import ( func NewFury(referenceTracking bool) *Fury { fury := &Fury{ typeResolver: newTypeResolver(), - refResolver: newReferenceResolver(referenceTracking), + refResolver: newRefResolver(referenceTracking), referenceTracking: referenceTracking, language: XLANG, buffer: NewByteBuffer(nil), @@ -89,7 +89,7 @@ const ( type Fury struct { typeResolver *typeResolver - refResolver *ReferenceResolver + refResolver *RefResolver referenceTracking bool language Language bufferCallback BufferCallback @@ -226,7 +226,7 @@ func (f *Fury) WriteReferencable(buffer *ByteBuffer, value reflect.Value) error } func (f *Fury) writeReferencableBySerializer(buffer *ByteBuffer, value reflect.Value, serializer Serializer) error { - if refWritten, err := f.refResolver.WriteReferenceOrNull(buffer, value); err == nil && !refWritten { + if refWritten, err := f.refResolver.WriteRefOrNull(buffer, value); err == nil && !refWritten { // check ptr if value.Kind() == reflect.Ptr { switch value.Elem().Kind() { @@ -353,7 +353,7 @@ func (f *Fury) ReadReferencable(buffer *ByteBuffer, value reflect.Value) error { } func (f *Fury) readReferencableBySerializer(buf *ByteBuffer, value reflect.Value, serializer Serializer) (err error) { - refId, err := f.refResolver.TryPreserveReferenceId(buf) + refId, err := f.refResolver.TryPreserveRefId(buf) if err != nil { return err } diff --git a/go/fury/reference.go b/go/fury/reference.go index 8c7ef85b30..33aaa45034 100644 --- a/go/fury/reference.go +++ b/go/fury/reference.go @@ -31,35 +31,35 @@ const ( RefValueFlag int8 = 0 ) -// ReferenceResolver class is used to track objects that have already been read or written. -type ReferenceResolver struct { - referenceTracking bool - writtenObjects map[referenceKey]int32 - readObjects []reflect.Value - readReferenceIds []int32 - readObject reflect.Value // last read object which is not a reference +// RefResolver class is used to track objects that have already been read or written. +type RefResolver struct { + refTracking bool + writtenObjects map[refKey]int32 + readObjects []reflect.Value + readRefIds []int32 + readObject reflect.Value // last read object which is not a reference } -type referenceKey struct { +type refKey struct { pointer unsafe.Pointer length int // for slice and *array only } -func newReferenceResolver(referenceTracking bool) *ReferenceResolver { - referenceResolver := &ReferenceResolver{ - referenceTracking: referenceTracking, - writtenObjects: map[referenceKey]int32{}, +func newRefResolver(refTracking bool) *RefResolver { + refResolver := &RefResolver{ + refTracking: refTracking, + writtenObjects: map[refKey]int32{}, } - return referenceResolver + return refResolver } -// WriteReferenceOrNull write reference and tag for the value if the value has been written previously, +// WriteRefOrNull write reference and tag for the value if the value has been written previously, // write null/not-null tag otherwise. Returns true if no bytes need to be written for the object. // See https://go101.org/article/value-part.html for internal structure definitions of common types. // Note that for slice and substring, if the start addr or length are different, we take two objects as // different references. -func (r *ReferenceResolver) WriteReferenceOrNull(buffer *ByteBuffer, value reflect.Value) (refWritten bool, err error) { - if !r.referenceTracking { +func (r *RefResolver) WriteRefOrNull(buffer *ByteBuffer, value reflect.Value) (refWritten bool, err error) { + if !r.refTracking { if isNil(value) { buffer.WriteInt8(NullFlag) return true, nil @@ -86,7 +86,7 @@ func (r *ReferenceResolver) WriteReferenceOrNull(buffer *ByteBuffer, value refle length = value.Len() case reflect.Interface: value = value.Elem() - return r.WriteReferenceOrNull(buffer, value) + return r.WriteRefOrNull(buffer, value) case reflect.String: isNil = false str := unsafeGetBytes(value.Interface().(string)) @@ -103,7 +103,7 @@ func (r *ReferenceResolver) WriteReferenceOrNull(buffer *ByteBuffer, value refle buffer.WriteInt8(NullFlag) return true, nil } else { - refKey := referenceKey{pointer: unsafe.Pointer(value.Pointer()), length: length} + refKey := refKey{pointer: unsafe.Pointer(value.Pointer()), length: length} if writtenId, ok := r.writtenObjects[refKey]; ok { // The obj has been written previously. buffer.WriteInt8(RefFlag) @@ -122,16 +122,16 @@ func (r *ReferenceResolver) WriteReferenceOrNull(buffer *ByteBuffer, value refle } } -// ReadReferenceOrNull returns RefFlag if a Reference to a previously read object +// ReadRefOrNull returns RefFlag if a ref to a previously read object // was read. Returns NullFlag if the object is null. Returns RefValueFlag if the object is not -// null and Reference tracking is not enabled or the object is first read. -func (r *ReferenceResolver) ReadReferenceOrNull(buffer *ByteBuffer) int8 { +// null and ref tracking is not enabled or the object is first read. +func (r *RefResolver) ReadRefOrNull(buffer *ByteBuffer) int8 { refTag := buffer.ReadInt8() - if !r.referenceTracking { + if !r.refTracking { return refTag } if refTag == RefFlag { - // read reference id and get object from reference resolver + // read ref id and get object from ref resolver refId := buffer.ReadVarInt32() r.readObject = r.GetReadObject(refId) return RefFlag @@ -141,11 +141,11 @@ func (r *ReferenceResolver) ReadReferenceOrNull(buffer *ByteBuffer) int8 { return refTag } -// PreserveReferenceId preserve a reference id, which is used by Reference / SetReadObject to +// PreserveRefId preserve a ref id, which is used by Reference / SetReadObject to // set up reference for object that is first deserialized. -// Returns a reference id or -1 if reference is not enabled. -func (r *ReferenceResolver) PreserveReferenceId() (int32, error) { - if !r.referenceTracking { +// Returns a ref id or -1 if reference is not enabled. +func (r *RefResolver) PreserveRefId() (int32, error) { + if !r.refTracking { return -1, nil } nextReadRefId_ := len(r.readObjects) @@ -154,56 +154,56 @@ func (r *ReferenceResolver) PreserveReferenceId() (int32, error) { } nextReadRefId := int32(nextReadRefId_) r.readObjects = append(r.readObjects, reflect.Value{}) - r.readReferenceIds = append(r.readReferenceIds, nextReadRefId) + r.readRefIds = append(r.readRefIds, nextReadRefId) return nextReadRefId, nil } -func (r *ReferenceResolver) TryPreserveReferenceId(buffer *ByteBuffer) (int32, error) { +func (r *RefResolver) TryPreserveRefId(buffer *ByteBuffer) (int32, error) { headFlag := buffer.ReadInt8() if headFlag == RefFlag { - // read reference id and get object from reference resolver + // read ref id and get object from ref resolver refId := buffer.ReadVarInt32() r.readObject = r.GetReadObject(refId) } else { r.readObject = reflect.Value{} if headFlag == RefValueFlag { - return r.PreserveReferenceId() + return r.PreserveRefId() } } - // `headFlag` except `REF_FLAG` can be used as stub reference id because we use + // `headFlag` except `REF_FLAG` can be used as stub ref id because we use // `refId >= NOT_NULL_VALUE_FLAG` to read data. return int32(headFlag), nil } // Reference tracking references relationship. Call this method immediately after composited object such as // object array/map/collection/bean is created so that circular reference can be deserialized correctly. -func (r *ReferenceResolver) Reference(value reflect.Value) { - if !r.referenceTracking { +func (r *RefResolver) Reference(value reflect.Value) { + if !r.refTracking { return } - length := len(r.readReferenceIds) - refId := r.readReferenceIds[length-1] - r.readReferenceIds = r.readReferenceIds[:length-1] + length := len(r.readRefIds) + refId := r.readRefIds[length-1] + r.readRefIds = r.readRefIds[:length-1] r.SetReadObject(refId, value) } // GetReadObject returns the object for the specified id. -func (r *ReferenceResolver) GetReadObject(refId int32) reflect.Value { - if !r.referenceTracking { +func (r *RefResolver) GetReadObject(refId int32) reflect.Value { + if !r.refTracking { return reflect.Value{} } return r.readObjects[refId] } -func (r *ReferenceResolver) GetCurrentReadObject() reflect.Value { +func (r *RefResolver) GetCurrentReadObject() reflect.Value { return r.readObject } // SetReadObject sets the id for an object that has been read. // id: The id from {@link #NextReadRefId}. // object: the object that has been read -func (r *ReferenceResolver) SetReadObject(refId int32, value reflect.Value) { - if !r.referenceTracking { +func (r *RefResolver) SetReadObject(refId int32, value reflect.Value) { + if !r.refTracking { return } if refId >= 0 { @@ -211,23 +211,23 @@ func (r *ReferenceResolver) SetReadObject(refId int32, value reflect.Value) { } } -func (r *ReferenceResolver) reset() { +func (r *RefResolver) reset() { r.resetRead() r.resetWrite() } -func (r *ReferenceResolver) resetRead() { - if !r.referenceTracking { +func (r *RefResolver) resetRead() { + if !r.refTracking { return } r.readObjects = nil - r.readReferenceIds = nil + r.readRefIds = nil r.readObject = reflect.Value{} } -func (r *ReferenceResolver) resetWrite() { +func (r *RefResolver) resetWrite() { if len(r.writtenObjects) > 0 { - r.writtenObjects = map[referenceKey]int32{} + r.writtenObjects = map[refKey]int32{} } } diff --git a/go/fury/reference_test.go b/go/fury/reference_test.go index 033681a55f..c43c01c3a7 100644 --- a/go/fury/reference_test.go +++ b/go/fury/reference_test.go @@ -22,7 +22,7 @@ import ( ) func TestReferenceResolver(t *testing.T) { - refResolver := newReferenceResolver(true) + refResolver := newRefResolver(true) buf := NewByteBuffer(nil) var values []interface{} values = append(values, commonSlice()...) @@ -31,36 +31,36 @@ func TestReferenceResolver(t *testing.T) { bar := Bar{} values = append(values, "", "str", &foo, &bar) for _, data := range values { - refWritten, err := refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(data)) + refWritten, err := refResolver.WriteRefOrNull(buf, reflect.ValueOf(data)) require.Nil(t, err) require.False(t, refWritten) - refWritten, err = refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(data)) + refWritten, err = refResolver.WriteRefOrNull(buf, reflect.ValueOf(data)) require.Nil(t, err) require.True(t, refWritten) } refResolver.readObjects = make([]reflect.Value, len(refResolver.writtenObjects)) for range values { - require.Equal(t, refResolver.ReadReferenceOrNull(buf), RefValueFlag) - require.Equal(t, refResolver.ReadReferenceOrNull(buf), RefFlag) + require.Equal(t, refResolver.ReadRefOrNull(buf), RefValueFlag) + require.Equal(t, refResolver.ReadRefOrNull(buf), RefFlag) } { s := []int{1, 2, 3} require.True(t, same(s, s)) require.False(t, same(s, s[1:])) - refWritten, err := refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(s)) + refWritten, err := refResolver.WriteRefOrNull(buf, reflect.ValueOf(s)) require.Nil(t, err) require.False(t, refWritten) - refWritten, err = refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(s)) + refWritten, err = refResolver.WriteRefOrNull(buf, reflect.ValueOf(s)) require.Nil(t, err) require.True(t, refWritten) - refWritten, err = refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(s[1:])) + refWritten, err = refResolver.WriteRefOrNull(buf, reflect.ValueOf(s[1:])) require.Nil(t, err) require.False(t, refWritten) } } func TestNonReferenceResolver(t *testing.T) { - refResolver := newReferenceResolver(false) + refResolver := newRefResolver(false) buf := NewByteBuffer(nil) var values []interface{} values = append(values, commonSlice()...) @@ -69,16 +69,16 @@ func TestNonReferenceResolver(t *testing.T) { bar := Bar{} values = append(values, "", "str", &foo, &bar) for _, data := range values { - refWritten, err := refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(data)) + refWritten, err := refResolver.WriteRefOrNull(buf, reflect.ValueOf(data)) require.Nil(t, err) require.False(t, refWritten) - refWritten, err = refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(data)) + refWritten, err = refResolver.WriteRefOrNull(buf, reflect.ValueOf(data)) require.Nil(t, err) require.False(t, refWritten) } for range values { - require.Equal(t, refResolver.ReadReferenceOrNull(buf), NotNullValueFlag) - require.Equal(t, refResolver.ReadReferenceOrNull(buf), NotNullValueFlag) + require.Equal(t, refResolver.ReadRefOrNull(buf), NotNullValueFlag) + require.Equal(t, refResolver.ReadRefOrNull(buf), NotNullValueFlag) } } diff --git a/go/fury/slice.go b/go/fury/slice.go index de45a78929..e2b335a467 100644 --- a/go/fury/slice.go +++ b/go/fury/slice.go @@ -329,7 +329,7 @@ func (s stringSliceSerializer) Write(f *Fury, buf *ByteBuffer, value reflect.Val return err } for _, str := range v { - if refWritten, err := f.refResolver.WriteReferenceOrNull(buf, reflect.ValueOf(str)); err == nil { + if refWritten, err := f.refResolver.WriteRefOrNull(buf, reflect.ValueOf(str)); err == nil { if !refWritten { if err := writeString(buf, str); err != nil { return err @@ -347,10 +347,10 @@ func (s stringSliceSerializer) Read(f *Fury, buf *ByteBuffer, type_ reflect.Type r := make([]string, length, length) f.refResolver.Reference(reflect.ValueOf(r)) for i := 0; i < length; i++ { - if refFlag := f.refResolver.ReadReferenceOrNull(buf); refFlag == RefValueFlag || refFlag == NotNullValueFlag { + if refFlag := f.refResolver.ReadRefOrNull(buf); refFlag == RefValueFlag || refFlag == NotNullValueFlag { var nextReadRefId int32 if refFlag == RefValueFlag { - nextReadRefId, err = f.refResolver.PreserveReferenceId() + nextReadRefId, err = f.refResolver.PreserveRefId() if err != nil { return err }