reform
reform copied to clipboard
Support anonymous struct fields
I would like to make use of Postgresql inheritance. To do that it's logical to use anonymous struct fields:
//reform:parents
type Parent struct {
ID int `reform:"id,pk"`
Name string `reform:"name"`
}
//reform:children
type Child struct {
Parent
Desc string `reform:"desc"`
}
but that is not supported.
May you share your database schema?
CREATE TABLE parents (
id int PRIMARY KEY,
name varchar(64)
);
CREATE TABLE children (
desc text
) INHERITS (parents);
Anonymous fields were reserved for valid future use-case, and that sounds like a valid one.
Proposed syntax:
//reform:parents
type Parent struct {
ID int `reform:"id,pk"`
Name string `reform:"name"`
}
//reform:children
type Child struct {
Parent `reform:",embed"`
Desc string `reform:"desc"`
}
,embed
tag value will allow us to differentiate from future use-cases.
Please don't stop on anonymous fields only. I need in support of embedded structure even if it's not an anonymous field.
In my hacky dirty fork of "reform" I already added support of embedded structures, and it looks like this:
//reform:t2
type T2 struct {
Id int `reform:"id,pk"`
Var2 int `reform:"var2"`
Var3 T1 `reform:"var3,embedded:anotherfile.go"`
}
anotherfile.go:
type T1 struct {
Var1 int `reform:"var1"`
}
t2
columns will be: id
(.Id
), var2
(.Var2
) and var3__var1
(.Var3.Var1
).
Looking ahead, the most difficult problem was to find this structure (it may be in another file) :)