Open iOliverNguyen opened 6 years ago
sqlgen:
from commentsRules about using sqlgen:
inside a comment:
sqlgen:
must appear at the start of the line (can be prefixed by other space characters). The following are valid://sqlgen: generate User
// sqlgen: generate User
// sqlgen:generate User
This one is INVALID:
// TEXT sqlgen: generate User
This one is also INVALID (notice missing :
):
// sqlgen generate User
/*
block or a block with multiple lines prefixed by //
. The two following are the same and are parsed as generate User from "user"
/* sqlgen: generate User
from "user"
*/
// sqlgen: generate User
// from "user"
But the following is treated as two distinct blocks, thus is parsed as generate User
(without the from "user"
part!).
/* sqlgen: generate User */
/* from "user" */
//
to start another (nested) comment.// sqlgen: generate User from "user" // TEXT
sqlgen:
annotation spans multiple lines until a comment line with only space characters.// TEXT
// sqlgen: generate User
// from "user" // TEXT
//
// TEXT
The above comment group is parsed as generate User from "user"
.
sqlgen:
annotation starts a new block, thus the following comment group is parsed as two distinct blocks and the second one is invalid.// sqlgen: generete User
// sqlgen: from "User"
sqlgen:
block immediate prefixes a type declaration is attached to the type, thus can get missing information from it.// sqlgen: generate from "user"
type User struct {}
The above is the same as generate User from "user"
while the standalone generate from "user"
is invalid (because it does not know which type to attach to).
// standalone, INVALID
// sqlgen: generate from "user"
type User struct{}
// standalone, VALID
// sqlgen: generate User from "user"
type User struct{}
sqlgen:
block can only be attached to global type declaration. It does not work on local type definition. The following is treated as standalone block.func Foo() {
// standalone, INVALID
// sqlgen: generate from "user"
type User struct{}
}
sqlgen:
block can be attached to a single type declaration.// VALID
// sqlgen: generate
type User struct {}
// VALID
// sqlgen: generate
type (
User struct{}
)
// INVALID
// sqlgen: generate
type (
type User struct {}
type Account struct{}
)
// INVALID
// sqlgen: generate
type (
type User struct {}
// sqlgen: generate
type Account struct{}
)
// VALID
type (
// sqlgen: generate
type User struct {}
// sqlgen: generate
type Account struct{}
)
The first prototype employs [goderive]() as DSL (example) for declaring code generation.
The next version should define its own DSL for more clarification and better error messages. The DSL can be put inside Go code or as separate files like protobuf. We don't intend to employ the whole SQL syntax, only a subset that makes sense.
Note that, unlike SQL, our keywords must be lowercase (
generate
) or uppercase (GENERATE
) but not mixed (or ~Generate
GeneRATE
~).Generate structs from table
Short-hand syntax. Both struct and table name can be inferred from the struct.
Full syntax with plural form:
Join between multiple tables
The above can be written in full syntax:
All statements in a single comment block
Full syntax: