github.com/goplus/igop@v0.25.0/xcall.go (about)

     1  /*
     2   * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package igop
    18  
    19  import (
    20  	"errors"
    21  	"reflect"
    22  
    23  	"github.com/goplus/reflectx"
    24  )
    25  
    26  func fieldAddrX(v interface{}, index int) (interface{}, error) {
    27  	x := reflect.ValueOf(v).Elem()
    28  	if !x.IsValid() {
    29  		return nil, errors.New("invalid memory address or nil pointer dereference")
    30  	}
    31  	return reflectx.FieldX(x, index).Addr().Interface(), nil
    32  }
    33  
    34  func fieldX(v interface{}, index int) (interface{}, error) {
    35  	x := reflect.ValueOf(v)
    36  	for x.Kind() == reflect.Ptr {
    37  		x = x.Elem()
    38  	}
    39  	if !x.IsValid() {
    40  		return nil, errors.New("invalid memory address or nil pointer dereference")
    41  	}
    42  	return reflectx.FieldX(x, index).Interface(), nil
    43  }
    44  
    45  func allMethodX(typ reflect.Type) []reflect.Method {
    46  	n := reflectx.NumMethodX(typ)
    47  	if n == 0 {
    48  		return nil
    49  	}
    50  	ms := make([]reflect.Method, n)
    51  	for i := 0; i < n; i++ {
    52  		ms[i] = reflectx.MethodX(typ, i)
    53  	}
    54  	return ms
    55  }