1
0
mirror of https://github.com/uptrace/go-clickhouse.git synced 2025-06-23 00:07:41 +02:00
Files
.github
ch
chpool
chproto
chschema
chtype
internal
testdata
bench_test.go
ch.go
config.go
db.go
db_test.go
hook.go
model.go
model_map.go
model_scan.go
model_slice_map.go
model_table_slice.go
model_table_struct.go
proto.go
query_base.go
query_insert.go
query_select.go
query_table_create.go
query_table_drop.go
query_table_truncate.go
query_test.go
reflect.go
chdebug
chmigrate
chotel
example
scripts
.prettierrc.yml
CHANGELOG.md
LICENSE
Makefile
README.md
go.mod
go.sum
package.json
version.go
go-clickhouse/ch/model_slice_map.go

58 lines
1.0 KiB
Go
Raw Normal View History

2022-01-23 09:36:24 +02:00
package ch
import (
"reflect"
"strings"
"github.com/uptrace/go-clickhouse/ch/chschema"
)
type sliceMapModel struct {
v reflect.Value
slice []map[string]any
}
var _ Model = (*sliceMapModel)(nil)
func newSliceMapModel(v reflect.Value) *sliceMapModel {
return &sliceMapModel{
v: v,
slice: v.Interface().([]map[string]any),
}
}
func (m *sliceMapModel) ScanBlock(block *chschema.Block) error {
for i := 0; i < block.NumRow; i++ {
row := make(map[string]any, block.NumColumn)
for _, col := range block.Columns {
set(row, col.Name, col.Index(i))
}
m.slice = append(m.slice, row)
}
m.v.Set(reflect.ValueOf(m.slice))
return nil
}
func set(m map[string]any, key string, value any) {
const sep = "__"
for {
idx := strings.Index(key, sep)
if idx == -1 {
break
}
subKey := key[:idx]
key = key[idx+len(sep):]
if subMap, ok := m[subKey].(map[string]any); ok {
m = subMap
continue
}
subMap := make(map[string]any)
m[subKey] = subMap
m = subMap
}
m[key] = value
}