Skip to content

Commit

Permalink
handle String Slice selector
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobozzo committed Sep 13, 2024
1 parent dbfff3f commit a183b62
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions capability/policy/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,14 @@ func resolve(sel Selector, subject ipld.Node, at []string) (ipld.Node, []ipld.No
case datamodel.Kind_Bytes:
b, _ := cur.AsBytes()
start, end = resolveSliceIndices(slice, int64(len(b)))
case datamodel.Kind_String:
str, _ := cur.AsString()
start, end = resolveSliceIndices(slice, int64(len(str)))
default:
return nil, nil, newResolutionError(fmt.Sprintf("can not slice on kind: %s", kindString(cur)), at)
}

if start < 0 || end < start || end > cur.Length() {
if start < 0 || end < start {
if seg.Optional() {
cur = nil
} else {
Expand All @@ -203,21 +206,31 @@ func resolve(sel Selector, subject ipld.Node, at []string) (ipld.Node, []ipld.No
} else {
switch cur.Kind() {
case datamodel.Kind_List:
if end > cur.Length() {
end = cur.Length()
}
nb := basicnode.Prototype.List.NewBuilder()
assembler, _ := nb.BeginList(end - start)
assembler, _ := nb.BeginList(int64(end - start))
for i := start; i < end; i++ {
item, _ := cur.LookupByIndex(i)
if err := assembler.AssembleValue().AssignNode(item); err != nil {
return nil, nil, err
}
}
if err := assembler.Finish(); err != nil {
return nil, nil, err
item, _ := cur.LookupByIndex(int64(i))
assembler.AssembleValue().AssignNode(item)
}
assembler.Finish()
cur = nb.Build()
case datamodel.Kind_Bytes:
b, _ := cur.AsBytes()
l := int64(len(b))
if end > l {
end = l
}
cur = basicnode.NewBytes(b[start:end])
case datamodel.Kind_String:
str, _ := cur.AsString()
l := int64(len(str))
if end > l {
end = l
}
cur = basicnode.NewString(str[start:end])
}
}
}
Expand Down

0 comments on commit a183b62

Please sign in to comment.