Documentation
¶
Overview ¶
Package clientgen provides a registry of language-specific client code generators.
The generator interface supports pluggable, language-specific code generators that produce type-safe client code from authorization schemas. Generators return a file map to support languages that need multiple output files (e.g., TypeScript with separate types.ts, client.ts, index.ts).
This is an internal package used by the melange CLI. For programmatic code generation, use pkg/clientgen which provides a stable public API.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HeaderVersion ¶ added in v0.11.0
HeaderVersion returns the melange version a generated file's header records, or "".
func Normalize ¶ added in v0.11.0
Normalize returns content as compared with a fresh generation: CRLF line endings converted to LF, the melange version and source lines of the leading comment block dropped (they change with the melange release and the directory generation ran from), and trailing whitespace at the end removed.
func Register ¶
func Register(g Generator)
Register adds a generator to the global registry. Generators should call this from their init() function.
Panics if a generator with the same name is already registered.
func Registered ¶
Registered returns true if a generator is registered for the given name.
Types ¶
type Config ¶
type Config struct {
// Package is the package/module name for generated code.
// For Go: package name (e.g., "authz")
// For TypeScript: not typically used (uses exports)
Package string
// Exposure is the SQL API exposure the database is migrated with (the
// sql section of melange.yaml). Accessible function names are emitted
// for the functions it installs for its exposed relations, and unless
// Relations is set its relations get constants. nil is the default
// exposure: every relation and every function family.
Exposure *compiler.Exposure
// Relations selects the relations that get constants, with the pattern
// syntax of Exposure.Relations. nil follows Exposure. Accessible function
// names are emitted only for relations both select.
Relations *compiler.RelationPatterns
// RelationFilter is a relation name prefix applied after Exposure and
// Relations, to relation constants and accessible function names alike.
// If empty, it selects every relation.
// Example: "can_" generates only permission relations, omitting roles.
// It is kept for compatibility; prefer Relations with an include pattern
// such as "can_*".
RelationFilter string
// IDType specifies the type to use for object IDs in constructors.
// For Go: "string", "int64", "uuid.UUID", etc.
// Other languages may ignore this or use their own type mappings.
IDType string
// Version is the melange version used for generation.
// Included in the generated file header for traceability.
// Example: "v0.4.3"
Version string
// SourcePath is the path to the source schema file.
// Included in the generated file header for traceability.
// Example: "schemas/schema.fga"
SourcePath string
// Options holds language-specific configuration.
// Each generator documents its supported options.
Options map[string]any
}
Config holds language-agnostic generation options.
Each generator may interpret these options differently based on language conventions. Language-specific options can be passed via the Options map.
type FileDrift ¶ added in v0.11.0
type FileDrift struct {
File string `json:"file"`
// Status is "missing" or "differs".
Status string `json:"status"`
// Line is the first line of the file on disk that differs (compared
// after Normalize); 0 when missing.
Line int `json:"line,omitempty"`
// OnDiskVersion is the melange version in the file's header, "" when
// it has none.
OnDiskVersion string `json:"on_disk_version,omitempty"`
}
FileDrift is a generated file that differs from what generation produces.
func CompareFiles ¶ added in v0.11.0
CompareFiles compares the generated files want, by name, with the files of the same names in dir, after Normalize. Other files in dir are ignored. The result is sorted by file name.
type Generator ¶
type Generator interface {
// Name returns the runtime identifier ("go", "typescript").
// This is used as the value for --runtime in the CLI.
Name() string
// Generate returns a map of filename -> content for all generated files.
// For single-file outputs (like Go), returns a single entry.
// For multi-file outputs (like TypeScript), returns multiple entries.
//
// The filenames are relative paths (e.g., "schema_gen.go" or "types.ts").
// The caller is responsible for writing these to the appropriate location.
Generate(types []schema.TypeDefinition, cfg *Config) (map[string][]byte, error)
// DefaultConfig returns the default configuration for this generator.
// This is used when the user doesn't provide explicit configuration.
DefaultConfig() *Config
}
Generator produces language-specific client code from a schema.
Implementations should be registered via Register() in their init() function. The CLI uses the registry to dispatch generation based on the --runtime flag.
type Identifiers ¶ added in v0.11.0
type Identifiers struct {
// contains filtered or unexported fields
}
Identifiers records the top-level identifiers of a generated file, so a generator reports two schema names that map to one identifier instead of emitting code that does not compile.
func NewIdentifiers ¶ added in v0.11.0
func NewIdentifiers(valid func(string) bool) *Identifiers
NewIdentifiers returns an empty set whose identifiers must satisfy valid.
func (*Identifiers) Declare ¶ added in v0.11.0
func (ids *Identifiers) Declare(ident, what string) error
Declare records ident for what, the schema element it is generated from. It fails when ident is not a valid identifier or already names something else.
type Model ¶ added in v0.11.0
type Model struct {
// ObjectTypes is every type of the schema, sorted.
ObjectTypes []string
// Relations is the relation names selected on at least one type, sorted.
Relations []string
// Selected is every selected relation, in model order. The generators
// emit relation names only; checks compare the pairs with a database.
Selected []RelationRef
// Accessible is the accessible_<t>_<r> functions installed for the
// selected relations, sorted by type and relation.
Accessible []compiler.AccessibleFunction
// AccessibleByParent is the parent-key accessible_<t>_<r>_by_<link>
// functions installed for the selected relations, sorted by type,
// relation and link.
AccessibleByParent []compiler.AccessibleFunction
}
Model is what a generator emits for a schema under a Config.
func Resolve ¶ added in v0.11.0
func Resolve(types []schema.TypeDefinition, cfg *Config) (*Model, error)
Resolve validates types as a migration does, compiles them under cfg.Exposure, and selects what the generators emit: every type, the relations cfg selects, and the accessible functions installed for them. A nil cfg is the zero Config.
Names are computed over the whole model and then filtered, as migrate does, so the selection never renames a function.
type RelationRef ¶ added in v0.11.0
type RelationRef struct {
Type, Relation string
}
RelationRef names a relation of a type.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package gogen implements the Go client code generator for melange.
|
Package gogen implements the Go client code generator for melange. |
|
Package typescript implements the TypeScript client code generator for melange.
|
Package typescript implements the TypeScript client code generator for melange. |