joao-r-reis commented on code in PR #1855: URL: https://github.com/apache/cassandra-gocql-driver/pull/1855#discussion_r2024872482
########## types.go: ########## @@ -0,0 +1,431 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 gocql + +import ( + "fmt" + "reflect" +) + +// CQLType is the interface that must be implemented by all registered types. +type CQLType interface { + // Params should return the types to build the slice of params for + // TypeInfoFromParams. These params are sent to TypeInfoFromParams after being read + // from the frame. The supported types are: Type, TypeInfo, []UDTField, + // []byte, string, int, byte. + // If no params are needed this can return a nil slice. + Params(proto int) []reflect.Type + + // TypeInfoFromParams should return a TypeInfo implementation for the type + // with the given parameters filled after the return from Params(). + TypeInfoFromParams(proto int, params []interface{}) TypeInfo + + // TypeInfoFromString should return a TypeInfo implementation for the type with + // the given names/classes. Only the portion within the parantheses or arrows + // are passed to this function. For simple types, the name passed might be empty. + TypeInfoFromString(proto int, name string) TypeInfo + + // Marshal should marshal the value for the given TypeInfo into a byte slice + Marshal(info TypeInfo, value interface{}) ([]byte, error) + + // Unmarshal should unmarshal the byte slice into the value for the given + // TypeInfo. It must support data being nil for nullable columns and it must + // support pointers to empty interfaces to get the default go type for the + // column. + Unmarshal(info TypeInfo, data []byte, value interface{}) error +} + +var ( + asciiRegisteredType = ®isteredType{ + CQLType: varcharLikeCQLType{ + typ: TypeAscii, + }, + str: "ascii", + } + + bigIntRegisteredType = ®isteredType{ + CQLType: bigIntLikeCQLType{ + typ: TypeBigInt, + }, + str: "bigint", + } + + blobRegisteredType = ®isteredType{ + CQLType: varcharLikeCQLType{ + typ: TypeBlob, + }, + str: "blob", + } + + booleanRegisteredType = ®isteredType{ + CQLType: booleanCQLType{}, + str: "boolean", + } + + counterRegisteredType = ®isteredType{ + CQLType: bigIntLikeCQLType{ + typ: TypeCounter, + }, + str: "counter", + } + + dateRegisteredType = ®isteredType{ + CQLType: dateCQLType{}, + str: "date", + } + + decimalRegisteredType = ®isteredType{ + CQLType: decimalCQLType{}, + str: "decimal", + } + + doubleRegisteredType = ®isteredType{ + CQLType: doubleCQLType{}, + str: "double", + } + + durationRegisteredType = ®isteredType{ + CQLType: durationCQLType{}, + str: "duration", + } + + floatRegisteredType = ®isteredType{ + CQLType: floatCQLType{}, + str: "float", + } + + inetRegisteredType = ®isteredType{ + CQLType: inetCQLType{}, + str: "inet", + } + + intRegisteredType = ®isteredType{ + CQLType: intCQLType{}, + str: "int", + } + + smallintRegisteredType = ®isteredType{ + CQLType: smallIntCQLType{}, + str: "smallint", + } + + textRegisteredType = ®isteredType{ + CQLType: varcharLikeCQLType{ + typ: TypeText, + }, + str: "text", + } + + timeRegisteredType = ®isteredType{ + CQLType: timeCQLType{}, + str: "time", + } + + timestampRegisteredType = ®isteredType{ + CQLType: timestampCQLType{}, + str: "timestamp", + } + + timeUUIDRegisteredType = ®isteredType{ + CQLType: timeUUIDCQLType{}, + str: "timeuuid", + } + + tinyIntRegisteredType = ®isteredType{ + CQLType: tinyIntCQLType{}, + str: "tinyint", + } + + uuidRegisteredType = ®isteredType{ + CQLType: uuidCQLType{}, + str: "uuid", + } + + varcharRegisteredType = ®isteredType{ + CQLType: varcharLikeCQLType{ + typ: TypeVarchar, + }, + str: "varchar", + } + + varintRegisteredType = ®isteredType{ + CQLType: varintCQLType{}, + str: "varint", + } + + listRegisteredType = ®isteredType{ + CQLType: listSetCQLType{ + typ: TypeList, + }, + str: "list", + } + + mapRegisteredType = ®isteredType{ + CQLType: mapCQLType{}, + str: "map", + } + + setRegisteredType = ®isteredType{ + CQLType: listSetCQLType{ + typ: TypeSet, + }, + str: "set", + } + + tupleRegisteredType = ®isteredType{ + CQLType: tupleCQLType{}, + str: "tuple", + } + + udtRegisteredType = ®isteredType{ + CQLType: udtCQLType{}, + str: "udt", + } + + customRegisteredType = ®isteredType{ + CQLType: customCQLType{}, + str: "custom", + } +) + +var registeredTypes = map[Type]*registeredType{ + TypeAscii: asciiRegisteredType, + TypeBigInt: bigIntRegisteredType, + TypeBlob: blobRegisteredType, + TypeBoolean: booleanRegisteredType, + TypeCounter: counterRegisteredType, + TypeDate: dateRegisteredType, + TypeDecimal: decimalRegisteredType, + TypeDouble: doubleRegisteredType, + TypeDuration: durationRegisteredType, + TypeFloat: floatRegisteredType, + TypeInet: inetRegisteredType, + TypeInt: intRegisteredType, + TypeSmallInt: smallintRegisteredType, + TypeText: textRegisteredType, + TypeTime: timeRegisteredType, + TypeTimestamp: timestampRegisteredType, + TypeTimeUUID: timeUUIDRegisteredType, + TypeTinyInt: tinyIntRegisteredType, + TypeUUID: uuidRegisteredType, + TypeVarchar: varcharRegisteredType, + TypeVarint: varintRegisteredType, + TypeList: listRegisteredType, + TypeMap: mapRegisteredType, + TypeSet: setRegisteredType, + TypeTuple: tupleRegisteredType, + TypeUDT: udtRegisteredType, + TypeCustom: customRegisteredType, +} + +// fastRegisteredTypeLookup is a fast lookup for the registered type that avoids +// the need for a map lookup which was shown to be significant +// in cases where it's necessary you should consider manually inlining this method +func fastRegisteredTypeLookup(typ Type) CQLType { + switch typ { + case TypeAscii: + return asciiRegisteredType.CQLType + case TypeBigInt: + return bigIntRegisteredType.CQLType + case TypeBlob: + return blobRegisteredType.CQLType + case TypeBoolean: + return booleanRegisteredType.CQLType + case TypeCounter: + return counterRegisteredType.CQLType + case TypeDate: + return dateRegisteredType.CQLType + case TypeDecimal: + return decimalRegisteredType.CQLType + case TypeDouble: + return doubleRegisteredType.CQLType + case TypeDuration: + return durationRegisteredType.CQLType + case TypeFloat: + return floatRegisteredType.CQLType + case TypeInet: + return inetRegisteredType.CQLType + case TypeInt: + return intRegisteredType.CQLType + case TypeSmallInt: + return smallintRegisteredType.CQLType + case TypeText: + return textRegisteredType.CQLType + case TypeTime: + return timeRegisteredType.CQLType + case TypeTimestamp: + return timestampRegisteredType.CQLType + case TypeTimeUUID: + return timeUUIDRegisteredType.CQLType + case TypeTinyInt: + return tinyIntRegisteredType.CQLType + case TypeUUID: + return uuidRegisteredType.CQLType + case TypeVarchar: + return varcharRegisteredType.CQLType + case TypeVarint: + return varintRegisteredType.CQLType + case TypeList: + return listRegisteredType.CQLType + case TypeMap: + return mapRegisteredType.CQLType + case TypeSet: + return setRegisteredType.CQLType + case TypeTuple: + return tupleRegisteredType.CQLType + case TypeUDT: + return udtRegisteredType.CQLType + case TypeCustom: + return customRegisteredType.CQLType + default: + rt, ok := registeredTypes[typ] + if !ok { + return nil + } + return rt.CQLType + } +} + +var registeredTypesByString = map[string]Type{ + "ascii": TypeAscii, + "bigint": TypeBigInt, + "blob": TypeBlob, + "boolean": TypeBoolean, + "counter": TypeCounter, + "date": TypeDate, + "decimal": TypeDecimal, + "double": TypeDouble, + "duration": TypeDuration, + "float": TypeFloat, + "inet": TypeInet, + "int": TypeInt, + "smallint": TypeSmallInt, + "text": TypeText, + "time": TypeTime, + "timestamp": TypeTimestamp, + "timeuuid": TypeTimeUUID, + "tinyint": TypeTinyInt, + "uuid": TypeUUID, + "varchar": TypeVarchar, + "varint": TypeVarint, + "map": TypeMap, + "list": TypeList, + "set": TypeSet, + "tuple": TypeTuple, + + // these are all for Cassandra 2.x and older + "AsciiType": TypeAscii, + "LongType": TypeBigInt, + "BytesType": TypeBlob, + "BooleanType": TypeBoolean, + "CounterColumnType": TypeCounter, + "DecimalType": TypeDecimal, + "DoubleType": TypeDouble, + "FloatType": TypeFloat, + "Int32Type": TypeInt, + "ShortType": TypeSmallInt, + "ByteType": TypeTinyInt, + "TimeType": TypeTime, + "DateType": TypeTimestamp, // DateType was a timestamp when date didn't exist + "TimestampType": TypeTimestamp, + "UUIDType": TypeUUID, + "LexicalUUIDType": TypeUUID, + "UTF8Type": TypeVarchar, + "IntegerType": TypeVarint, + "TimeUUIDType": TypeTimeUUID, + "InetAddressType": TypeInet, + "MapType": TypeMap, + "ListType": TypeList, + "SetType": TypeSet, + "TupleType": TypeTuple, +} + +type registeredType struct { + CQLType + str string +} + +// RegisterType registers a new CQL data type. Type should be the CQL id for +// the type. Name is the name of the type as returned in the metadata for the +// column. CQLType is the implementation of the type. +// This function is not goroutine-safe and must be called before any other usage +// of the gocql package. Typically this should be called in an init function. +func RegisterType(typ Type, name string, t CQLType) { Review Comment: > I don't think it would be very common for people to have different handling for different types in different clusters. For basic native types maybe not but eventually this API will evolve to support UDTs and custom types and with these types I can definitely see a use case for having separate type registrations on the same app. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org