d4l3k/go-pry

Access to unexported

q6r opened this issue · 1 comments

q6r commented

reflect panic when trying to access an unexported field in a struct. For example :

type A struct {
  unexp string
  Exp    string
}
a := &A{/*.....*/}

Access to a.unexp will panic. Problem is here..

if field := rVal.FieldByName(sel.Name); field.IsValid() {

I believe there should be a check on field.isValid() AND sel.isExported() or not. If exported then field.Interface(), otherwise need to do something like this.

    // ......
    if field.IsValid() && sel.IsExported() {
      return field.Interface(), nil
    } else if field.IsValid() && !sel.IsExported() {
      k := rVal.FieldByName(sel.Name)
      if k.CanAddr() { // can address
        k = reflect.NewAt(k.Type(), unsafe.Pointer(k.UnsafeAddr())).Elem()
      } else { // can't address
        switch k.Kind() {
        case reflect.String:
          return k.String(), nil
        // ......
        default:
          return k.Interface(), nil
        }   
      }
      return k.Interface(), nil
    }
d4l3k commented

Might also be able to use something like https://github.com/d4l3k/bypass