Tang8330 commented on issue #1909:
URL:
https://github.com/apache/cassandra-gocql-driver/issues/1909#issuecomment-3277174808
I ended up writing an unfold method,
```go
package cassandra
import (
"fmt"
"regexp"
"strconv"
"strings"
)
func FoldTuples(row map[string]any, col string) (map[string]any, error) {
re, err := regexp.Compile(fmt.Sprintf(`^(%s)\[(\d+)\]$`, col))
if err != nil {
return nil, fmt.Errorf("failed to compile regex pattern: %w",
err)
}
foldedParts := make(map[int]any)
out := make(map[string]any)
for key, value := range row {
if strings.HasPrefix(key, col) && key != col {
parts := re.FindStringSubmatch(key)
if len(parts) != 3 {
return nil, fmt.Errorf("expected 2 parts got %d
(%s) for key: %q", len(parts), strings.Join(parts, ","), key)
}
// Skip the first part, since it'll be the whole match.
index, err := strconv.Atoi(parts[2])
if err != nil {
return nil, fmt.Errorf("failed to convert index
to int: %w", err)
}
if _, ok := foldedParts[index]; ok {
return nil, fmt.Errorf("duplicate index found
for key: %q", key)
}
foldedParts[index] = value
} else {
out[key] = value
}
}
if len(foldedParts) == 0 {
return out, nil
}
packedValue := make(map[string]any)
// Else, now let's pack all of the folded parts into the tuple.
for i := range len(foldedParts) {
// This is done to be compatible with Debezium, where it marks
the first value as "field1".
value, ok := foldedParts[i]
if !ok {
return nil, fmt.Errorf("expected value for index %d but
got none", i)
}
packedValue[fmt.Sprintf("field%d", i+1)] = value
}
out[col] = packedValue
return out, nil
}
```
It'd be great if this wasn't required.
--
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]