|
|
@ -1,6 +1,10 @@
|
|
|
|
package operation
|
|
|
|
package operation
|
|
|
|
|
|
|
|
|
|
|
|
import "github.com/aziis98/cabret"
|
|
|
|
import (
|
|
|
|
|
|
|
|
"log"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/aziis98/cabret"
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
func init() {
|
|
|
|
registerType("chunk", &Chunk{})
|
|
|
|
registerType("chunk", &Chunk{})
|
|
|
@ -8,13 +12,13 @@ func init() {
|
|
|
|
|
|
|
|
|
|
|
|
// Chunk is a list operation that will group incoming items in groups of the given size
|
|
|
|
// Chunk is a list operation that will group incoming items in groups of the given size
|
|
|
|
type Chunk struct {
|
|
|
|
type Chunk struct {
|
|
|
|
Count int
|
|
|
|
Size int
|
|
|
|
SkipRemaining bool
|
|
|
|
SkipRemaining bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (op *Chunk) Configure(options map[string]any) error {
|
|
|
|
func (op *Chunk) Configure(options map[string]any) error {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
op.Count, err = getKey[int](options, "size")
|
|
|
|
op.Size, err = getKey[int](options, "size")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -27,16 +31,29 @@ func (op *Chunk) Configure(options map[string]any) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (op *Chunk) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
|
|
|
|
func (op *Chunk) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
|
|
|
|
totalPages := len(items) / op.Count
|
|
|
|
totalChunks := len(items) / op.Size
|
|
|
|
|
|
|
|
|
|
|
|
chunks := make([][]cabret.Content, totalPages, totalPages+1)
|
|
|
|
chunks := make([][]cabret.Content, totalChunks, totalChunks+1)
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < totalPages; i++ {
|
|
|
|
for i := 0; i < totalChunks; i++ {
|
|
|
|
chunks = append(chunks, items[i*op.Count:(i+1)*op.Count])
|
|
|
|
chunks = append(chunks, items[i*op.Size:(i+1)*op.Size])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !op.SkipRemaining {
|
|
|
|
if !op.SkipRemaining {
|
|
|
|
chunks = append(chunks, items[totalPages*op.Count:])
|
|
|
|
if len(items)%op.Size != 0 {
|
|
|
|
|
|
|
|
lastExtraChunk := items[totalChunks*op.Size:]
|
|
|
|
|
|
|
|
chunks = append(chunks, lastExtraChunk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(lastExtraChunk) != len(items)%op.Size {
|
|
|
|
|
|
|
|
panic("ehm last chunk and modulus should be equal")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf(`[operation.Chunk] chunked items in %d chunk(s), last with %d items`, totalChunks+1, len(lastExtraChunk))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.Printf(`[operation.Chunk] chunked items in %d chunk(s)`, totalChunks)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.Printf(`[operation.Chunk] chunked items in %d chunk(s)`, totalChunks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result := make([]cabret.Content, len(chunks))
|
|
|
|
result := make([]cabret.Content, len(chunks))
|
|
|
@ -44,8 +61,8 @@ func (op *Chunk) ProcessList(items []cabret.Content) ([]cabret.Content, error) {
|
|
|
|
result[i] = cabret.Content{
|
|
|
|
result[i] = cabret.Content{
|
|
|
|
Type: cabret.MetadataOnly,
|
|
|
|
Type: cabret.MetadataOnly,
|
|
|
|
Metadata: cabret.Map{
|
|
|
|
Metadata: cabret.Map{
|
|
|
|
"Page": i + 1,
|
|
|
|
"Index": i,
|
|
|
|
"TotalPages": totalPages,
|
|
|
|
"Total": totalChunks,
|
|
|
|
"Items": chunk,
|
|
|
|
"Items": chunk,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|