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

Add support for embedded structs #582

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions columns/columns_for_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func ForStructWithAlias(s interface{}, tableName string, tableAlias string) (col
}
}

appendStruct(columns, st)

return columns
}

func appendStruct(columns Columns, st reflect.Type) {
fieldCount := st.NumField()

for i := 0; i < fieldCount; i++ {
Expand All @@ -39,6 +45,9 @@ func ForStructWithAlias(s interface{}, tableName string, tableAlias string) (col
popTags := TagsFor(field)
tag := popTags.Find("db")

if tag.Options["inline"] {
appendStruct(columns, field.Type)
}
if !tag.Ignored() && !tag.Empty() {
col := tag.Value

Expand All @@ -58,6 +67,4 @@ func ForStructWithAlias(s interface{}, tableName string, tableAlias string) (col
}
}
}

return columns
}
23 changes: 23 additions & 0 deletions columns/columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/gobuffalo/nulls"
"github.com/gobuffalo/pop/v5/columns"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -81,3 +82,25 @@ func Test_Columns_Sorted(t *testing.T) {
r.Equal(c.String(), "amount, amount_units")
r.Equal(c.QuotedString(fooQuoter{}), "`amount`, `amount_units`")
}

type bar struct {
foo `db:"foo,inline"`
MiddleName string `db:"middle_name"`
Bio nulls.String `db:"bio"`
}

func Test_Columns_Embedded(t *testing.T) {
r := require.New(t)

c := columns.ForStruct(bar{}, "bar")

r.Equal(c.Cols["first_name"], &columns.Column{Name: "first_name", Writeable: false, Readable: true, SelectSQL: "first_name as f"})
r.Equal(c.Cols["middle_name"], &columns.Column{Name: "middle_name", Writeable: true, Readable: true, SelectSQL: "bar.middle_name"})
r.Equal(c.Cols["LastName"], &columns.Column{Name: "LastName", Writeable: true, Readable: true, SelectSQL: "bar.LastName"})
r.Equal(c.Cols["read"], &columns.Column{Name: "read", Writeable: false, Readable: true, SelectSQL: "bar.read"})
r.Equal(c.Cols["write"], &columns.Column{Name: "write", Writeable: true, Readable: false, SelectSQL: "bar.write"})
r.Equal(c.Cols["foo"], &columns.Column{Name: "foo", Writeable: true, Readable: true, SelectSQL: "bar.foo"})
// Ensure it's not including non-inlined structs (nulls.String)
r.NotContains(c.Cols, "String")
r.NotContains(c.Cols, "Valid")
}
14 changes: 10 additions & 4 deletions columns/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ var tags = "db rw select belongs_to has_many has_one fk_id primary_id order_by m

// Tag represents a field tag defined exclusively for pop package.
type Tag struct {
Value string
Name string
Value string
Name string
Options map[string]bool
}

// Empty validates if this pop tag is empty.
Expand Down Expand Up @@ -42,14 +43,19 @@ func (t Tags) Find(name string) Tag {
// in model field.
func TagsFor(field reflect.StructField) Tags {
pTags := Tags{}
attrs := map[string]bool{}
for _, tag := range strings.Fields(tags) {
if valTag := field.Tag.Get(tag); valTag != "" {
pTags = append(pTags, Tag{valTag, tag})
vals := strings.Split(valTag, ",")
for _, attr := range vals[1:] {
attrs[attr] = true
}
pTags = append(pTags, Tag{vals[0], tag, attrs})
}
}

if len(pTags) == 0 {
pTags = append(pTags, Tag{field.Name, "db"})
pTags = append(pTags, Tag{field.Name, "db", attrs})
}
return pTags
}