|
7 | 7 | "errors" |
8 | 8 | "math/rand" |
9 | 9 | "strconv" |
| 10 | + "strings" |
10 | 11 |
|
11 | 12 | "go.mondoo.com/cnquery/v12/types" |
12 | 13 | "go.mondoo.com/cnquery/v12/utils/multierr" |
@@ -506,6 +507,63 @@ func arrayFlat(e *blockExecutor, bind *RawData, chunk *Chunk, ref uint64) (*RawD |
506 | 507 | return &RawData{Type: types.Array(typ), Value: res}, 0, nil |
507 | 508 | } |
508 | 509 |
|
| 510 | +func arrayReverse(e *blockExecutor, bind *RawData, chunk *Chunk, ref uint64) (*RawData, uint64, error) { |
| 511 | + if bind.Value == nil { |
| 512 | + return &RawData{Type: bind.Type, Error: bind.Error}, 0, nil |
| 513 | + } |
| 514 | + |
| 515 | + list, ok := bind.Value.([]any) |
| 516 | + // this should not happen at this point |
| 517 | + if !ok { |
| 518 | + return &RawData{Type: bind.Type, Error: errors.New("incorrect type, no array data found")}, 0, nil |
| 519 | + } |
| 520 | + |
| 521 | + res := make([]any, len(list)) |
| 522 | + for i := range list { |
| 523 | + res[len(res)-1-i] = list[i] |
| 524 | + } |
| 525 | + |
| 526 | + return &RawData{Type: bind.Type, Value: res}, 0, nil |
| 527 | +} |
| 528 | + |
| 529 | +func arrayJoin(e *blockExecutor, bind *RawData, chunk *Chunk, ref uint64) (*RawData, uint64, error) { |
| 530 | + if bind.Value == nil { |
| 531 | + return &RawData{Type: bind.Type, Error: bind.Error}, 0, nil |
| 532 | + } |
| 533 | + |
| 534 | + list, ok := bind.Value.([]any) |
| 535 | + // this should not happen at this point |
| 536 | + if !ok { |
| 537 | + return &RawData{Type: bind.Type, Error: errors.New("incorrect type, no array data found")}, 0, nil |
| 538 | + } |
| 539 | + |
| 540 | + var sep string |
| 541 | + if len(chunk.Function.Args) == 1 { |
| 542 | + item, rref, err := e.resolveValue(chunk.Function.Args[0], ref) |
| 543 | + if err != nil || rref > 0 { |
| 544 | + return nil, rref, err |
| 545 | + } |
| 546 | + sep, ok = item.Value.(string) |
| 547 | + if !ok { |
| 548 | + return nil, rref, errors.New("separator provided to join() must be a string") |
| 549 | + } |
| 550 | + } |
| 551 | + |
| 552 | + var res strings.Builder |
| 553 | + for i := range list { |
| 554 | + s, ok := list[i].(string) |
| 555 | + if !ok { |
| 556 | + return nil, 0, errors.New("failed to join() elements, must be strings") |
| 557 | + } |
| 558 | + if sep != "" && i > 0 { |
| 559 | + res.WriteString(sep) |
| 560 | + } |
| 561 | + res.WriteString(s) |
| 562 | + } |
| 563 | + |
| 564 | + return &RawData{Type: types.String, Value: res.String()}, 0, nil |
| 565 | +} |
| 566 | + |
509 | 567 | // Take an array and separate it into a list of unique entries and another |
510 | 568 | // list of only duplicates. The latter list only has every entry appear only |
511 | 569 | // once. |
|
0 commit comments