{"modulePath":"github.com/txn2/mcp-data-platform","version":"v1.171.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package scriptprintf\n\nPackage scriptprintf is the string formatting a managed script's \\`%\\` operator and str.format method run (#2048).\n\nStarlark's own \\`%\\` takes bare verbs only and its str.format takes no format spec, so \"%02X\" % b, \"%.2f\" % rate and \"{:\u003e10}\".format(name) fail at run time with \"unknown conversion\". Every Python-shaped language an author knows accepts them, and the failure surfaces on the first run that reaches the line, often a branch no draft took. This package implements the printf subset (flags - 0 + space #, width, precision, \\* for either) and Python's format-spec mini-language, and Rewrite routes a script's \\`%\\` and .format through them before the script is compiled. A left operand that is not a string, and a .format on anything but a string, go to Starlark's own.\n\nThe package knows nothing about runs or the platform; CheckPercent and CheckFormat let the authoring gates refuse a literal format string that would still fail, at save rather than mid-run.\n\n## Constants\n\n```go\nconst (\n\tPercentName = \"%\"\n\tFormatName  = \".format\"\n)\n```\nPercentName and FormatName are the globals Rewrite routes \\`%\\` and .format through. Neither is an identifier a script can write, so a script can neither call nor shadow them: the resolver looks a name up by its text and does not check that it could have been typed.\n\n```go\nconst MaxField = 1 \u003c\u003c 20\n```\nMaxField is the widest field and the longest precision a format may ask for (#2713). The text a field pads or a precision prints to is allocated whole, outside the run's memory budget until the next host call, so \"%100000000000000c\" asked one replica for a hundred terabytes and ended it (found by FuzzFormatting). Python refuses such a field too.\n\n## Functions\n\n```go\nfunc Bind(env starlark.StringDict) starlark.StringDict\n```\nBind returns env with PercentName and FormatName bound, for the module a rewritten file is compiled against.\n\n```go\nfunc CheckFormat(template string) error\n```\nCheckFormat reports why template would be refused by str.format, or nil. It reads the template alone: whether the arguments fit is the run's to find out.\n\n```go\nfunc CheckPercent(format string) error\n```\nCheckPercent reports why format would be refused as the left operand of \\`%\\`, or nil. It reads the format alone: whether the arguments fit is the run's to find out.\n\n```go\nfunc Format(template string, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)\n```\nFormat is s.format(\\*args, \\*\\*kwargs) for a string s.\n\n```go\nfunc Percent(x, y starlark.Value) (starlark.Value, error)\n```\nPercent is \\`x % y\\`: Python's printf-style formatting when x is a string, and Starlark's own operator otherwise.\n\n```go\nfunc ReplaceExprs(file *syntax.File, rewrite func(syntax.Expr) syntax.Expr)\n```\nReplaceExprs replaces, everywhere in file, each expression rewrite returns a replacement for, in place. The replacement's own subexpressions are visited after it, so a nested form (a + b + c) is replaced at every level. It is the walk Rewrite does, for another package's rewrite of the same kind.\n\n```go\nfunc Rewrite(file *syntax.File)\n```\nRewrite routes every \\`x % y\\`, \\`name %= y\\` and \\`x.format(...)\\` in file through PercentName and FormatName, in place. Positions are kept: the call is placed at the operator or the method name, so an error is reported where the script wrote it.\n\nIt has to be a rewrite. Starlark evaluates \\`%\\` and a string's methods inside the interpreter, with no hook a predeclared value can take, and the alternative is a fork of the interpreter.\n\nA \\`%=\\` whose target is not a plain name (d\\[\"k\"] %= v) is left to Starlark: rewriting it would evaluate the target twice.\n\n","path":"github.com/txn2/mcp-data-platform/internal/scriptprintf","name":"scriptprintf","synopsis":"Package scriptprintf is the string formatting a managed script's `%` operator and str.format method run (#2048).","isRedistributable":true}
