Skip to content

Commit d60f097

Browse files
new universal get helper functions for repacking and other cases
1 parent 7b950b5 commit d60f097

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

access/get.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func (g *GetAccess) GetCopyBytes(pos int) ([]byte, error) {
352352
// Create an independent copy to break the reference to g.buf
353353
cp := make([]byte, len(data))
354354
copy(cp, data)
355-
355+
356356
return cp, nil
357357
}
358358

@@ -499,3 +499,39 @@ func (g *GetAccess) GetNestedGetAccess(pos int) (*GetAccess, typetags.Type, erro
499499
}
500500
return NewGetAccess(g.buf[start:end]), tp, nil
501501
}
502+
503+
// function to get type and value, which can be used for repacking or other purposes
504+
func (g *GetAccess) GetTypeAndValue(pos int) (typetags.Type, []byte) {
505+
tp, start, end := g.rangeAt(pos)
506+
if end < start {
507+
return typetags.TypeInvalid, nil
508+
}
509+
return tp, g.buf[start:end]
510+
}
511+
512+
type PackableTagValue struct {
513+
head typetags.Type
514+
value []byte
515+
}
516+
517+
func (px *PackableTagValue) HeaderType() typetags.Type {
518+
return px.head
519+
}
520+
func (px *PackableTagValue) ValueSize() int {
521+
return len(px.value)
522+
}
523+
func (px *PackableTagValue) Write(buf []byte, pos int) int {
524+
return WriteBytes(buf, pos, px.value)
525+
}
526+
527+
func (px *PackableTagValue) PackInto(p *PutAccess) {
528+
p.AppendTagAndValue(px.head, px.value)
529+
}
530+
531+
func (g *GetAccess) GetAsPackable(pos int) (Packable, error) {
532+
tp, value := g.GetTypeAndValue(pos)
533+
if value == nil && tp == typetags.TypeInvalid {
534+
return nil, fmt.Errorf("invalid")
535+
}
536+
return &PackableTagValue{head: tp, value: value}, nil
537+
}

0 commit comments

Comments
 (0)