Skip to content
Open
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
2 changes: 1 addition & 1 deletion bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func packItem(ns *reindexerNamespace, item any, json []byte, ser *cjson.Serializ
}
if t != ns.rtype {
if ns.rtype.Name() != t.Name() || ns.rtype.PkgPath() != t.PkgPath() {
panic(ErrWrongType)
return 0, 0, false, ErrWrongType
}
}

Expand Down
8 changes: 6 additions & 2 deletions cjson/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cjson

import (
"encoding/binary"
"errors"
"fmt"
"math"
"reflect"
Expand All @@ -15,6 +16,7 @@ var isLittleEndian = func() bool {
var v uint16 = 0x0102
return *(*byte)(unsafe.Pointer(&v)) == 0x02
}()
var errInvalidReflectionValue = errors.New("rq: invalid reflection value")

type Serializer struct {
buf []byte
Expand Down Expand Up @@ -181,10 +183,12 @@ func (s *Serializer) PutValue(v reflect.Value) error {
s.PutVarCUInt(valueTuple)
s.PutVarCUInt(v.Len())
for i := 0; i < v.Len(); i++ {
s.PutValue(v.Index(i))
if err := s.PutValue(v.Index(i)); err != nil {
return err
}
}
default:
panic(fmt.Errorf("rq: Invalid reflection type %s", v.Kind().String()))
return errInvalidReflectionValue
}
return nil
}
Expand Down
56 changes: 10 additions & 46 deletions expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ type SubQuery struct {
SubQuery *Query
}

// FlatArrayLen is the flat_array_len(field) expression; implements IFunction and IExpression.
// FlatArrayLen is the flat_array_len(field) expression.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the idea behind removing the comment?

type FlatArrayLen struct {
Field string
}

// Now is the now(time_unit) expression; implements IFunction and IExpression.
// Now is the now(time_unit) expression.
type Now struct {
TimeUnit TimeUnit
}
Expand Down Expand Up @@ -87,48 +87,6 @@ func (s SubQuery) Serialize(ser *cjson.Serializer) {
ser.PutVBytes(s.SubQuery.ser.Bytes())
}

func (f FlatArrayLen) FunctionType() int {
return functionFlatArrayLen
}

func (f FlatArrayLen) Fields() []string {
return []string{f.Field}
}

func (f FlatArrayLen) Args() []any {
return []any{}
}

func (f Now) FunctionType() int {
return functionNow
}

func (f Now) Fields() []string {
return []string{}
}

func (f Now) Args() []any {
return []any{f.TimeUnit}
}

type IFunction interface {
FunctionType() int
Fields() []string
Args() []any
}

func SerializeFunction(fn IFunction, ser *cjson.Serializer) {
ser.PutVarCUInt(len(fn.Fields()))
for _, field := range fn.Fields() {
ser.PutVString(field)
}
ser.PutVarCUInt(len(fn.Args()))
for _, arg := range fn.Args() {
ser.PutValue(reflect.ValueOf(arg))
}
ser.PutVarCUInt(fn.FunctionType())
}

// Type reports expressionTypeExpression for the flat_array_len function node.
func (f FlatArrayLen) Type() int {
return expressionTypeExpression
Expand All @@ -137,7 +95,10 @@ func (f FlatArrayLen) Type() int {
// Serialize writes the function expression tag and flat_array_len payload.
func (f FlatArrayLen) Serialize(ser *cjson.Serializer) {
ser.PutVarCUInt(int(f.Type()))
SerializeFunction(f, ser)
ser.PutVarCUInt(1)
ser.PutVString(f.Field)
ser.PutVarCUInt(0)
ser.PutVarCUInt(functionFlatArrayLen)
}

// Type reports expressionTypeExpression for the now function node.
Expand All @@ -148,5 +109,8 @@ func (n Now) Type() int {
// Serialize writes the function expression tag and now() payload.
func (n Now) Serialize(ser *cjson.Serializer) {
ser.PutVarCUInt(int(n.Type()))
SerializeFunction(n, ser)
ser.PutVarCUInt(0)
ser.PutVarCUInt(1)
ser.PutVarCUInt(valueString).PutVString(string(n.TimeUnit))
ser.PutVarCUInt(functionNow)
}
Loading