Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Context struct {
typedefs *TypeDefs
named map[string]*TypeNamed
stringErr atomic.Pointer[TypeError]
toValue map[Type]scode.Bytes
toValue map[Type][]byte
toType map[string]Type
}

Expand Down Expand Up @@ -390,12 +390,12 @@ func (c *Context) LookupTypeFusion(inner Type) *TypeFusion {
// LookupByValue returns the Type indicated by a binary-serialized type value.
// This provides a means to translate a type-context-independent serialized
// encoding for an arbitrary type into the reciever Context.
func (c *Context) LookupByValue(tv scode.Bytes) (Type, error) {
func (c *Context) LookupByValue(tv []byte) (Type, error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.toType == nil {
c.toType = make(map[string]Type)
c.toValue = make(map[Type]scode.Bytes)
c.toValue = make(map[Type][]byte)
}
typ, ok := c.toType[string(tv)]
if ok {
Expand Down Expand Up @@ -436,7 +436,7 @@ func (c *Context) LookupTypeValue(typ Type) Value {
return c.LookupTypeValue(typ)
}

func (c *Context) DecodeTypeValue(tv scode.Bytes) (Type, scode.Bytes) {
func (c *Context) DecodeTypeValue(tv []byte) (Type, []byte) {
if len(tv) == 0 {
return nil, nil
}
Expand Down Expand Up @@ -601,28 +601,28 @@ func (c *Context) DecodeTypeValue(tv scode.Bytes) (Type, scode.Bytes) {
}
}

func DecodeName(tv scode.Bytes) (string, scode.Bytes) {
func DecodeName(tv []byte) (string, []byte) {
namelen, tv := DecodeLength(tv)
if tv == nil || namelen > len(tv) {
return "", nil
}
return string(tv[:namelen]), tv[namelen:]
}

func MustDecodeName(tv scode.Bytes) (string, scode.Bytes) {
func MustDecodeName(tv []byte) (string, []byte) {
namelen, tv := MustDecodeLength(tv)
return string(tv[:namelen]), tv[namelen:]
}

func DecodeLength(tv scode.Bytes) (int, scode.Bytes) {
func DecodeLength(tv []byte) (int, []byte) {
namelen, n := binary.Uvarint(tv)
if n <= 0 {
return 0, nil
}
return int(namelen), tv[n:]
}

func MustDecodeLength(tv scode.Bytes) (int, scode.Bytes) {
func MustDecodeLength(tv []byte) (int, []byte) {
namelen, n := binary.Uvarint(tv)
if n <= 0 {
panic(tv)
Expand Down Expand Up @@ -740,7 +740,7 @@ func IsOptionType(typ Type) bool {
return u != nil
}

func IsNone(typ Type, bytes scode.Bytes) bool {
func IsNone(typ Type, bytes []byte) bool {
if union, ok := TypeUnder(typ).(*TypeUnion); ok {
typ, _ := union.Untag(bytes)
return typ == TypeNone
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestContextLookupByValueAndLookupTypeValue(t *testing.T) {
// Overwriting tv should not affect sctx's cached type value for recType.
super.AppendTypeValue(tv[:0], super.TypeNull)
val := sctx.LookupTypeValue(recType)
assert.Exactly(t, super.EncodeTypeValue(recType), val.Bytes())
assert.Exactly(t, super.EncodeTypeValue(recType), []byte(val.Bytes()))
}

func TestContextLookupTypeNamedErrors(t *testing.T) {
Expand Down
15 changes: 6 additions & 9 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"fmt"
"sort"
"strings"

"github.com/brimdata/super/scode"
)

var (
Expand Down Expand Up @@ -149,13 +147,12 @@ const (
TypeValueMax = TypeValueFusion
)

// True iff the type id is encoded as a BSUP signed or unsigened integer scode.Bytes.
// True iff the type id is a signed or unsigened integer.
func IsInteger(id int) bool {
return id <= IDInt256
}

// True iff the type id is encoded as a BSUP signed or unsigned integer scode.Bytes,
// float16 scode.Bytes, float32 scode.Bytes, or float64 scode.Bytes.
// True iff the type id is a signed or unsigned integer, float16, float32, or float64.
func IsNumber(id int) bool {
return id <= IDDecimal256
}
Expand Down Expand Up @@ -503,16 +500,16 @@ func (t *TypeOfType) Kind() Kind {
return PrimitiveKind
}

func EncodeTypeValue(t Type) scode.Bytes {
func EncodeTypeValue(t Type) []byte {
return AppendTypeValue(nil, t)
}

func AppendTypeValue(b scode.Bytes, t Type) scode.Bytes {
func AppendTypeValue(b []byte, t Type) []byte {
var typedefs map[string]Type
return appendTypeValue(b, t, &typedefs)
}

func appendTypeValue(b scode.Bytes, t Type, typedefs *map[string]Type) scode.Bytes {
func appendTypeValue(b []byte, t Type, typedefs *map[string]Type) []byte {
switch t := t.(type) {
case *TypeNamed:
if *typedefs == nil {
Expand All @@ -529,7 +526,7 @@ func appendTypeValue(b scode.Bytes, t Type, typedefs *map[string]Type) scode.Byt
}
b = append(b, id)
b = binary.AppendUvarint(b, uint64(len(t.Name)))
b = append(b, scode.Bytes(t.Name)...)
b = append(b, t.Name...)
if id == TypeValueNameRef {
return b
}
Expand Down
Loading