Init initializes an array from the "indexes" and "elts". The indexes must be an ascending int32 slice, otherwise, return the ErrIndexNotAscending error. The "elts" is a slice. Since 0.2.0
(indexes []int32, elts interface{})
| 69 | // |
| 70 | // Since 0.2.0 |
| 71 | func (a *Base) Init(indexes []int32, elts interface{}) error { |
| 72 | |
| 73 | rElts := reflect.ValueOf(elts) |
| 74 | if rElts.Kind() != reflect.Slice { |
| 75 | panic("elts is not a slice") |
| 76 | } |
| 77 | |
| 78 | n := rElts.Len() |
| 79 | if len(indexes) != n { |
| 80 | return ErrIndexLen |
| 81 | } |
| 82 | |
| 83 | err := a.InitIndex(indexes) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | |
| 88 | if len(indexes) == 0 { |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | var encoder encode.Encoder |
| 93 | |
| 94 | if a.EltEncoder == nil { |
| 95 | var err error |
| 96 | encoder, err = encode.NewTypeEncoderEndian(rElts.Index(0).Interface(), endian) |
| 97 | if err != nil { |
| 98 | // TODO wrap |
| 99 | return err |
| 100 | } |
| 101 | } else { |
| 102 | encoder = a.EltEncoder |
| 103 | } |
| 104 | |
| 105 | _, err = a.InitElts(elts, encoder) |
| 106 | if err != nil { |
| 107 | return errors.Wrapf(err, "failure Init Array") |
| 108 | } |
| 109 | |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | // InitElts initialized a.Elts, by encoding elements in to bytes. |
| 114 | // |