blockloop / scan

Tiny lib to scan SQL rows directly to structs, slices, and primitive types

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: Support embedded structs in `scan.Columns` and `scan.Values`

max-stytch opened this issue · comments

Essentially #16, but for scan.Columns and scan.Values

Today it is impossible to list all columns for nested structs.

type thingy struct {
	ColA int `db:"a"`
}
type thingyWrapper struct {
	thingy
	ColB string `db:"b"`
}

func TestEmbeddedBehavior(t *testing.T) {
	thing := &thingy{}
	cols1, err := scan.Columns(thing)
	assert.NoError(t, err)
	assert.Equal(t, []string{"a"}, cols1)

	thingWrapper := &thingyWrapper{}
	cols2, err := scan.Columns(thingWrapper)
	assert.NoError(t, err)
	assert.Equal(t, []string{"a", "b"}, cols2)
}

fails with

Expected :[]string{"a", "b"}
Actual   :[]string{"b"}
commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@blockloop I started looking into this and I got it working. But before I do any PR I would like to know if these test cases for columns would be correct?

func TestColumnsAddsComplexTypesWhenStructTag(t *testing.T) {
	type person struct {
		Address struct {
			Street string
		} `db:"address"`
	}

	cols, err := Columns(&person{})
	assert.NoError(t, err)
	assert.EqualValues(t, []string{"address", "Street"}, cols)
}

func TestColumnsIgnoresComplexTypesWhenNoStructTag(t *testing.T) {
	type person struct {
		Address struct {
			Street string
		}
	}

	cols, err := Columns(&person{})
	assert.NoError(t, err)
	assert.EqualValues(t, []string{"Street"}, cols)
}