github.com/dolthub/go-mysql-server@v0.18.0/sql/types/extended.go (about)

     1  // Copyright 2024 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package types
    16  
    17  import (
    18  	"encoding/hex"
    19  	"errors"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  )
    23  
    24  // ExtendedType is a serializable type that offers an extended interface for interacting with types in a wider context.
    25  type ExtendedType interface {
    26  	sql.Type
    27  	// SerializedCompare compares two byte slices that each represent a serialized value, without first deserializing
    28  	// the value. This should return the same result as the Compare function.
    29  	SerializedCompare(v1 []byte, v2 []byte) (int, error)
    30  	// SerializeValue converts the given value into a binary representation.
    31  	SerializeValue(val any) ([]byte, error)
    32  	// DeserializeValue converts a binary representation of a value into its canonical type.
    33  	DeserializeValue(val []byte) (any, error)
    34  	// FormatValue returns a string version of the value. Primarily intended for display.
    35  	FormatValue(val any) (string, error)
    36  	// MaxSerializedWidth returns the maximum size that the serialized value may represent.
    37  	MaxSerializedWidth() ExtendedTypeSerializedWidth
    38  }
    39  
    40  type ExtendedTypeSerializedWidth uint8
    41  
    42  const (
    43  	ExtendedTypeSerializedWidth_64K       ExtendedTypeSerializedWidth = iota // Represents a variably-sized value. The maximum number of bytes is (2^16)-1.
    44  	ExtendedTypeSerializedWidth_Unbounded                                    // Represents a variably-sized value. The maximum number of bytes is (2^64)-1, which is practically unbounded.
    45  )
    46  
    47  // ExtendedTypeSerializer is the function signature for the extended type serializer.
    48  type ExtendedTypeSerializer func(extendedType ExtendedType) ([]byte, error)
    49  
    50  // ExtendedTypeDeserializer is the function signature for the extended type deserializer.
    51  type ExtendedTypeDeserializer func(serializedType []byte) (ExtendedType, error)
    52  
    53  // extendedTypeDeserializer refers to the function that will handle deserialization of extended types.
    54  var extendedTypeDeserializer ExtendedTypeDeserializer = func(serializedType []byte) (ExtendedType, error) {
    55  	return nil, errors.New("placeholder extended type deserializer")
    56  }
    57  
    58  // extendedTypeSerializer refers to the function that will handle serialization of extended types.
    59  var extendedTypeSerializer ExtendedTypeSerializer = func(extendedType ExtendedType) ([]byte, error) {
    60  	return nil, errors.New("placeholder extended type serializer")
    61  }
    62  
    63  // SetExtendedTypeSerializers sets the handlers that are able to serialize and deserialize extended types.
    64  // It is recommended to set these from within an init function within the calling package.
    65  func SetExtendedTypeSerializers(serializer ExtendedTypeSerializer, deserializer ExtendedTypeDeserializer) {
    66  	extendedTypeSerializer = serializer
    67  	extendedTypeDeserializer = deserializer
    68  }
    69  
    70  // SerializeType serializes the given extended type into a byte slice.
    71  func SerializeType(typ ExtendedType) ([]byte, error) {
    72  	return extendedTypeSerializer(typ)
    73  }
    74  
    75  // SerializeTypeToString serializes the given extended type into a hex-encoded string.
    76  func SerializeTypeToString(typ ExtendedType) (string, error) {
    77  	serializedType, err := extendedTypeSerializer(typ)
    78  	if err != nil {
    79  		return "", err
    80  	}
    81  	return hex.EncodeToString(serializedType), nil
    82  }
    83  
    84  // DeserializeType deserializes a byte slice representing a serialized extended type.
    85  func DeserializeType(typ []byte) (ExtendedType, error) {
    86  	return extendedTypeDeserializer(typ)
    87  }
    88  
    89  // DeserializeTypeFromString deserializes a hex-encoded string representing a serialized extended type.
    90  func DeserializeTypeFromString(typ string) (ExtendedType, error) {
    91  	serializedType, err := hex.DecodeString(typ)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return extendedTypeDeserializer(serializedType)
    96  }
    97  
    98  // IsExtendedType returns whether the given sql.Type is an ExtendedType.
    99  func IsExtendedType(typ sql.Type) bool {
   100  	_, ok := typ.(ExtendedType)
   101  	return ok
   102  }