Skip to content

Commit

Permalink
Add ability to slice sets (#4939)
Browse files Browse the repository at this point in the history
Closes #4938
  • Loading branch information
mattnibs authored Dec 18, 2023
1 parent 59b72d8 commit b7d3b41
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 3 additions & 3 deletions runtime/expr/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ func (s *Slice) Eval(ectx Context, this *zed.Value) *zed.Value {
length = len(elem.Bytes())
case *zed.TypeOfString:
length = utf8.RuneCount(elem.Bytes())
case *zed.TypeArray:
case *zed.TypeArray, *zed.TypeSet:
n, err := elem.ContainerLength()
if err != nil {
panic(err)
}
length = n
default:
return s.zctx.WrapError("sliced value is not array, bytes, or string", elem)
return s.zctx.WrapError("sliced value is not array, set, bytes, or string", elem)
}
if elem.IsNull() {
return elem
Expand All @@ -69,7 +69,7 @@ func (s *Slice) Eval(ectx Context, this *zed.Value) *zed.Value {
case *zed.TypeOfString:
bytes = bytes[utf8PrefixLen(bytes, from):]
bytes = bytes[:utf8PrefixLen(bytes, to-from)]
case *zed.TypeArray:
case *zed.TypeArray, *zed.TypeSet:
it := bytes.Iter()
for k := 0; k < to && !it.Done(); k++ {
if k == from {
Expand Down
4 changes: 3 additions & 1 deletion runtime/expr/ztests/slice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ input: |
{a:"0\u2071\u20723"}
{a:"\u2070\u2071\u2072\u2073"}
{a:[10(int32),11(int32),12(int32),13(int32)]}
{a:|[10(int32),11(int32),12(int32),13(int32)]|}
output: |
{a1:error({message:"sliced value is not array, bytes, or string",on:null}),a2:error({message:"sliced value is not array, bytes, or string",on:null}),a3:error({message:"sliced value is not array, bytes, or string",on:null}),a4:error({message:"sliced value is not array, bytes, or string",on:null}),a5:error({message:"sliced value is not array, bytes, or string",on:null}),a6:error({message:"sliced value is not array, bytes, or string",on:null}),a7:error({message:"sliced value is not array, bytes, or string",on:null}),a8:null}
{a1:error({message:"sliced value is not array, set, bytes, or string",on:null}),a2:error({message:"sliced value is not array, set, bytes, or string",on:null}),a3:error({message:"sliced value is not array, set, bytes, or string",on:null}),a4:error({message:"sliced value is not array, set, bytes, or string",on:null}),a5:error({message:"sliced value is not array, set, bytes, or string",on:null}),a6:error({message:"sliced value is not array, set, bytes, or string",on:null}),a7:error({message:"sliced value is not array, set, bytes, or string",on:null}),a8:null}
{a1:null(bytes),a2:null(bytes),a3:null(bytes),a4:null(bytes),a5:null(bytes),a6:null(bytes),a7:null(bytes),a8:null}
{a1:null(string),a2:null(string),a3:null(string),a4:null(string),a5:null(string),a6:null(string),a7:null(string),a8:null}
{a1:null([int32]),a2:null([int32]),a3:null([int32]),a4:null([int32]),a5:null([int32]),a6:null([int32]),a7:null([int32]),a8:null}
Expand All @@ -27,3 +28,4 @@ output: |
{a1:"ⁱ⁲",a2:"ⁱ⁲3",a3:"0",a4:"0ⁱ⁲",a5:"",a6:"3",a7:"⁲",a8:error("slice index is not a number")}
{a1:"ⁱ⁲",a2:"ⁱ⁲⁳",a3:"⁰",a4:"⁰ⁱ⁲",a5:"",a6:"⁳",a7:"⁲",a8:error("slice index is not a number")}
{a1:[11(int32),12(int32)],a2:[11(int32),12(int32),13(int32)],a3:[10(int32)],a4:[10(int32),11(int32),12(int32)],a5:[]([int32]),a6:[13(int32)],a7:[12(int32)],a8:[10(int32),11(int32)]}
{a1:|[11(int32),12(int32)]|,a2:|[11(int32),12(int32),13(int32)]|,a3:|[10(int32)]|,a4:|[10(int32),11(int32),12(int32)]|,a5:|[]|(|[int32]|),a6:|[13(int32)]|,a7:|[12(int32)]|,a8:|[10(int32),11(int32)]|}

0 comments on commit b7d3b41

Please sign in to comment.