joao-r-reis commented on code in PR #1855: URL: https://github.com/apache/cassandra-gocql-driver/pull/1855#discussion_r2026776611
########## types.go: ########## @@ -0,0 +1,476 @@ +/* + * 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" + "strings" + "sync" +) + +// CQLType is the interface that must be implemented by all registered types. +// For simple types, you can just wrap a TypeInfo with SimpleCQLType. +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(). + // This will not be called for custom types. + 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 +} + +// SimpleCQLType is a convenience wrapper around a TypeInfo that implements +// CQLType by returning nil for Params, and the TypeInfo for TypeInfoFromParams +// and TypeInfoFromString. +type SimpleCQLType struct { + TypeInfo +} + +// Params returns nil. +func (SimpleCQLType) Params(proto int) []reflect.Type { + return nil +} + +// TypeInfoFromParams returns the wrapped TypeInfo. +func (s SimpleCQLType) TypeInfoFromParams(proto int, params []interface{}) TypeInfo { + return s.TypeInfo +} + +// TypeInfoFromString returns the wrapped TypeInfo. +func (s SimpleCQLType) TypeInfoFromString(proto int, name string) TypeInfo { + return s.TypeInfo +} + +// TypeInfo describes a Cassandra specific data type and handles marshalling +// and unmarshalling. +type TypeInfo interface { + // Type returns the Type id for the TypeInfo. + Type() Type + + // TODO: should we add String() back? + + // Marshal should marshal the value for the given TypeInfo into a byte slice + Marshal(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(data []byte, value interface{}) error +} + +type RegisteredTypes struct { + byType map[Type]CQLType + simples map[Type]TypeInfo + byString map[string]Type + custom map[string]CQLType + + // this mutex is only used for registration and after that it's assumed that + // the types are immutable + mut sync.Mutex + initialized sync.Once +} + +func (r *RegisteredTypes) init() { + r.initialized.Do(func() { + if r.byType == nil { + r.byType = map[Type]CQLType{} + } + if r.simples == nil { + r.simples = map[Type]TypeInfo{} + } + if r.byString == nil { + r.byString = map[string]Type{} + } + if r.custom == nil { + r.custom = map[string]CQLType{} + } + + r.registerType(TypeAscii, "ascii", SimpleCQLType{varcharLikeCQLType{ + typ: TypeAscii, + }}) + r.registerType(TypeBigInt, "bigint", SimpleCQLType{bigIntLikeCQLType{ + typ: TypeBigInt, + }}) + r.registerType(TypeBlob, "blob", SimpleCQLType{varcharLikeCQLType{ + typ: TypeBlob, + }}) + r.registerType(TypeBoolean, "boolean", SimpleCQLType{booleanCQLType{}}) + r.registerType(TypeCounter, "counter", SimpleCQLType{bigIntLikeCQLType{ + typ: TypeCounter, + }}) + r.registerType(TypeDate, "date", SimpleCQLType{dateCQLType{}}) + r.registerType(TypeDecimal, "decimal", SimpleCQLType{decimalCQLType{}}) + r.registerType(TypeDouble, "double", SimpleCQLType{doubleCQLType{}}) + r.registerType(TypeDuration, "duration", SimpleCQLType{durationCQLType{}}) + r.registerType(TypeFloat, "float", SimpleCQLType{floatCQLType{}}) + r.registerType(TypeInet, "inet", SimpleCQLType{inetCQLType{}}) + r.registerType(TypeInt, "int", SimpleCQLType{intCQLType{}}) + r.registerType(TypeSmallInt, "smallint", SimpleCQLType{smallIntCQLType{}}) + r.registerType(TypeText, "text", SimpleCQLType{varcharLikeCQLType{ + typ: TypeText, + }}) + r.registerType(TypeTime, "time", SimpleCQLType{timeCQLType{}}) + r.registerType(TypeTimestamp, "timestamp", SimpleCQLType{timestampCQLType{}}) + r.registerType(TypeTimeUUID, "timeuuid", SimpleCQLType{timeUUIDCQLType{}}) + r.registerType(TypeTinyInt, "tinyint", SimpleCQLType{tinyIntCQLType{}}) + r.registerType(TypeUUID, "uuid", SimpleCQLType{uuidCQLType{}}) + r.registerType(TypeVarchar, "varchar", SimpleCQLType{varcharLikeCQLType{ + typ: TypeVarchar, + }}) + r.registerType(TypeVarint, "varint", SimpleCQLType{varintCQLType{}}) + r.registerType(TypeUDT, "udt", udtCQLType{}) + + // these types need references to the registered types + r.registerType(TypeList, "list", listSetCQLType{ + typ: TypeList, + types: r, + }) + r.registerType(TypeSet, "set", listSetCQLType{ + typ: TypeSet, + types: r, + }) + r.registerType(TypeMap, "map", mapCQLType{ + types: r, + }) + r.registerType(TypeTuple, "tuple", tupleCQLType{ + types: r, + }) + + // these are all for Cassandra 2.x and older + r.byString["AsciiType"] = TypeAscii + r.byString["LongType"] = TypeBigInt + r.byString["BytesType"] = TypeBlob + r.byString["BooleanType"] = TypeBoolean + r.byString["CounterColumnType"] = TypeCounter + r.byString["DecimalType"] = TypeDecimal + r.byString["DoubleType"] = TypeDouble + r.byString["FloatType"] = TypeFloat + r.byString["InetAddressType"] = TypeInet + r.byString["Int32Type"] = TypeInt + r.byString["ShortType"] = TypeSmallInt + r.byString["UTF8Type"] = TypeVarchar + r.byString["TimeType"] = TypeTime + r.byString["TimestampType"] = TypeTimestamp + // DateType was a timestamp when date didn't exist + r.byString["DateType"] = TypeTimestamp + r.byString["TimeUUIDType"] = TypeTimeUUID + r.byString["ByteType"] = TypeTinyInt + r.byString["UUIDType"] = TypeUUID + r.byString["LexicalUUIDType"] = TypeUUID + r.byString["IntegerType"] = TypeVarint + r.byString["UserType"] = TypeUDT + r.byString["ListType"] = TypeList + r.byString["MapType"] = TypeMap + r.byString["SetType"] = TypeSet + r.byString["TupleType"] = TypeTuple + }) +} + +// 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 must not be called after a session has been created. +func (r *RegisteredTypes) RegisterType(typ Type, name string, t CQLType) { + r.init() + r.mut.Lock() + defer r.mut.Unlock() + r.registerType(typ, name, t) +} + +func (r *RegisteredTypes) registerType(typ Type, name string, t CQLType) { + if typ == TypeCustom { + panic("custom types must be registered with RegisterCustom") + } + + if _, ok := r.byType[typ]; ok { + panic(fmt.Errorf("type %d already registered", typ)) + } + if _, ok := r.byString[name]; ok { + panic(fmt.Errorf("type name %s already registered", name)) Review Comment: should probably return errors instead of panic -- 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