Skip to main content

type_string.go

type_string.go - Overview

This file defines a String struct and related functions for handling string values within the DiceDB system. It includes functionalities for creating string objects, serializing them, and converting other data types to strings.

Detailed Documentation

String struct

type String struct {
value string
}
  • Purpose: Represents a string value.
  • Fields:
    • value: The string value itself.

NewString

func NewString(value string) *String {
return &String{
value: value,
}
}
  • Purpose: Creates a new String object.
  • Parameters:
    • value: The string to be stored in the object.
  • Returns: A pointer to the newly created String object.

String.Serialize

func (s *String) Serialize() []byte {
return []byte{}
}
  • Purpose: Serializes the String object into a byte array.
  • Parameters:
    • s: A pointer to the String object.
  • Returns: An empty byte slice.

getRawStringOrInt

func getRawStringOrInt(v string) (interface{}, object.ObjectType) {
if len(v) > 1 && v[0] == '0' {
// If so, treat as string
return v, object.ObjTypeString
}
intValue, err := strconv.ParseInt(v, 10, 64)
if err != nil { // value is not an integer, hence a string
return v, object.ObjTypeString
}
return intValue, object.ObjTypeInt // value is an integer
}
  • Purpose: Determines if a given string can be parsed as an integer. If the string starts with 0 or cannot be parsed as an Integer, it returns the string and object.ObjTypeString. Otherwise, it returns the integer value and object.ObjTypeInt.
  • Parameters:
    • v: The string to check.
  • Returns:
    • interface{}: Either the string or the integer value.
    • object.ObjectType: Either object.ObjTypeString or object.ObjTypeInt.

convertValueToString

func convertValueToString(obj *object.Obj, oType object.ObjectType) (string, error) {
var currentValueStr string

switch oType {
case object.ObjTypeInt:
// Convert int64 to string for concatenation
currentValueStr = strconv.FormatInt(obj.Value.(int64), 10)
case object.ObjTypeString:
// Use the string value directly
currentValueStr = obj.Value.(string)
case object.ObjTypeByteArray:
val, ok := obj.Value.(*ByteArray)
if !ok {
return "", diceerrors.ErrWrongTypeOperation
}
currentValueStr = string(val.data)
default:
return "", diceerrors.ErrWrongTypeOperation
}

return currentValueStr, nil
}
  • Purpose: Converts a value from an object.Obj to a string based on its type.
  • Parameters:
    • obj: A pointer to the object.Obj containing the value to convert.
    • oType: The object.ObjectType of the value.
  • Returns:
    • string: The converted string value.
    • error: An error if the type is not supported or if there's a type assertion failure.

Getting Started Relevance