Skip to content

Commit 799fb03

Browse files
beltranZariel
authored andcommitted
Fix metadata tuple parsing (#1190)
* Fix metadata tuple parsing * adding failing tests for tuple and map in getCassandraType
1 parent 5a139e8 commit 799fb03

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

helpers.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,20 @@ func getCassandraType(name string) TypeInfo {
132132
Elem: getCassandraType(strings.TrimPrefix(name[:len(name)-1], "list<")),
133133
}
134134
} else if strings.HasPrefix(name, "map<") {
135-
names := strings.SplitN(strings.TrimPrefix(name[:len(name)-1], "map<"), ", ", 2)
135+
names := splitCompositeTypes(strings.TrimPrefix(name[:len(name)-1], "map<"))
136+
if len(names) != 2 {
137+
Logger.Printf("Error parsing map type, it has %d subelements, expecting 2\n", len(names))
138+
return NativeType{
139+
typ: TypeCustom,
140+
}
141+
}
136142
return CollectionType{
137143
NativeType: NativeType{typ: TypeMap},
138144
Key: getCassandraType(names[0]),
139145
Elem: getCassandraType(names[1]),
140146
}
141147
} else if strings.HasPrefix(name, "tuple<") {
142-
names := strings.Split(strings.TrimPrefix(name[:len(name)-1], "tuple<"), ", ")
148+
names := splitCompositeTypes(strings.TrimPrefix(name[:len(name)-1], "tuple<"))
143149
types := make([]TypeInfo, len(names))
144150

145151
for i, name := range names {
@@ -157,6 +163,34 @@ func getCassandraType(name string) TypeInfo {
157163
}
158164
}
159165

166+
func splitCompositeTypes(name string) []string {
167+
if !strings.Contains(name, "<") {
168+
return strings.Split(name, ", ")
169+
}
170+
var parts []string
171+
lessCount := 0
172+
segment := ""
173+
for _, char := range name {
174+
if char == ',' && lessCount == 0 {
175+
if segment != "" {
176+
parts = append(parts, strings.TrimSpace(segment))
177+
}
178+
segment = ""
179+
continue
180+
}
181+
segment += string(char)
182+
if char == '<' {
183+
lessCount++
184+
} else if char == '>' {
185+
lessCount--
186+
}
187+
}
188+
if segment != "" {
189+
parts = append(parts, strings.TrimSpace(segment))
190+
}
191+
return parts
192+
}
193+
160194
func getApacheCassandraType(class string) Type {
161195
switch strings.TrimPrefix(class, apacheCassandraTypePrefix) {
162196
case "AsciiType":

helpers_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,100 @@ func TestGetCassandraType(t *testing.T) {
7777
},
7878
},
7979
},
80+
{
81+
"frozen<tuple<frozen<tuple<text, frozen<list<frozen<tuple<int, int>>>>>>, frozen<tuple<text, frozen<list<frozen<tuple<int, int>>>>>>, frozen<map<text, frozen<list<frozen<tuple<int, int>>>>>>>>",
82+
TupleTypeInfo{
83+
NativeType: NativeType{typ: TypeTuple},
84+
Elems: []TypeInfo{
85+
TupleTypeInfo{
86+
NativeType: NativeType{typ: TypeTuple},
87+
Elems: []TypeInfo{
88+
NativeType{typ: TypeText},
89+
CollectionType{
90+
NativeType: NativeType{typ: TypeList},
91+
Elem: TupleTypeInfo{
92+
NativeType: NativeType{typ: TypeTuple},
93+
Elems: []TypeInfo{
94+
NativeType{typ: TypeInt},
95+
NativeType{typ: TypeInt},
96+
},
97+
},
98+
},
99+
},
100+
},
101+
TupleTypeInfo{
102+
NativeType: NativeType{typ: TypeTuple},
103+
Elems: []TypeInfo{
104+
NativeType{typ: TypeText},
105+
CollectionType{
106+
NativeType: NativeType{typ: TypeList},
107+
Elem: TupleTypeInfo{
108+
NativeType: NativeType{typ: TypeTuple},
109+
Elems: []TypeInfo{
110+
NativeType{typ: TypeInt},
111+
NativeType{typ: TypeInt},
112+
},
113+
},
114+
},
115+
},
116+
},
117+
CollectionType{
118+
NativeType: NativeType{typ: TypeMap},
119+
Key: NativeType{typ: TypeText},
120+
Elem: CollectionType{
121+
NativeType: NativeType{typ: TypeList},
122+
Elem: TupleTypeInfo{
123+
NativeType: NativeType{typ: TypeTuple},
124+
Elems: []TypeInfo{
125+
NativeType{typ: TypeInt},
126+
NativeType{typ: TypeInt},
127+
},
128+
},
129+
},
130+
},
131+
},
132+
},
133+
},
134+
{
135+
"frozen<tuple<frozen<tuple<int, int>>, int, frozen<tuple<int, int>>>>", TupleTypeInfo{
136+
NativeType: NativeType{typ: TypeTuple},
137+
138+
Elems: []TypeInfo{
139+
TupleTypeInfo{
140+
NativeType: NativeType{typ: TypeTuple},
141+
142+
Elems: []TypeInfo{
143+
NativeType{typ: TypeInt},
144+
NativeType{typ: TypeInt},
145+
},
146+
},
147+
NativeType{typ: TypeInt},
148+
TupleTypeInfo{
149+
NativeType: NativeType{typ: TypeTuple},
150+
151+
Elems: []TypeInfo{
152+
NativeType{typ: TypeInt},
153+
NativeType{typ: TypeInt},
154+
},
155+
},
156+
},
157+
},
158+
},
159+
{
160+
"frozen<map<frozen<tuple<int, int>>, int>>", CollectionType{
161+
NativeType: NativeType{typ: TypeMap},
162+
163+
Key: TupleTypeInfo{
164+
NativeType: NativeType{typ: TypeTuple},
165+
166+
Elems: []TypeInfo{
167+
NativeType{typ: TypeInt},
168+
NativeType{typ: TypeInt},
169+
},
170+
},
171+
Elem: NativeType{typ: TypeInt},
172+
},
173+
},
80174
}
81175

82176
for _, test := range tests {

0 commit comments

Comments
 (0)