jameshartig commented on code in PR #1855:
URL:
https://github.com/apache/cassandra-gocql-driver/pull/1855#discussion_r2127467713
##########
frame.go:
##########
@@ -847,84 +850,96 @@ func (w *writePrepareFrame) buildFrame(f *framer,
streamID int) error {
return f.finish()
}
-func (f *framer) readTypeInfo() TypeInfo {
- // TODO: factor this out so the same code paths can be used to parse
custom
- // types and other types, as much of the logic will be duplicated.
- id := f.readShort()
-
- simple := NativeType{
- proto: f.proto,
- typ: Type(id),
- }
-
- if simple.typ == TypeCustom {
- simple.custom = f.readString()
- if cassType := getApacheCassandraType(simple.custom); cassType
!= TypeCustom {
- simple.typ = cassType
- }
- }
+var (
+ typeInfoType = reflect.TypeOf((*TypeInfo)(nil)).Elem()
+ typeType = reflect.TypeOf(Type(0))
+ typeInfoSliceType = reflect.TypeOf([]TypeInfo(nil))
+ stringSliceType = reflect.TypeOf([]string(nil))
+ udtFieldSliceType = reflect.TypeOf([]UDTField(nil))
+ stringType = reflect.TypeOf("")
+ shortType = reflect.TypeOf(uint16(0))
+ byteType = reflect.TypeOf(byte(0))
+ intType = reflect.TypeOf(int(0))
+)
- switch simple.typ {
- case TypeTuple:
+func (f *framer) readForType(typ reflect.Type) interface{} {
+ // check simple equality first
+ switch typ {
+ case stringType:
+ return f.readString()
+ case shortType:
+ return f.readShort()
+ case byteType:
+ return f.readByte()
+ case intType:
+ return f.readInt()
+ case stringSliceType:
+ return f.readStringList()
+ case udtFieldSliceType:
n := f.readShort()
- tuple := TupleTypeInfo{
- NativeType: simple,
- Elems: make([]TypeInfo, n),
- }
-
+ fields := make([]UDTField, n)
for i := 0; i < int(n); i++ {
- tuple.Elems[i] = f.readTypeInfo()
+ fields[i] = UDTField{
+ Name: f.readString(),
+ Type: f.readTypeInfo(),
+ }
}
-
- return tuple
-
- case TypeUDT:
- udt := UDTTypeInfo{
- NativeType: simple,
+ return fields
+ case typeInfoType:
+ return f.readTypeInfo()
+ case typeType:
+ return Type(f.readShort())
+ case typeInfoSliceType:
+ n := f.readShort()
+ types := make([]TypeInfo, n)
+ for i := 0; i < int(n); i++ {
+ types[i] = f.readTypeInfo()
}
- udt.KeySpace = f.readString()
- udt.Name = f.readString()
+ return types
+ }
+ // then check the kind and try to convert
+ switch typ.Kind() {
+ case reflect.String:
+ return reflect.ValueOf(f.readString()).Convert(typ).Interface()
+ case reflect.Int:
+ return reflect.ValueOf(f.readInt()).Convert(typ).Interface()
+ case reflect.Slice:
n := f.readShort()
- udt.Elements = make([]UDTField, n)
+ slice := reflect.MakeSlice(typ, int(n), int(n))
for i := 0; i < int(n); i++ {
- field := &udt.Elements[i]
- field.Name = f.readString()
- field.Type = f.readTypeInfo()
+
slice.Index(i).Set(reflect.ValueOf(f.readForType(typ.Elem())))
}
+ return slice.Interface()
+ }
+ panic(fmt.Errorf("unsupported type for reading from frame: %s",
typ.String()))
+}
- return udt
- case TypeMap, TypeList, TypeSet:
- collection := CollectionType{
- NativeType: simple,
- }
+func (f *framer) readTypeInfo() TypeInfo {
+ typ := Type(f.readShort())
+ if typ == TypeCustom {
+ name := f.readString()
+ return f.types.typeInfoFromString(int(f.proto), name)
+ }
- if simple.typ == TypeMap {
- collection.Key = f.readTypeInfo()
- }
+ if ti := f.types.fastTypeInfoLookup(typ); ti != nil {
+ return ti
+ }
- collection.Elem = f.readTypeInfo()
-
- return collection
- case TypeCustom:
- if strings.HasPrefix(simple.custom, VECTOR_TYPE) {
- spec := strings.TrimPrefix(simple.custom, VECTOR_TYPE)
- spec = spec[1 : len(spec)-1] // remove parenthesis
- idx := strings.LastIndex(spec, ",")
- typeStr := spec[:idx]
- dimStr := spec[idx+1:]
- subType :=
getCassandraLongType(strings.TrimSpace(typeStr), f.proto, nopLogger{})
- dim, _ := strconv.Atoi(strings.TrimSpace(dimStr))
- vector := VectorType{
- NativeType: simple,
- SubType: subType,
- Dimensions: dim,
- }
- return vector
- }
+ cqlt, ok := f.types.byType[typ]
+ if !ok {
+ panic(fmt.Errorf("unknown type id: %d", typ))
Review Comment:
Fair point, I'll add a new unknownType type for this and then try to write
some tests around this.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]