Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to unexported #46

Open
q6r opened this issue Nov 24, 2017 · 1 comment
Open

Access to unexported #46

q6r opened this issue Nov 24, 2017 · 1 comment

Comments

@q6r
Copy link

q6r commented Nov 24, 2017

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
Copy link
Owner

d4l3k commented Nov 24, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants