本文整理汇总了Golang中reflect.Append函数的典型用法代码示例。如果您正苦于以下问题:Golang Append函数的具体用法?Golang Append怎么用?Golang Append使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Append函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: partitionate
func (p *partitioner) partitionate(val, idx_or_key reflect.Value) {
if ok := callPredicate(p.fn, val, idx_or_key); ok {
p.t = reflect.Append(p.t, val)
} else {
p.f = reflect.Append(p.f, val)
}
}
示例2: UniqueWithError
// Remove duplicate element of a slice.
// If error occurs, return value of r will be nil.
func UniqueWithError(s interface{}) (r interface{}, err error) {
v := reflect.ValueOf(s)
if v.Kind() != reflect.Slice {
err = fmt.Errorf("Parameter a must be a slice.")
return
}
rest := reflect.MakeSlice(reflect.TypeOf(s), 0, 0)
for i := v.Len() - 1; i >= 0; i-- {
current := reflect.ValueOf(s).Slice(0, i)
exist, e := InWithError(current.Interface(), v.Index(i).Interface())
if e != nil {
err = e
return
}
if !exist {
rest = reflect.Append(rest, v.Index(i))
}
s = current.Interface()
}
n := reflect.ValueOf(s)
for i := rest.Len() - 1; i >= 0; i-- {
n = reflect.Append(n, rest.Index(i))
}
r = n.Interface()
return
}
示例3: getColumnAsScope
func (scope *Scope) getColumnAsScope(column string) *Scope {
indirectScopeValue := scope.IndirectValue()
switch indirectScopeValue.Kind() {
case reflect.Slice:
if fieldStruct, ok := scope.GetModelStruct().ModelType.FieldByName(column); ok {
fieldType := fieldStruct.Type
if fieldType.Kind() == reflect.Slice || fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
}
results := reflect.New(reflect.SliceOf(reflect.PtrTo(fieldType))).Elem()
for i := 0; i < indirectScopeValue.Len(); i++ {
result := indirect(indirect(indirectScopeValue.Index(i)).FieldByName(column))
if result.Kind() == reflect.Slice {
for j := 0; j < result.Len(); j++ {
if elem := result.Index(j); elem.CanAddr() {
results = reflect.Append(results, elem.Addr())
}
}
} else if result.CanAddr() {
results = reflect.Append(results, result.Addr())
}
}
return scope.New(results.Interface())
}
case reflect.Struct:
if field := indirectScopeValue.FieldByName(column); field.CanAddr() {
return scope.New(field.Addr().Interface())
}
}
return nil
}
示例4: Append
func (association *Association) Append(values ...interface{}) *Association {
scope := association.Scope
field := scope.IndirectValue().FieldByName(association.Column)
for _, value := range values {
reflectvalue := reflect.ValueOf(value)
if reflectvalue.Kind() == reflect.Ptr {
if reflectvalue.Elem().Kind() == reflect.Struct {
if field.Type().Elem().Kind() == reflect.Ptr {
field.Set(reflect.Append(field, reflectvalue))
} else if field.Type().Elem().Kind() == reflect.Struct {
field.Set(reflect.Append(field, reflectvalue.Elem()))
}
} else if reflectvalue.Elem().Kind() == reflect.Slice {
if field.Type().Elem().Kind() == reflect.Ptr {
field.Set(reflect.AppendSlice(field, reflectvalue))
} else if field.Type().Elem().Kind() == reflect.Struct {
field.Set(reflect.AppendSlice(field, reflectvalue.Elem()))
}
}
} else if reflectvalue.Kind() == reflect.Struct && field.Type().Elem().Kind() == reflect.Struct {
field.Set(reflect.Append(field, reflectvalue))
} else if reflectvalue.Kind() == reflect.Slice && field.Type().Elem() == reflectvalue.Type().Elem() {
field.Set(reflect.AppendSlice(field, reflectvalue))
} else {
association.err(errors.New("invalid association type"))
}
}
scope.callCallbacks(scope.db.parent.callback.updates)
return association.err(scope.db.Error)
}
示例5: decode
func (processor *processor) decode() (errors []error) {
if processor.checkSkipLeft() || processor.MetaValues == nil {
return
}
for _, metaValue := range processor.MetaValues.Values {
meta := metaValue.Meta
if meta == nil {
continue
}
if processor.newRecord && !meta.HasPermission(roles.Create, processor.Context) {
continue
} else if !meta.HasPermission(roles.Update, processor.Context) {
continue
}
if setter := meta.GetSetter(); setter != nil {
setter(processor.Result, metaValue, processor.Context)
continue
}
res := metaValue.Meta.GetResource()
if res == nil {
continue
}
field := reflect.Indirect(reflect.ValueOf(processor.Result)).FieldByName(meta.GetFieldName())
if field.Kind() == reflect.Struct {
value := reflect.New(field.Type())
associationProcessor := DecodeToResource(res, value.Interface(), metaValue.MetaValues, processor.Context)
associationProcessor.Start()
if !associationProcessor.SkipLeft {
field.Set(value.Elem())
}
} else if field.Kind() == reflect.Slice {
var fieldType = field.Type().Elem()
var isPtr bool
if fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
isPtr = true
}
value := reflect.New(fieldType)
associationProcessor := DecodeToResource(res, value.Interface(), metaValue.MetaValues, processor.Context)
associationProcessor.Start()
if !associationProcessor.SkipLeft {
if !reflect.DeepEqual(reflect.Zero(fieldType).Interface(), value.Elem().Interface()) {
if isPtr {
field.Set(reflect.Append(field, value))
} else {
field.Set(reflect.Append(field, value.Elem()))
}
}
}
}
}
return
}
示例6: getColumnsAsScope
func (scope *Scope) getColumnsAsScope(column string) *Scope {
values := scope.IndirectValue()
switch values.Kind() {
case reflect.Slice:
modelType := values.Type().Elem()
if modelType.Kind() == reflect.Ptr {
modelType = modelType.Elem()
}
fieldStruct, _ := modelType.FieldByName(column)
var columns reflect.Value
if fieldStruct.Type.Kind() == reflect.Slice || fieldStruct.Type.Kind() == reflect.Ptr {
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type.Elem()))).Elem()
} else {
columns = reflect.New(reflect.SliceOf(reflect.PtrTo(fieldStruct.Type))).Elem()
}
for i := 0; i < values.Len(); i++ {
column := reflect.Indirect(values.Index(i)).FieldByName(column)
if column.Kind() == reflect.Ptr {
column = column.Elem()
}
if column.Kind() == reflect.Slice {
for i := 0; i < column.Len(); i++ {
columns = reflect.Append(columns, column.Index(i).Addr())
}
} else {
columns = reflect.Append(columns, column.Addr())
}
}
return scope.New(columns.Interface())
case reflect.Struct:
return scope.New(values.FieldByName(column).Addr().Interface())
}
return nil
}
示例7: MultiQuery
//MultiQuery executes given query and returns slice of all the entities it returns, with their keys set.
func MultiQuery(c appengine.Context, typ reflect.Type, kind string, query *datastore.Query) (ms interface{}, err error) {
is := reflect.MakeSlice(reflect.SliceOf(reflect.PtrTo(typ)), 0, 0)
iter := query.Run(c)
for {
val := reflect.New(typ)
var key *datastore.Key
key, err = iter.Next(val.Interface())
if err != nil {
if err == datastore.Done {
err = nil
val.MethodByName("SetKey").Call([]reflect.Value{reflect.ValueOf(key)})
reflect.Append(is, val)
break
}
return
}
val.MethodByName("SetKey").Call([]reflect.Value{reflect.ValueOf(key)})
is = reflect.Append(is, val)
}
ms = is.Interface()
return
}
示例8: add
func (l *listSink) add(i *item) (bool, error) {
if l.limit == 0 {
return true, nil
}
if l.skip > 0 {
l.skip--
return false, nil
}
if !l.results.IsValid() {
l.results = reflect.MakeSlice(reflect.Indirect(l.ref).Type(), 0, 0)
}
if l.limit > 0 {
l.limit--
}
if l.idx == l.results.Len() {
if l.isPtr {
l.results = reflect.Append(l.results, *i.value)
} else {
l.results = reflect.Append(l.results, reflect.Indirect(*i.value))
}
}
l.idx++
return l.limit == 0, nil
}
示例9: GroupBy
// If op is Operator of "[]T", type of f must be "func(T) T2".
// And GroupBy(f) returns value of "map[T2] []T"
func (op *Op) GroupBy(f interface{}) interface{} {
vs := reflect.ValueOf(op.Slice)
vf := reflect.ValueOf(f)
tf := vf.Type()
if tf.NumIn() != 1 {
panic("Number of Argument must be 1")
}
if tf.NumOut() != 1 {
panic("Number of return value must be 1")
}
tif := tf.In(0)
tof := tf.Out(0)
if tif != vs.Type().Elem() {
panic("Mismatch function type")
}
len := vs.Len()
vom := reflect.MakeMap(reflect.MapOf(tof, vs.Type()))
for i := 0; i < len; i++ {
v := vs.Index(i)
vk := vf.Call([]reflect.Value{v})[0]
vi := vom.MapIndex(vk)
if vi.IsValid() {
vom.SetMapIndex(vk, reflect.Append(vi, v))
} else {
vom.SetMapIndex(vk, reflect.Append(reflect.MakeSlice(vs.Type(), 0, len), v))
}
}
return vom.Interface()
}
示例10: ArrayMerge
func ArrayMerge(arrayData interface{}, arrayData2 interface{}, arrayOther ...interface{}) interface{} {
arrayOther = append([]interface{}{arrayData2}, arrayOther...)
arrayOtherMap := sliceToMap(arrayOther)
arrayValue := reflect.ValueOf(arrayData)
arrayType := arrayValue.Type()
arrayLen := arrayValue.Len()
result := reflect.MakeSlice(arrayType, 0, 0)
for i := 0; i != arrayLen; i++ {
singleArrayDataValue := arrayValue.Index(i)
singleArrayDataValueInterface := singleArrayDataValue.Interface()
isFirst, isExist := arrayOtherMap[singleArrayDataValueInterface]
if isExist == true && isFirst == false {
continue
}
result = reflect.Append(result, singleArrayDataValue)
arrayOtherMap[singleArrayDataValueInterface] = false
}
for single, isFirst := range arrayOtherMap {
if isFirst == false {
continue
}
result = reflect.Append(result, reflect.ValueOf(single))
}
return result.Interface()
}
示例11: _partition
func _partition(values []reflect.Value) []reflect.Value {
slice := interfaceToValue(values[0])
fn := values[1]
var t, f reflect.Value
if values[0].Kind() == reflect.Interface {
t = reflect.ValueOf(make([]interface{}, 0))
f = reflect.ValueOf(make([]interface{}, 0))
} else {
t = reflect.MakeSlice(slice.Type(), 0, 0)
f = reflect.MakeSlice(slice.Type(), 0, 0)
}
for i := 0; i < slice.Len(); i++ {
e := slice.Index(i)
r := fn.Call([]reflect.Value{e})
if r[0].Bool() {
t = reflect.Append(t, e)
} else {
f = reflect.Append(f, e)
}
}
return []reflect.Value{t, f}
}
示例12: decodeSlice
func (d *Decoder) decodeSlice(s *decodStatus, v reflect.Value, t reflect.Type, tg tag) error {
if _, isInline := tg.Modifiers["inline"]; isInline {
val, ok := s.GetValue(d.env)
if !ok {
return nil
}
elements := d.arrSep.Split(val, -1)
slice := reflect.MakeSlice(t, 0, len(elements))
for _, s := range elements {
mv := reflect.New(t.Elem())
err := d.decodeField(mv.Elem(), s)
if err != nil {
return err
}
slice = reflect.Append(slice, mv.Elem())
}
v.Set(slice)
return nil
}
index := s.GetIndex()
slice := reflect.MakeSlice(t, 0, len(index))
for _, i := range index {
mv := reflect.New(t.Elem())
err := d.decodeElement(s, mv.Elem(), mv.Elem().Type(), tag{Name: strconv.Itoa(i), Modifiers: tg.Modifiers})
if err != nil {
return err
}
slice = reflect.Append(slice, mv.Elem())
}
v.Set(slice)
return nil
}
示例13: ScanVals
//This will execute the SQL and append results to the slice.
// var ids []uint32
// if err := From("test").Select("id").ScanVals(&ids); err != nil{
// panic(err.Error()
// }
//
//i: Takes a pointer to a slice of primitive values.
func (me CrudExec) ScanVals(i interface{}) error {
if me.err != nil {
return me.err
}
val := reflect.ValueOf(i)
if val.Kind() != reflect.Ptr {
return NewGoquError("Type must be a pointer to a slice when calling ScanVals")
}
val = reflect.Indirect(val)
if val.Kind() != reflect.Slice {
return NewGoquError("Type must be a pointer to a slice when calling ScanVals")
}
t, _, isSliceOfPointers := getTypeInfo(i, val)
rows, err := me.database.Query(me.Sql, me.Args...)
if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
row := reflect.New(t)
if err := rows.Scan(row.Interface()); err != nil {
return err
}
if isSliceOfPointers {
val.Set(reflect.Append(val, row))
} else {
val.Set(reflect.Append(val, reflect.Indirect(row)))
}
}
if err := rows.Err(); err != nil {
return err
}
return nil
}
示例14: GetSliceConvert
func GetSliceConvert(c *Convertors) func(*Params, string, reflect.Type) reflect.Value {
return func(p *Params, name string, typ reflect.Type) reflect.Value {
var values ItemByIndex
var maxIdx = 0
var noindex = 0
processItem := func(key string, vals []string) {
var idx int
var err error
if !strings.HasPrefix(key, name+"[") {
return
}
lIdx, rIdx := len(name), strings.IndexByte(key[len(name):], ']')+len(name)
if rIdx == -1 {
//not have ] char in key
return
}
//process e.g. name[]
if lIdx == rIdx-1 {
noindex++
goto END
}
idx, err = strconv.Atoi(key[lIdx+1 : rIdx])
if err != nil {
return
}
if idx > maxIdx {
maxIdx = idx
}
END:
value := c.Convert(p, key[:rIdx+1], typ.Elem())
values = append(values, &item{idx, value})
}
for k, vals := range p.Values {
processItem(k, vals)
}
//if array len small than 10000, keep index
if maxIdx < 10000 {
slice := reflect.MakeSlice(typ, maxIdx+1, maxIdx+noindex+1)
for _, val := range values {
if val.index > -1 {
slice.Index(val.index).Set(val.value)
} else {
slice = reflect.Append(slice, val.value)
}
}
return slice
}
sort.Sort(values)
slice := reflect.MakeSlice(typ, 0, len(values))
for _, val := range values {
slice = reflect.Append(slice, val.value)
}
return slice
}
}
示例15: StructScan
// Fully scan a sql.Rows result into the dest slice. StructScan destinations MUST
// have fields that map to every column in the result, and they MAY have fields
// in addition to those. Fields are mapped to column names by lowercasing the
// field names by default: use the struct tag `db` to specify exact column names
// for each field.
//
// StructScan will scan in the entire rows result, so if you need do not want to
// allocate structs for the entire result, use Queryx and see sqlx.Rows.StructScan.
func StructScan(rows *sql.Rows, dest interface{}) error {
var v, vp reflect.Value
var isPtr bool
value := reflect.ValueOf(dest)
if value.Kind() != reflect.Ptr {
return errors.New("Must pass a pointer, not a value, to StructScan destination.")
}
direct := reflect.Indirect(value)
slice, err := BaseSliceType(value.Type())
if err != nil {
return err
}
isPtr = slice.Elem().Kind() == reflect.Ptr
base, err := BaseStructType(slice.Elem())
if err != nil {
return err
}
fm, err := getFieldmap(base)
if err != nil {
return err
}
columns, err := rows.Columns()
if err != nil {
return err
}
fields, err := getFields(fm, columns)
if err != nil {
return err
}
// this will hold interfaces which are pointers to each field in the struct
values := make([]interface{}, len(columns))
for rows.Next() {
// create a new struct type (which returns PtrTo) and indirect it
vp = reflect.New(base)
v = reflect.Indirect(vp)
setValues(fields, v, values)
// scan into the struct field pointers and append to our results
err = rows.Scan(values...)
if err != nil {
return err
}
if isPtr {
direct.Set(reflect.Append(direct, vp))
} else {
direct.Set(reflect.Append(direct, v))
}
}
return nil
}