reform icon indicating copy to clipboard operation
reform copied to clipboard

Support anonymous struct fields

Open ababo opened this issue 7 years ago • 4 comments

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.

ababo avatar Oct 29 '16 23:10 ababo

May you share your database schema?

AlekSi avatar Oct 30 '16 15:10 AlekSi

CREATE TABLE parents (
  id int PRIMARY KEY,
  name varchar(64)
);

CREATE TABLE children (
  desc text
) INHERITS (parents);

ababo avatar Oct 30 '16 15:10 ababo

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.

AlekSi avatar Oct 30 '16 21:10 AlekSi

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) :)

xaionaro avatar Feb 12 '17 09:02 xaionaro