// When an array or slice type is found in the Go structure, this function encodes it
// recursively.
345096
func (e *Encoder) encodeArray(valueField reflect.Value, key string) error {
	adaptersCnt := 0
	devicesCnt := 0

	for i := 0; i < valueField.Len(); i++ {
		switch key {
		case "ide":
			if i >= (MAX_IDE_ADAPTERS * MAX_IDE_DEVICES_PER_ADAPTER) {
				return nil
			}

			if devicesCnt >= MAX_IDE_DEVICES_PER_ADAPTER {
				adaptersCnt++
				devicesCnt = 0
			}

			e.parentKey = fmt.Sprintf("%s%d:%d", key, adaptersCnt, devicesCnt)
			devicesCnt++

		case "scsi":
			if i >= (MAX_SCSI_ADAPTERS * MAX_SCSI_DEVICES_PER_ADAPTER) {
				return nil
			}

			if devicesCnt >= MAX_SCSI_DEVICES_PER_ADAPTER {
				adaptersCnt++
				devicesCnt = 0
			}

			val := valueField.Index(i).FieldByName("VirtualDev")
			if !isEmptyValue(val) {
				e.parentKey = fmt.Sprintf("%s%d", key, adaptersCnt)
			} else {
				e.parentKey = fmt.Sprintf("%s%d:%d", key, adaptersCnt, devicesCnt)
				devicesCnt++
			}
		case "sata":
			if i >= (MAX_SATA_ADAPTERS * MAX_SATA_DEVICES_PER_ADAPTER) {
				return nil
			}

			if devicesCnt >= MAX_SATA_DEVICES_PER_ADAPTER {
				adaptersCnt++
				devicesCnt = 0
			}

			e.parentKey = fmt.Sprintf("%s%d:%d", key, adaptersCnt, devicesCnt)
			devicesCnt++
		case "usb":
			if i >= MAX_USB_ADAPTERS*MAX_USB_DEVICES {
				return nil
			}

			e.parentKey = fmt.Sprintf("%s:%d", key, devicesCnt)
			devicesCnt++
		case "ethernet":
			if i >= MAX_VNICS {
				return nil
			}
			e.parentKey = key + strconv.Itoa(i)
		default:
			e.parentKey = key + strconv.Itoa(i)
		}

		err := e.encode(valueField.Index(i))
		if err != nil {
			return err
		}
	}
	return nil
}
107441
func SliceGroups(input []string, size int, fn func(group []string)) {
	// how many to add
	padding := size - (len(input) % size)

	if padding != size {
		p := []string{}

		for i := 0; i <= padding; i++ {
			p = append(p, "")
		}

		input = append(input, p...)
	}

	// how many chunks we're making
	count := len(input) / size

	for i := 0; i < count; i++ {
		chunk := input[i*size : i*size+size]
		fn(chunk)
	}
}
469732
func mustArrayToByteSlice(value reflect.Value) reflect.Value {
	slice := reflect.MakeSlice(reflect.TypeOf([]byte{}), value.Len(), value.Len())
	reflect.Copy(slice, value)
	return slice
}
855835
func (e *Encoder) StructAsArray(flag bool) *Encoder {
	e.structAsArray = flag
	return e
}
1307000
func Function(fn string) {
	if e, ok := encodingDefinition(); ok {
		e.Function = fn
	}
}
494834
func init() {
	noTraverseTypeList = map[reflect.Type]bool{}
	converterMap = map[reflect.Type]map[reflect.Type]Converter{}

	// Default NoTraverseTypeList
	// --------------------------
	// Auto No Traverse struct list for not traversing Deep Level
	// However, field value will be evaluated/processed by go-model library
	AddNoTraverseType(
		time.Time{},
		&time.Time{},
		os.File{},
		&os.File{},
		http.Request{},
		&http.Request{},
		http.Response{},
		&http.Response{},

		// it's better to add it to the list for appropriate type(s)
	)
}
942407
func SliceIt(pointerToASlice interface{}, it ValIterator) error {
	/* TODO defer func() {
		if exp := recover(); exp != nil && it != nil {
			logger.Error(exp)
			exp = safeclose.Close(it)
			if exp != nil {
				logger.Error(exp)
			}
		}
	}()*/

	sl := reflect.ValueOf(pointerToASlice)
	if sl.Kind() == reflect.Ptr {
		sl = sl.Elem()
	} else {
		panic("must be pointer")
	}

	if sl.Kind() != reflect.Slice {
		panic("must be slice")
	}

	sliceType := sl.Type()

	sliceElemType := sliceType.Elem()
	sliceElemPtr := sliceElemType.Kind() == reflect.Ptr
	if sliceElemPtr {
		sliceElemType = sliceElemType.Elem()
	}
	for {
		row := reflect.New(sliceElemType)
		if stop := it.GetNext(row.Interface()); stop {
			break
		}

		if sliceElemPtr {
			sl.Set(reflect.Append(sl, row))
		} else {
			sl.Set(reflect.Append(sl, row.Elem()))
		}
	}

	return safeclose.Close(it)
}
939366
func StructSliceFromByteSlice(b []byte, out interface{}) {
	ptr := reflect.ValueOf(out)
	if ptr.Kind() != reflect.Ptr {
		panic("expected pointer to a slice of structs (*[]X)")
	}
	slice := ptr.Elem()
	if slice.Kind() != reflect.Slice {
		panic("expected pointer to a slice of structs (*[]X)")
	}
	// TODO: More checks, such as ensuring that:
	// - elements are NOT pointers
	// - structs do not contain pointers, slices or maps
	stride := int(slice.Type().Elem().Size())
	if len(b)%stride != 0 {
		panic("size of byte buffer is not a multiple of struct size")
	}
	sh := (*reflect.SliceHeader)(unsafe.Pointer(slice.UnsafeAddr()))
	newRawSliceHeader(sh, b, stride)
}
319508
func Reverse(in interface{}) interface{} {
	value := reflect.ValueOf(in)
	valueType := value.Type()

	kind := value.Kind()

	if kind == reflect.String {
		return ReverseString(in.(string))
	}

	if kind == reflect.Array || kind == reflect.Slice {
		length := value.Len()

		resultSlice := makeSlice(value, length)

		j := 0
		for i := length - 1; i >= 0; i-- {
			resultSlice.Index(j).Set(value.Index(i))
			j++
		}

		return resultSlice.Interface()
	}

	panic(fmt.Sprintf("Type %s is not supported by Reverse", valueType.String()))
}
79848
func DecodeArray(data []byte) ([]interface{}, error) {
	d := NewDecoder(data)
	if c := d.skipSpaces(); c != '[' {
		return nil, d.error(c, "looking for beginning of array")
	}
	val, err := d.array()
	if err != nil {
		return nil, err
	}
	if c := d.skipSpaces(); d.pos < d.end {
		return nil, d.error(c, "after top-level value")
	}
	return val, nil
}
217949
func (sd *structDef) buildArray() {
	sd.buildOneLine(convertToGoType(sd.T.Type.(string), ""))
}
1243793
func (v *Value) Array() ([]*Value, error) {
	var valid bool

	// Check the type of this data
	switch v.data.(type) {
	case []interface{}:
		valid = true
	}

	// Unsure if this is a good way to use slices, it's probably not
	var slice []*Value

	if valid {
		for _, element := range v.data.([]interface{}) {
			child := Value{element, true}
			slice = append(slice, &child)
		}
		return slice, nil
	}
	return slice, ErrNotArray
}
1124181
func (d *Decoder) DecodeArray() ([]interface{}, error) {
	if c := d.skipSpaces(); c != '[' {
		return nil, d.error(c, "looking for beginning of array")
	}
	val, err := d.array()
	if err != nil {
		return nil, err
	}
	if c := d.skipSpaces(); d.pos < d.end {
		return nil, d.error(c, "after top-level value")
	}
	return val, nil
}
623358
func ReverseSlice(slice interface{}) interface{} {
	switch reflect.TypeOf(slice).Kind() {
	case reflect.Slice, reflect.Ptr:
		values := reflect.Indirect(reflect.ValueOf(slice))
		if values.Len() == 0 {
			return slice
		}

		reversedSlice := reflect.MakeSlice(reflect.SliceOf(reflect.Indirect(values.Index(0)).Type()), values.Len(), values.Len())

		k := 0
		for i := values.Len() - 1; i >= 0; i-- {
			reversedSlice.Index(k).Set(copyElement(values.Index(i)))
			k++
		}

		return reversedSlice.Interface()
	default:
		panic(fmt.Sprintf("Type not allowed: %v", reflect.TypeOf(slice).Kind()))
	}
}
549895
func PrintArray(m interface{}) string {
	var outputText string

	switch reflect.TypeOf(m).Kind() {
	case reflect.Slice:
		var xs []string

		slice := reflect.ValueOf(m)
		for i := 0; i < slice.Len(); i++ {
			xs = append(xs, fmt.Sprintf("[%d]=%s", i, slice.Index(i)))
		}

		if len(xs) == 0 {
			outputText = "len(array) == 0"
		} else {
			outputText = strings.Join(xs, "\n")
		}
	default:
		outputText = fmt.Sprintf("error while printing array\nreceived a %T\n  %s\n", m, m)
	}

	return outputText
}
951243
func encodeByteArray(v reflect.Value) (interface{}, error) {
	b := make([]byte, v.Len())
	for i := 0; i < v.Len(); i++ {
		b[i] = v.Index(i).Interface().(byte)
	}

	dst := make([]byte, base64.StdEncoding.EncodedLen(len(b)))
	base64.StdEncoding.Encode(dst, b)

	return map[string]interface{}{
		"$reql_type$": "BINARY",
		"data":        string(dst),
	}, nil
}
991288
func IsArray(val Value) bool {
	r := reflect.Indirect(reflect.ValueOf(val))
	switch r.Kind() {
	case reflect.Slice, reflect.Array:
		return true
	}
	return false
}