feat: add CopyRef and Deref methods to RVType(Ptr)

This commit is contained in:
Jonathan Barrow 2024-11-16 10:16:37 -05:00
parent 4fbbe2da25
commit b36470e680
No known key found for this signature in database
GPG key ID: E86E9FE9049C741F
29 changed files with 363 additions and 15 deletions

View file

@ -165,13 +165,11 @@ func (rmc *RMCMessage) decodeVerbose(data []byte) error {
}
versionContainer := types.NewClassVersionContainer()
ptr, _ := any(&versionContainer).(types.RVTypePtr)
if err := ptr.ExtractFrom(stream); err != nil {
if err := versionContainer.ExtractFrom(stream); err != nil {
return fmt.Errorf("Failed to read RMC Message ClassVersionContainer. %s", err.Error())
}
rmc.VersionContainer = ptr.(*types.ClassVersionContainer)
rmc.VersionContainer = &versionContainer
rmc.Parameters = stream.ReadRemaining()
} else {
rmc.IsSuccess, err = stream.ReadBool()

View file

@ -84,7 +84,7 @@ func (adh *AnyDataHolder) ExtractFrom(readable Readable) error {
func (adh AnyDataHolder) Copy() RVType {
copied := NewAnyDataHolder()
copied.TypeName = adh.TypeName.Copy().(String)
copied.TypeName = adh.TypeName
copied.Length1 = adh.Length1.Copy().(UInt32)
copied.Length2 = adh.Length2.Copy().(UInt32)
copied.ObjectData = adh.ObjectData.Copy()
@ -115,6 +115,19 @@ func (adh AnyDataHolder) Equals(o RVType) bool {
return adh.ObjectData.Equals(other.ObjectData)
}
// CopyRef copies the current value of the AnyDataHolder
// and returns a pointer to the new copy
func (adh AnyDataHolder) CopyRef() RVTypePtr {
return &adh
}
// Deref takes a pointer to the AnyDataHolder
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (adh *AnyDataHolder) Deref() RVType {
return *adh
}
// String returns a string representation of the struct
func (adh AnyDataHolder) String() string {
return adh.FormatToString(0)

View file

@ -36,6 +36,19 @@ func (b Bool) Equals(o RVType) bool {
return b == other
}
// CopyRef copies the current value of the Bool
// and returns a pointer to the new copy
func (b Bool) CopyRef() RVTypePtr {
return &b
}
// Deref takes a pointer to the Bool
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (b *Bool) Deref() RVType {
return *b
}
// String returns a string representation of the Bool
func (b Bool) String() string {
return fmt.Sprintf("%t", b)

View file

@ -52,6 +52,19 @@ func (b Buffer) Equals(o RVType) bool {
return bytes.Equal(b, o.(Buffer))
}
// CopyRef copies the current value of the Buffer
// and returns a pointer to the new copy
func (b Buffer) CopyRef() RVTypePtr {
return &b
}
// Deref takes a pointer to the Buffer
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (b *Buffer) Deref() RVType {
return *b
}
// String returns a string representation of the struct
func (b Buffer) String() string {
return hex.EncodeToString(b)

View file

@ -39,6 +39,19 @@ func (cvc ClassVersionContainer) Equals(o RVType) bool {
return cvc.ClassVersions.Equals(o)
}
// CopyRef copies the current value of the ClassVersionContainer
// and returns a pointer to the new copy
func (cvc ClassVersionContainer) CopyRef() RVTypePtr {
return &cvc
}
// Deref takes a pointer to the ClassVersionContainer
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (cvc *ClassVersionContainer) Deref() RVType {
return *cvc
}
// String returns a string representation of the struct
func (cvc ClassVersionContainer) String() string {
return cvc.FormatToString(0)

View file

@ -44,6 +44,19 @@ func (d Data) Equals(o RVType) bool {
return d.StructureVersion == other.StructureVersion
}
// CopyRef copies the current value of the Data
// and returns a pointer to the new copy
func (d Data) CopyRef() RVTypePtr {
return &d
}
// Deref takes a pointer to the Data
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (d *Data) Deref() RVType {
return *d
}
// String returns a string representation of the struct
func (d Data) String() string {
return d.FormatToString(0)

View file

@ -41,6 +41,19 @@ func (dt DateTime) Equals(o RVType) bool {
return dt == o.(DateTime)
}
// CopyRef copies the current value of the DateTime
// and returns a pointer to the new copy
func (dt DateTime) CopyRef() RVTypePtr {
return &dt
}
// Deref takes a pointer to the DateTime
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (dt *DateTime) Deref() RVType {
return *dt
}
// Make initilizes a DateTime with the input data
func (dt *DateTime) Make(year, month, day, hour, minute, second int) DateTime {
*dt = DateTime(second | (minute << 6) | (hour << 12) | (day << 17) | (month << 22) | (year << 26))

View file

@ -36,6 +36,19 @@ func (d Double) Equals(o RVType) bool {
return d == other
}
// CopyRef copies the current value of the Double
// and returns a pointer to the new copy
func (d Double) CopyRef() RVTypePtr {
return &d
}
// Deref takes a pointer to the Double
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (d *Double) Deref() RVType {
return *d
}
// String returns a string representation of the Double
func (d Double) String() string {
return fmt.Sprintf("%f", d)

View file

@ -36,6 +36,19 @@ func (f Float) Equals(o RVType) bool {
return f == other
}
// CopyRef copies the current value of the Float
// and returns a pointer to the new copy
func (f Float) CopyRef() RVTypePtr {
return &f
}
// Deref takes a pointer to the Float
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (f *Float) Deref() RVType {
return *f
}
// String returns a string representation of the Float
func (f Float) String() string {
return fmt.Sprintf("%f", f)

View file

@ -36,6 +36,19 @@ func (i16 Int16) Equals(o RVType) bool {
return i16 == other
}
// CopyRef copies the current value of the Int16
// and returns a pointer to the new copy
func (i16 Int16) CopyRef() RVTypePtr {
return &i16
}
// Deref takes a pointer to the Int16
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (i16 *Int16) Deref() RVType {
return *i16
}
// String returns a string representation of the Int16
func (i16 Int16) String() string {
return fmt.Sprintf("%d", i16)

View file

@ -36,6 +36,19 @@ func (i32 Int32) Equals(o RVType) bool {
return i32 == other
}
// CopyRef copies the current value of the Int32
// and returns a pointer to the new copy
func (i32 Int32) CopyRef() RVTypePtr {
return &i32
}
// Deref takes a pointer to the Int32
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (i32 *Int32) Deref() RVType {
return *i32
}
// String returns a string representation of the Int32
func (i32 Int32) String() string {
return fmt.Sprintf("%d", i32)

View file

@ -36,6 +36,19 @@ func (i64 Int64) Equals(o RVType) bool {
return i64 == other
}
// CopyRef copies the current value of the Int64
// and returns a pointer to the new copy
func (i64 Int64) CopyRef() RVTypePtr {
return &i64
}
// Deref takes a pointer to the Int64
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (i64 *Int64) Deref() RVType {
return *i64
}
// String returns a string representation of the Int64
func (i64 Int64) String() string {
return fmt.Sprintf("%d", i64)

View file

@ -36,6 +36,19 @@ func (i8 Int8) Equals(o RVType) bool {
return i8 == other
}
// CopyRef copies the current value of the Int8
// and returns a pointer to the new copy
func (i8 Int8) CopyRef() RVTypePtr {
return &i8
}
// Deref takes a pointer to the Int8
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (i8 *Int8) Deref() RVType {
return *i8
}
// String returns a string representation of the Int8
func (i8 Int8) String() string {
return fmt.Sprintf("%d", i8)

View file

@ -87,6 +87,19 @@ func (l List[T]) Equals(o RVType) bool {
return true
}
// CopyRef copies the current value of the List
// and returns a pointer to the new copy
func (l List[T]) CopyRef() RVTypePtr {
return &l
}
// Deref takes a pointer to the List
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (l *List[T]) Deref() RVType {
return *l
}
// Contains checks if the provided value exists in the List
func (l List[T]) Contains(checkValue T) bool {
for _, v := range l {

View file

@ -75,7 +75,7 @@ func (m *Map[K, V]) ExtractFrom(readable Readable) error {
func (m Map[K, V]) copyType(t any) RVType {
// * This just makes Map.Copy() a bit cleaner
// * since it doesn't have to type check
if rvt, ok := t.(interface{ Copy() RVType }); ok {
if rvt, ok := t.(RVType); ok {
return rvt.Copy()
}
@ -127,6 +127,19 @@ func (m Map[K, V]) Equals(o RVType) bool {
return true
}
// CopyRef copies the current value of the Map
// and returns a pointer to the new copy
func (m Map[K, V]) CopyRef() RVTypePtr {
return &m
}
// Deref takes a pointer to the Map
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (m *Map[K, V]) Deref() RVType {
return *m
}
// String returns a string representation of the struct
func (m Map[K, V]) String() string {
return m.FormatToString(0)

View file

@ -56,6 +56,19 @@ func (p PID) Equals(o RVType) bool {
return p == o.(PID)
}
// CopyRef copies the current value of the PID
// and returns a pointer to the new copy
func (p PID) CopyRef() RVTypePtr {
return &p
}
// Deref takes a pointer to the PID
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (p *PID) Deref() RVType {
return *p
}
// String returns a string representation of the struct
func (p PID) String() string {
return p.FormatToString(0)

View file

@ -52,6 +52,19 @@ func (qb QBuffer) Equals(o RVType) bool {
return bytes.Equal(qb, o.(QBuffer))
}
// CopyRef copies the current value of the QBuffer
// and returns a pointer to the new copy
func (qb QBuffer) CopyRef() RVTypePtr {
return &qb
}
// Deref takes a pointer to the QBuffer
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (qb *QBuffer) Deref() RVType {
return *qb
}
// String returns a string representation of the struct
func (qb QBuffer) String() string {
return hex.EncodeToString(qb)

View file

@ -43,6 +43,19 @@ func (r QResult) Equals(o RVType) bool {
return r == o.(QResult)
}
// CopyRef copies the current value of the QResult
// and returns a pointer to the new copy
func (r QResult) CopyRef() RVTypePtr {
return &r
}
// Deref takes a pointer to the QResult
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (r *QResult) Deref() RVType {
return *r
}
// IsSuccess returns true if the QResult is a success
func (r QResult) IsSuccess() bool {
return int(r)&errorMask == 0

View file

@ -43,6 +43,19 @@ func (qu QUUID) Equals(o RVType) bool {
return bytes.Equal(qu, o.(QUUID))
}
// CopyRef copies the current value of the QUUID
// and returns a pointer to the new copy
func (qu QUUID) CopyRef() RVTypePtr {
return &qu
}
// Deref takes a pointer to the QUUID
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (qu *QUUID) Deref() RVType {
return *qu
}
// String returns a string representation of the struct
func (qu QUUID) String() string {
return qu.FormatToString(0)

View file

@ -78,6 +78,19 @@ func (rr ResultRange) Equals(o RVType) bool {
return rr.Length.Equals(&other.Length)
}
// CopyRef copies the current value of the ResultRange
// and returns a pointer to the new copy
func (rr ResultRange) CopyRef() RVTypePtr {
return &rr
}
// Deref takes a pointer to the ResultRange
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (rr *ResultRange) Deref() RVType {
return *rr
}
// String returns a string representation of the struct
func (rr ResultRange) String() string {
return rr.FormatToString(0)

View file

@ -113,6 +113,19 @@ func (rvcd RVConnectionData) Equals(o RVType) bool {
return true
}
// CopyRef copies the current value of the RVConnectionData
// and returns a pointer to the new copy
func (rvcd RVConnectionData) CopyRef() RVTypePtr {
return &rvcd
}
// Deref takes a pointer to the RVConnectionData
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (rvcd *RVConnectionData) Deref() RVType {
return *rvcd
}
// String returns a string representation of the struct
func (rvcd RVConnectionData) String() string {
return rvcd.FormatToString(0)

View file

@ -6,12 +6,14 @@ package types
type RVType interface {
WriteTo(writable Writable)
Copy() RVType
CopyRef() RVTypePtr
Equals(other RVType) bool
}
// RVTypePtr represents a pointer to an RVType.
// User to separate pointer receivers for easier type checking.
// Used to separate pointer receivers for easier type checking.
type RVTypePtr interface {
RVType
ExtractFrom(readable Readable) error
Deref() RVType
}

View file

@ -141,6 +141,19 @@ func (s StationURL) Equals(o RVType) bool {
return true
}
// CopyRef copies the current value of the StationURL
// and returns a pointer to the new copy
func (s StationURL) CopyRef() RVTypePtr {
return &s
}
// Deref takes a pointer to the StationURL
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (s *StationURL) Deref() RVType {
return *s
}
// Set sets a StationURL parameter.
//
// "custom" determines whether or not the parameter is a standard

View file

@ -72,6 +72,19 @@ func (s String) Equals(o RVType) bool {
return s == o.(String)
}
// CopyRef copies the current value of the String
// and returns a pointer to the new copy
func (s String) CopyRef() RVTypePtr {
return &s
}
// Deref takes a pointer to the String
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (s *String) Deref() RVType {
return *s
}
// String returns a string representation of the struct
func (s String) String() string {
return fmt.Sprintf("%q", string(s))

View file

@ -36,6 +36,19 @@ func (u16 UInt16) Equals(o RVType) bool {
return u16 == other
}
// CopyRef copies the current value of the UInt16
// and returns a pointer to the new copy
func (u16 UInt16) CopyRef() RVTypePtr {
return &u16
}
// Deref takes a pointer to the UInt16
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (u16 *UInt16) Deref() RVType {
return *u16
}
// String returns a string representation of the UInt16
func (u16 UInt16) String() string {
return fmt.Sprintf("%d", u16)

View file

@ -36,6 +36,19 @@ func (u32 UInt32) Equals(o RVType) bool {
return u32 == other
}
// CopyRef copies the current value of the UInt32
// and returns a pointer to the new copy
func (u32 UInt32) CopyRef() RVTypePtr {
return &u32
}
// Deref takes a pointer to the UInt32
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (u32 *UInt32) Deref() RVType {
return *u32
}
// String returns a string representation of the UInt32
func (u32 UInt32) String() string {
return fmt.Sprintf("%d", u32)

View file

@ -36,6 +36,19 @@ func (u64 UInt64) Equals(o RVType) bool {
return u64 == other
}
// CopyRef copies the current value of the UInt64
// and returns a pointer to the new copy
func (u64 UInt64) CopyRef() RVTypePtr {
return &u64
}
// Deref takes a pointer to the UInt64
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (u64 *UInt64) Deref() RVType {
return *u64
}
// String returns a string representation of the UInt64
func (u64 UInt64) String() string {
return fmt.Sprintf("%d", u64)

View file

@ -36,6 +36,19 @@ func (u8 UInt8) Equals(o RVType) bool {
return u8 == other
}
// CopyRef copies the current value of the UInt8
// and returns a pointer to the new copy
func (u8 UInt8) CopyRef() RVTypePtr {
return &u8
}
// Deref takes a pointer to the UInt8
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (u8 *UInt8) Deref() RVType {
return *u8
}
// String returns a string representation of the UInt8
func (u8 UInt8) String() string {
return fmt.Sprintf("%d", u8)

View file

@ -1,7 +1,6 @@
package types
import (
"errors"
"fmt"
"strings"
)
@ -48,17 +47,15 @@ func (v *Variant) ExtractFrom(readable Readable) error {
return fmt.Errorf("Invalid Variant type ID %d", typeID)
}
v.Type = VariantTypes[typeID].Copy()
ptr, ok := any(&v.Type).(RVTypePtr)
if !ok {
return errors.New("Variant type data is not a valid RVType. Missing ExtractFrom pointer receiver")
}
// * Create a new copy and get a pointer to it.
// * Required so that we have access to ExtractFrom
ptr := VariantTypes[typeID].CopyRef()
if err := ptr.ExtractFrom(readable); err != nil {
return fmt.Errorf("Failed to read Variant type data. %s", err.Error())
}
v.Type = ptr.Deref() // * Dereference the RVTypePtr pointer back into a non-pointer type
return nil
}
@ -94,6 +91,19 @@ func (v Variant) Equals(o RVType) bool {
return true
}
// CopyRef copies the current value of the Variant
// and returns a pointer to the new copy
func (v Variant) CopyRef() RVTypePtr {
return &v
}
// Deref takes a pointer to the Variant
// and dereferences it to the raw value.
// Only useful when working with an instance of RVTypePtr
func (v *Variant) Deref() RVType {
return *v
}
// String returns a string representation of the struct
func (v Variant) String() string {
return v.FormatToString(0)