Skip to content

Commit

Permalink
Apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-antoniak committed Oct 30, 2024
1 parent 24daa93 commit ae2d2e4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
31 changes: 9 additions & 22 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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),
}
}

Expand All @@ -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),
Expand All @@ -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<text, text>) 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)
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit ae2d2e4

Please sign in to comment.