You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Pretty printing is wanted features, but it is hard to implement by memefish users.
If SQL() will be enhanced to support pretty printing, it will be very useful.
It is better to be default behavior.
It should support also printing without newlines and indentation(current behavior)
It may better to support indentation configuration.
If it is used for formatter, parse-unparse should retain comments.
I think it is not mandatory for SQL builder usecases.
Proposal
Introduce structs to have format context and options.
// FormatContext is container of format option and current indentation.// Note: All methods of FormatContext must support nil receiver.typeFormatContextstruct {
// unexported
}
// FormatContextCompact is format context without newline and indentation.funcFormatContextCompact() *FormatContext// FormatContextPretty is format context with newline and configured indentation.funcFormatContextPretty(indentint) *FormatContext// newlineOr returns newline with indentation if formatOptionPretty is used.// Otherwise, it returns argument string.func (fc*FormatContext) newlineOr(sstring) string// indentScope executes inner function using FormatContext with deeper indentation.func (fc*FormatContext) indentScope(ffunc(fc*FormatContext) string) string
Conceptually enhance Node.SQL(), sqlJoin, sqlOpt to support FormatContext.
Migration
There is two method to migrate.
a) Change Node.SQL(), sqlJoin, sqlOpt to receive FormatContext
If breaking change is permitted, it is possible to change Node.SQL().
It may be possible to migrate using text manipulation like sed.
b) Keep Node.SQL() signature and use NodeContext.sqlContext(*FormatContext) and (*FormatContext).SQL(Node)
SQL() can be implemented using sqlContext(*FormatContext).
It dosn't introduce breaking changes
Migration can be incremental
It also require FormatContext version of sqlJoin, sqlOpt.
// SQL is entry point of pretty printing.
// If node implements NodeFormat, it calls NodeFormat.sqlContext() instead of Node.SQL().
// If it is called with nil receiver, FormatContextCompact() is used instead.
func (fc *FormatContext) SQL(node Node) string
// NodeFormat is Node with FormatContext support.
// If it is implemented, (*FormatContext).SQL() calls sqlContext() instead of SQL()
type NodeFormat interface {
Node
// sqlContext is Node.SQL() with FormatContext conceptually.
// If it is called with nil FormatContext, FormatContextCompact() is used instead.
// Note: It would become to Node.SQL() finally.
sqlContext(fmtCtx *FormatContext) string
}
Pretty printing is wanted features, but it is hard to implement by memefish users.
If
SQL()
will be enhanced to support pretty printing, it will be very useful.Proposal
Node.SQL()
,sqlJoin
,sqlOpt
to supportFormatContext
.Migration
There is two method to migrate.
a) Change
Node.SQL()
,sqlJoin
,sqlOpt
to receiveFormatContext
Node.SQL()
.sed
.b) Keep
Node.SQL()
signature and useNodeContext.sqlContext(*FormatContext)
and(*FormatContext).SQL(Node)
SQL()
can be implemented usingsqlContext(*FormatContext)
.FormatContext
version ofsqlJoin
,sqlOpt
.Typical implementation of
sqlContext()
before
after
The text was updated successfully, but these errors were encountered: