ipld-eth-server/vendor/github.com/polydawn/refmt/obj/unmarshalSliceWildcard.go
Elizabeth Engelman 36533f7c3f Update vendor directory and make necessary code changes
Fixes for new geth version
2019-09-25 16:32:27 -05:00

82 lines
2.7 KiB
Go

package obj
import (
"reflect"
. "github.com/polydawn/refmt/tok"
)
type unmarshalMachineSliceWildcard struct {
target_rv reflect.Value // target slice handle
working_rv reflect.Value // working slice (target is set to this at end)
value_rt reflect.Type
valueZero_rv reflect.Value // a zero of the slice's value type (for kicking append)
valueMach UnmarshalMachine
step unmarshalMachineStep
index int
}
func (mach *unmarshalMachineSliceWildcard) Reset(slab *unmarshalSlab, rv reflect.Value, rt reflect.Type) error {
mach.target_rv = rv
mach.working_rv = rv
mach.value_rt = rt.Elem()
mach.valueZero_rv = reflect.Zero(mach.value_rt)
mach.valueMach = slab.requisitionMachine(mach.value_rt)
mach.step = mach.step_Initial
mach.index = 0
return nil
}
func (mach *unmarshalMachineSliceWildcard) Step(driver *Unmarshaller, slab *unmarshalSlab, tok *Token) (done bool, err error) {
return mach.step(driver, slab, tok)
}
func (mach *unmarshalMachineSliceWildcard) step_Initial(_ *Unmarshaller, slab *unmarshalSlab, tok *Token) (done bool, err error) {
// If it's a special state, start an object.
// (Or, blow up if its a special state that's silly).
switch tok.Type {
case TMapOpen:
return true, ErrMalformedTokenStream{tok.Type, "start of array"}
case TArrOpen:
// Great. Consumed.
mach.step = mach.step_AcceptValue
// Initialize the slice.
mach.target_rv.Set(reflect.MakeSlice(mach.target_rv.Type(), 0, 0))
return false, nil
case TMapClose:
return true, ErrMalformedTokenStream{tok.Type, "start of array"}
case TArrClose:
return true, ErrMalformedTokenStream{tok.Type, "start of array"}
case TNull:
mach.target_rv.Set(reflect.Zero(mach.target_rv.Type()))
return true, nil
default:
return true, ErrMalformedTokenStream{tok.Type, "start of array"}
}
}
func (mach *unmarshalMachineSliceWildcard) step_AcceptValue(driver *Unmarshaller, slab *unmarshalSlab, tok *Token) (done bool, err error) {
// Either form of open token are valid, but
// - an arrClose is ours
// - and a mapClose is clearly invalid.
switch tok.Type {
case TMapClose:
// no special checks for ends of wildcard slice; no such thing as incomplete.
return true, ErrMalformedTokenStream{tok.Type, "start of value or end of array"}
case TArrClose:
mach.target_rv.Set(mach.working_rv)
// release the slab row we requisitioned for our value machine.
slab.release()
return true, nil
}
// Grow the slice if necessary.
mach.working_rv = reflect.Append(mach.working_rv, mach.valueZero_rv)
// Recurse on a handle to the next index.
rv := mach.working_rv.Index(mach.index)
mach.index++
return false, driver.Recurse(tok, rv, mach.value_rt, mach.valueMach)
// Step simply remains `step_AcceptValue` -- arrays don't have much state machine.
}