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

Reflect error 2 #118

Closed
efureev opened this issue Jul 30, 2019 · 1 comment

Comments

@efureev
Copy link

commented Jul 30, 2019

// struct is in a custom package
type SimpleRole struct {
	sync.RWMutex
	permissions []string
}

// .....

type Role struct {
	*SimpleRole

	Id        string    `json:"id" db:"id" goqu:"skipinsert"`
	Key       string    `json:"key" db:"key"`
	Name      string    `json:"name" db:"name"`
	CreatedAt time.Time `json:"-" db:"created_at" goqu:"skipinsert"`
}

Error 1

panic: reflect: indirection through nil pointer to embedded struct

rUser := &Role{
		Key:  `user`,
		Name: `User role`,
	}

// The Error appears here:
sql, arg, err := database.Insert(`rbac_roles`).
		Returning(goqu.C(`id`)).
		Rows(rUser).
		ToSQL()

Error 2

reflect.Value.Interface: cannot return value obtained from unexported field or method.

rUser := &Role{
		Key:  `user`,
		Name: `User role`,
		SimpleRole: &SimpleRole{
			IDStr: ``,
		},
	}

// The Error appears here:
sql, arg, err := database.Insert(`rbac_roles`).
		Returning(goqu.C(`id`)).
		Rows(rUser).
		ToSQL()

and :
(reflect.Value.Interface: cannot return value obtained from unexported field or method)

type Role struct {
	sync.RWMutex `db:"-"`
	Id        string    `json:"id" db:"id"`
}

rUser := Role{
	Id:  `user`,
}
sql, arg, err := database.Insert(`rbac_roles`).Rows(rUser).ToSQL()
@doug-martin

This comment has been minimized.

Copy link
Owner

commented Jul 31, 2019

I've done some more investigation into this and I found a couple of issues that will be addressed in the next release. I'm currently working on fixes.

  1. When creating the columnMap goqu is not filtering out unexported fields that cant be set, in the next release those will be filtered.
  2. I've added support for ignoring embedded structs.
// struct is in a custom package
type SimpleRole struct {
    sync.RWMutex
    permissions []string
}

// .....

type Role struct {
   *SimpleRole `db:"-"`
    Id        string    `json:"id" db:"id" goqu:"skipinsert"`
    Key       string    `json:"key" db:"key"`
    Name      string    `json:"name" db:"name"`
    CreatedAt time.Time `json:"-" db:"created_at" goqu:"skipinsert"`
}
  1. When building the insert or update statements that access the field values Im using the column map to look up fields however goqu was not accounting for the fact that a field may be part of an embedded pointer that may be nil.

Thank you for submitting this issue it pointed out quite use cases that should be handled. I hope to have a fix out in the next day or so.

doug-martin added a commit that referenced this issue Aug 1, 2019
* [FIX] Fix reflection errors related to nil pointers and unexported fields #118
    * Unexported fields are ignored when creating a columnMap
    * Nil embedded pointers will no longer cause a panic
    * Fields on nil embedded pointers will be ignored when creating update or insert statements.
* [ADDED] You can now ingore embedded structs and their fields by using `db:"-"` tag on the embedded struct.
@doug-martin doug-martin referenced this issue Aug 1, 2019
doug-martin added a commit that referenced this issue Aug 1, 2019
* [FIX] Fix reflection errors related to nil pointers and unexported fields #118
    * Unexported fields are ignored when creating a columnMap
    * Nil embedded pointers will no longer cause a panic
    * Fields on nil embedded pointers will be ignored when creating update or insert statements.
* [ADDED] You can now ingore embedded structs and their fields by using `db:"-"` tag on the embedded struct.
doug-martin added a commit that referenced this issue Aug 1, 2019
Fix for #118
@doug-martin doug-martin closed this Aug 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.