// StructScan copies the columns from the matched row into the struct
// pointed at by dest. If more than one row matches the query, Scan
// uses the first row and discards the rest. If no row matches the
// query, Scan returns ErrNoRows.
769008
func (r *Row) StructScan(dest interface{}) error {
	if r.err != nil {
		return r.err
	}

	defer r.rows.Close()

	if !r.rows.Next() {
		if err := r.rows.Err(); err != nil {
			return err
		}
		return sql.ErrNoRows
	}
	if err := r.rows.StructScan(dest); err != nil {
		return err
	}
	// Make sure the query can be processed to completion with no errors.
	return r.rows.Close()
}
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)
}
1283683
func (ss *StringSlice) Scan(src interface{}) error {
	buf, ok := src.([]byte)
	if !ok {
		return errors.New("invalid StringSlice")
	}

	// change quote escapes for csv parser
	str := quoteEscapeRegex.ReplaceAllString(string(buf), `$1""`)
	str = strings.Replace(str, `\\`, `\`, -1)

	// remove braces
	str = str[1 : len(str)-1]

	// bail if only one
	if len(str) == 0 {
		*ss = StringSlice([]string{})
		return nil
	}

	// parse with csv reader
	cr := csv.NewReader(strings.NewReader(str))
	slice, err := cr.Read()
	if err != nil {
		fmt.Printf("exiting!: %v\n", err)
		return err
	}

	*ss = StringSlice(slice)

	return nil
}
435607
func (r *Report) SetScan(scanId bson.ObjectId) {
	r.Scan = scanId
	if r.Type == TypeMulti {
		for _, rep := range r.Multi {
			rep.SetScan(scanId)
		}
	}
}
578056
func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
	var state errorState
	// Encode fields in tag order so that decoders may use optimizations
	// that depend on the ordering.
	// http://code.google.com/apis/protocolbuffers/docs/encoding.html#order
	for _, i := range prop.order {
		p := prop.Prop[i]
		if p.enc != nil {
			err := p.enc(o, p, base)
			if err != nil {
				if err == ErrNil {
					if p.Required && state.err == nil {
						state.err = &RequiredNotSetError{p.Name}
					}
				} else if !state.shouldContinue(err, p) {
					return err
				}
			}
		}
	}

	// Add unrecognized fields at the end.
	if prop.unrecField.IsValid() {
		v := *structPointer_Bytes(base, prop.unrecField)
		if len(v) > 0 {
			o.buf = append(o.buf, v...)
		}
	}

	return state.err
}
606188
func (c *Client) HScan(hash string, cursor string, options ...interface{}) (string, map[string]string, error) {
	return c.scanCommandMap("HSCAN", hash, cursor, options...)
}
214350
func TableFromStructs(structs Slice) *Table {
	s := reflectSlice(structs)
	st := s.Type()
	if st.Elem().Kind() != reflect.Struct {
		panic(&generic.TypeError{st, nil, "is not a slice of struct"})
	}

	var t Builder
	rows := s.Len()
	var rec func(reflect.Type, []int)
	rec = func(typ reflect.Type, index []int) {
		for fn := 0; fn < typ.NumField(); fn++ {
			field := typ.Field(fn)
			if field.PkgPath != "" {
				continue
			}
			oldIndexLen := len(index)
			index = append(index, field.Index...)
			if field.Anonymous {
				rec(field.Type, index)
			} else {
				col := reflect.MakeSlice(reflect.SliceOf(field.Type), rows, rows)
				for i := 0; i < rows; i++ {
					col.Index(i).Set(s.Index(i).FieldByIndex(index))
				}
				t.Add(field.Name, col.Interface())
			}
			index = index[:oldIndexLen]
		}
	}
	rec(st.Elem(), []int{})
	return t.Done()
}
592324
func scan(s string) <-chan match {
	ch := make(chan match)
	go func() {
		depth := 0
		opener := 0
		for index, char := range s {
			switch char {
			case '(':
				if depth == 0 {
					opener = index
				}
				depth++
			case ')':
				depth--
				if depth == 0 {
					ch <- match{
						openPos:  opener,
						closePos: index + 1,
					}
				}
			}

		}
		close(ch)
	}()
	return ch
}
496690
func (g *Group) StructFunc(f func(*Group)) *Statement {
	s := StructFunc(f)
	g.items = append(g.items, s)
	return s
}
259954
func StructsAreEqual(left, right Struct) bool {
	if len(left.Fields) != len(right.Fields) {
		return false
	}

	// Fields are unordered so we need to build a map to actually compare
	// them.

	leftFields := left.fieldMap()
	rightFields := right.fieldMap()

	for i, lvalue := range leftFields {
		if rvalue, ok := rightFields[i]; !ok {
			return false
		} else if !ValuesAreEqual(lvalue, rvalue) {
			return false
		}
	}

	return true
}
872534
func Struct(name string, opts ...FieldOpt) FieldOpt {
	return func(em *eventMetadata, ed *eventData) {
		em.writeStruct(name, uint8(len(opts)), 0)
		for _, opt := range opts {
			opt(em, ed)
		}
	}
}
985605
func NewScanStr(ctx context.Context, table string, options ...func(Call) error) (*Scan, error) {
	return NewScan(ctx, []byte(table), options...)
}
1261517
func (gc *GeometryCollection) Scan(src interface{}) error {
	if src == nil {
		gc.GeometryCollection = nil
		return nil
	}
	b, ok := src.([]byte)
	if !ok {
		return ErrExpectedByteSlice{Value: src}
	}
	got, err := Unmarshal(b)
	if err != nil {
		return err
	}
	gc1, ok := got.(*geom.GeometryCollection)
	if !ok {
		return wkbcommon.ErrUnexpectedType{Got: gc1, Want: gc}
	}
	gc.GeometryCollection = gc1
	return nil
}
32513
func (m *StringArray) Scan(src interface{}) error {

	var srcBytes []byte
	switch val := src.(type) {
	case string:
		srcBytes = []byte(val)
	case []byte:
		srcBytes = val
	default:
		return fmt.Errorf("Incompatible type for StringArray got %T", src)
	}
	sa := make([]string, 0)
	if u.IsJsonArray(srcBytes) {
		err := json.Unmarshal(srcBytes, &sa)
		if err != nil {
			return err
		}
	} else if parts := strings.Split(string(srcBytes), ","); len(parts) > 0 {
		sa = parts
	}

	*m = StringArray(sa)
	return nil
}
723896
func (i *Int) Scan(src interface{}) error {
	b, ok := src.([]byte)
	if !ok {
		return errors.New("Scan source was not []byte")
	}
	str := string(b)
	*i = strToInt(str)
	return nil
}
581531
func Load(v interface{}, s ...Source) error {
	if reflect.Indirect(reflect.ValueOf(v)).Kind() != reflect.Struct {
		panic("scan for non-struct type")
	}
	if len(s) == 0 {
		return nil
	}
	scn := &scanner{s}
	return scn.scan(v)
}
506356
func StructsToMaps(Structs interface{}, Maps *[]map[string]interface{}, tag string) {
	sliceType := reflect.TypeOf(Structs)
	if sliceType.Kind() != reflect.Slice {
		panic(fmt.Errorf("Expected slice of structs as first argument, got %s", sliceType.Kind()))
	}

	structType := sliceType.Elem()
	if structType.Kind() != reflect.Struct {
		panic(fmt.Errorf("Expected slice of structs as first argument, got slice of %s", structType.Kind()))
	}

	structs := reflect.ValueOf(Structs)
	l := structs.Len()
	maps := reflect.MakeSlice(reflect.TypeOf([]map[string]interface{}{}), 0, l)

	for i := 0; i < l; i++ {
		m := make(map[string]interface{})
		StructValueToMap(structs.Index(i).Interface(), m, tag)
		maps = reflect.Append(maps, reflect.ValueOf(m))
	}

	reflect.ValueOf(Maps).Elem().Set(maps)
}