From ae2d2e452e1ed2b7ab5b542cc496dd2ca6f0382e Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Wed, 30 Oct 2024 17:04:27 +0100 Subject: [PATCH] Apply review comments --- helpers.go | 31 +++++++++---------------------- metadata.go | 2 +- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/helpers.go b/helpers.go index e7c1084c8..97f96f7de 100644 --- a/helpers.go +++ b/helpers.go @@ -164,7 +164,7 @@ func getCassandraBaseType(name string) Type { } } -// Parse long Java-style type definition to internal data structures. +// Parses long Java-style type definition to internal data structures. func getCassandraLongType(name string, protoVer byte, logger StdLogger) TypeInfo { if strings.HasPrefix(name, SET_TYPE) { return CollectionType{ @@ -179,7 +179,7 @@ func getCassandraLongType(name string, protoVer byte, logger StdLogger) TypeInfo } else if strings.HasPrefix(name, MAP_TYPE) { names := splitJavaCompositeTypes(strings.TrimPrefix(name[:len(name)-1], MAP_TYPE+"(")) if len(names) != 2 { - logger.Printf("Error parsing map type, it has %d subelements, expecting 2\n", len(names)) + logger.Printf("gocql: error parsing map type, it has %d subelements, expecting 2\n", len(names)) return NewNativeType(protoVer, TypeCustom) } return CollectionType{ @@ -208,7 +208,7 @@ func getCassandraLongType(name string, protoVer byte, logger StdLogger) TypeInfo fieldName, _ := hex.DecodeString(spec[0]) fields[i-2] = UDTField{ Name: string(fieldName), - Type: getTypeInfo(spec[1], protoVer, logger), + Type: getCassandraLongType(spec[1], protoVer, logger), } } @@ -222,7 +222,11 @@ func getCassandraLongType(name string, protoVer byte, logger StdLogger) TypeInfo } else if strings.HasPrefix(name, VECTOR_TYPE) { names := splitJavaCompositeTypes(strings.TrimPrefix(name[:len(name)-1], VECTOR_TYPE+"(")) subType := getCassandraLongType(strings.TrimSpace(names[0]), protoVer, logger) - dim, _ := strconv.Atoi(strings.TrimSpace(names[1])) + dim, err := strconv.Atoi(strings.TrimSpace(names[1])) + if err != nil { + logger.Printf("gocql: error parsing vector dimensions: %v\n", err) + return NewNativeType(protoVer, TypeCustom) + } return VectorType{ NativeType: NewCustomType(protoVer, TypeCustom, VECTOR_TYPE), @@ -238,9 +242,7 @@ func getCassandraLongType(name string, protoVer byte, logger StdLogger) TypeInfo } } -// Parses short CQL type representation to internal data structures. -// Mapping of long Java-style type definition into short format is performed in -// apacheToCassandraType function. +// Parses short CQL type representation (e.g. map) to internal data structures. func getCassandraType(name string, protoVer byte, logger StdLogger) TypeInfo { if strings.HasPrefix(name, "frozen<") { return getCassandraType(strings.TrimPrefix(name[:len(name)-1], "frozen<"), protoVer, logger) @@ -335,21 +337,6 @@ func splitCompositeTypes(name string, typeOpen int32, typeClose int32) []string return parts } -// Convert long Java style type definition into the short CQL type names. -func apacheToCassandraType(t string) string { - t = strings.Replace(t, apacheCassandraTypePrefix, "", -1) - t = strings.Replace(t, "(", "<", -1) - t = strings.Replace(t, ")", ">", -1) - types := strings.FieldsFunc(t, func(r rune) bool { - return r == '<' || r == '>' || r == ',' - }) - for _, typ := range types { - t = strings.Replace(t, typ, getApacheCassandraType(typ).String(), -1) - } - // This is done so it exactly matches what Cassandra returns - return strings.Replace(t, ",", ", ", -1) -} - func getApacheCassandraType(class string) Type { switch strings.TrimPrefix(class, apacheCassandraTypePrefix) { case "AsciiType": diff --git a/metadata.go b/metadata.go index cc0f93dc2..2773bae30 100644 --- a/metadata.go +++ b/metadata.go @@ -949,7 +949,7 @@ func getColumnMetadata(session *Session, keyspaceName string) ([]ColumnMetadata, func getTypeInfo(t string, protoVer byte, logger StdLogger) TypeInfo { if strings.HasPrefix(t, apacheCassandraTypePrefix) { - t = apacheToCassandraType(t) + return getCassandraLongType(t, protoVer, logger) } return getCassandraType(t, protoVer, logger) }