MCPcopy
hub / github.com/openacid/slim / Init

Method Init

array/base.go:71–111  ·  view source on GitHub ↗

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{})

Source from the content-addressed store, hash-verified

69//
70// Since 0.2.0
71func (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//

Callers 11

TestBaseInitFunction · 0.95
TestBaseInitWithEncoderFunction · 0.95
TestBase_GetFunction · 0.95
BenchmarkBaseGetFunction · 0.95
BenchmarkBaseGetBytesFunction · 0.95
NewU16Function · 0.45
NewU32Function · 0.45
NewU64Function · 0.45
NewI16Function · 0.45
NewI32Function · 0.45
NewI64Function · 0.45

Calls 3

InitIndexMethod · 0.95
InitEltsMethod · 0.95
NewTypeEncoderEndianFunction · 0.92

Tested by 5

TestBaseInitFunction · 0.76
TestBaseInitWithEncoderFunction · 0.76
TestBase_GetFunction · 0.76
BenchmarkBaseGetFunction · 0.76
BenchmarkBaseGetBytesFunction · 0.76