Documentation
¶
Overview ¶
Package tqlgen provides tools for parsing TypeQL schemas and generating Go code from them.
Package tqlgen provides utilities for transforming TypeDB names into Go-idiomatic names.
Package tqlgen provides code generation from TypeQL schemas.
Index ¶
- Variables
- func ExtractAnnotations(input string) map[string]map[string]string
- func Render(w io.Writer, schema *ParsedSchema, cfg RenderConfig) error
- func ToPascalCase(name string) string
- func ToPascalCaseAcronyms(name string) string
- func ToSnakeCase(name string) string
- type Annotation
- type AsClause
- type AttrDef
- type AttributeSpec
- type CardAnnot
- type EntityClause
- type EntityDef
- type EntitySpec
- type FunBodyTok
- type FunDef
- type FunctionSpec
- type OwnsDef
- type OwnsSpec
- type ParameterSpec
- type ParsedSchema
- type PlaysDef
- type PlaysSpec
- type RangeAnnot
- type RegexAnnot
- type RelatesDef
- type RelatesSpec
- type RelationClause
- type RelationDef
- type RelationSpec
- type RenderConfig
- type SimpleDef
- type StructDefP
- type StructFieldP
- type StructFieldSpec
- type StructSpec
- type SubClause
- type TQLFileSimple
- type ValuesAnnot
Constants ¶
This section is empty.
Variables ¶
var CommonAcronyms = map[string]string{
"id": "ID",
"url": "URL",
"uuid": "UUID",
"api": "API",
"http": "HTTP",
"iid": "IID",
"nf": "NF",
}
CommonAcronyms defines a set of common abbreviations that should be fully uppercased when generating Go names.
Functions ¶
func ExtractAnnotations ¶
ExtractAnnotations parses comment annotations of the form "# @key value" from schema text. Returns a map of type name -> annotation map.
func Render ¶
func Render(w io.Writer, schema *ParsedSchema, cfg RenderConfig) error
Render processes a ParsedSchema and writes the generated Go source code to the provided writer.
func ToPascalCase ¶
ToPascalCase transforms a kebab-case or snake_case string into PascalCase.
func ToPascalCaseAcronyms ¶
ToPascalCaseAcronyms transforms a string into PascalCase while preserving the casing of common Go acronyms.
func ToSnakeCase ¶
ToSnakeCase transforms a kebab-case string into snake_case.
Types ¶
type Annotation ¶
type Annotation struct {
Key bool `parser:" @'@key'"`
Unique bool `parser:"| @'@unique'"`
Card *CardAnnot `parser:"| @@"`
Regex *RegexAnnot `parser:"| @@"`
Values *ValuesAnnot `parser:"| @@"`
Range *RangeAnnot `parser:"| @@"`
}
Annotation parses: @key, @unique, @abstract, @card(...), @regex(...), @values(...), @range(...)
type AsClause ¶
type AsClause struct {
Parent string `parser:"'as' @Ident"`
}
AsClause parses: as parent-role
type AttrDef ¶
type AttrDef struct {
Name string `parser:"'attribute' @Ident ','?"`
ValueType string `parser:"'value' @Ident"`
Annots []Annotation `parser:"@@*"`
Semi string `parser:"';'"`
}
AttrDef parses: attribute name [,] value type [@constraint(...)];
type AttributeSpec ¶
type AttributeSpec struct {
// Name is the name of the attribute type.
Name string
// ValueType is the base value type of the attribute (string, integer, double, boolean, datetime).
ValueType string
// Regex is an optional regular expression constraint for the attribute values.
Regex string
// Values is an optional list of allowed values (enumeration constraint).
Values []string
// RangeOp is an optional range constraint (e.g., "1..5").
RangeOp string
}
AttributeSpec describes a TypeQL attribute definition.
type CardAnnot ¶
type CardAnnot struct {
Expr string `parser:"'@card' '(' @CardExpr ')'"`
}
CardAnnot parses: @card(expr)
type EntityClause ¶
EntityClause is one of: owns or plays.
type EntityDef ¶
type EntityDef struct {
Name string `parser:"'entity' @Ident"`
Parent *SubClause `parser:"@@?"`
Abstract bool `parser:"@'@abstract'?"`
Comma string `parser:"','?"`
Clauses []EntityClause `parser:"( @@ ( ',' @@ )* )? ';'"`
}
EntityDef parses: entity name [sub parent] [@abstract] [, clause...];
type EntitySpec ¶
type EntitySpec struct {
// Name is the name of the entity type.
Name string
// Parent is the name of the parent entity type if this is a subtype.
Parent string
// Abstract indicates whether the entity type is defined as abstract.
Abstract bool
// Owns is a list of attributes owned by this entity type.
Owns []OwnsSpec
// Plays is a list of relation roles this entity type can play.
Plays []PlaysSpec
}
EntitySpec describes a TypeQL entity definition.
type FunBodyTok ¶
type FunBodyTok struct {
Tok string `parser:"@(Ident | Keyword | Punct | String | CardExpr | AnnotKW | Var | Arrow | Operator)"`
}
FunBodyTok matches every token type EXCEPT FunKW. When the parser hits the next `fun`, it exits the current FunDef and starts a new one.
type FunDef ¶
type FunDef struct {
Name string `parser:"FunKW @Ident"`
Body []FunBodyTok `parser:"@@*"`
}
FunDef parses: fun name <body tokens until next fun or EOF> The body is captured as a flat list of tokens for signature extraction.
type FunctionSpec ¶
type FunctionSpec struct {
// Name is the name of the function.
Name string
// Parameters is a list of parameters accepted by the function.
Parameters []ParameterSpec
// ReturnType is the TypeQL return type of the function.
ReturnType string
}
FunctionSpec describes the signature of a TypeQL function definition.
type OwnsDef ¶
type OwnsDef struct {
Attribute string `parser:"'owns' @Ident"`
Annots []Annotation `parser:"@@*"`
}
OwnsDef parses: owns attr-name [@key] [@unique] [@card(...)]
type OwnsSpec ¶
type OwnsSpec struct {
// Attribute is the name of the attribute type being owned.
Attribute string
// Key indicates whether this attribute is a key for the owner.
Key bool
// Unique indicates whether the attribute value must be unique across all instances.
Unique bool
// Card specifies the cardinality of the ownership (e.g., "0..1", "1..5").
Card string
}
OwnsSpec describes an "owns attribute" clause in an entity or relation definition.
type ParameterSpec ¶
type ParameterSpec struct {
// Name is the name of the parameter (without the '$' prefix).
Name string
// TypeName is the TypeQL type name expected for this parameter.
TypeName string
}
ParameterSpec describes a single parameter of a TypeQL function.
type ParsedSchema ¶
type ParsedSchema struct {
// Attributes is a list of all attribute definitions in the schema.
Attributes []AttributeSpec
// Entities is a list of all entity definitions in the schema.
Entities []EntitySpec
// Relations is a list of all relation definitions in the schema.
Relations []RelationSpec
// Functions is a list of all function signatures in the schema.
Functions []FunctionSpec
// Structs is a list of all struct definitions in the schema.
Structs []StructSpec
}
ParsedSchema holds all components extracted from a TypeQL schema file, including attribute, entity, and relation definitions, as well as functions and structs.
func ParseSchema ¶
func ParseSchema(input string) (*ParsedSchema, error)
ParseSchema parses a TypeQL schema string into a ParsedSchema structure. It handles attribute, entity, relation, function, and struct definitions. Function blocks are parsed by the grammar natively — no pre-processing needed.
func ParseSchemaFile ¶
func ParseSchemaFile(path string) (*ParsedSchema, error)
ParseSchemaFile reads a TypeQL schema from the specified file path and parses it.
func (*ParsedSchema) AccumulateInheritance ¶
func (s *ParsedSchema) AccumulateInheritance()
AccumulateInheritance propagates owns/plays from parent entities/relations to their children, so each child has the complete set of fields.
type PlaysDef ¶
type PlaysDef struct {
Relation string `parser:"'plays' @Ident"`
Role string `parser:"':' @Ident"`
}
PlaysDef parses: plays relation:role
type PlaysSpec ¶
type PlaysSpec struct {
// Relation is the name of the relation type.
Relation string
// Role is the name of the role played by the owner within that relation.
Role string
}
PlaysSpec describes a "plays relation:role" clause.
type RangeAnnot ¶
type RangeAnnot struct {
Expr string `parser:"'@range' '(' @CardExpr ')'"`
}
RangeAnnot parses: @range(expr)
type RegexAnnot ¶
type RegexAnnot struct {
Pattern string `parser:"'@regex' '(' @String ')'"`
}
RegexAnnot parses: @regex("pattern")
type RelatesDef ¶
type RelatesDef struct {
Role string `parser:"'relates' @Ident"`
AsParent *AsClause `parser:"@@?"`
Annots []Annotation `parser:"@@*"`
}
RelatesDef parses: relates role-name [as parent-role] [@card(...)]
type RelatesSpec ¶
type RelatesSpec struct {
// Role is the name of the role in the relation.
Role string
// AsParent specifies an overridden role from a parent relation type.
AsParent string
// Card specifies the cardinality of players allowed for this role.
Card string
}
RelatesSpec describes a "relates role" clause in a relation definition.
type RelationClause ¶
type RelationClause struct {
Relates *RelatesDef `parser:" @@"`
Owns *OwnsDef `parser:"| @@"`
Plays *PlaysDef `parser:"| @@"`
}
RelationClause is one of: relates, owns, or plays.
type RelationDef ¶
type RelationDef struct {
Name string `parser:"'relation' @Ident"`
Parent *SubClause `parser:"@@?"`
Abstract bool `parser:"@'@abstract'?"`
Comma string `parser:"','?"`
Clauses []RelationClause `parser:"( @@ ( ',' @@ )* )? ';'"`
}
RelationDef parses: relation name [sub parent] [@abstract] [, clause...];
type RelationSpec ¶
type RelationSpec struct {
// Name is the name of the relation type.
Name string
// Parent is the name of the parent relation type if this is a subtype.
Parent string
// Abstract indicates whether the relation type is defined as abstract.
Abstract bool
// Relates is a list of roles involved in this relation.
Relates []RelatesSpec
// Owns is a list of attributes owned by this relation type.
Owns []OwnsSpec
// Plays is a list of relation roles this relation type can play.
Plays []PlaysSpec
}
RelationSpec describes a TypeQL relation definition.
type RenderConfig ¶
type RenderConfig struct {
// PackageName is the name of the Go package for the generated code.
PackageName string
// ModulePath is the module import path for the 'gotype' package.
ModulePath string
// UseAcronyms, if true, applies Go acronym naming conventions (e.g., 'ID' instead of 'Id').
UseAcronyms bool
// SkipAbstract, if true, excludes abstract TypeDB types from the generated Go code.
SkipAbstract bool
// SchemaVersion is an optional string included in the generated file header.
SchemaVersion string
}
RenderConfig specifies the settings for generating Go code from a TypeQL schema.
func DefaultConfig ¶
func DefaultConfig() RenderConfig
DefaultConfig returns a standard RenderConfig with sensible defaults.
type SimpleDef ¶
type SimpleDef struct {
Attribute *AttrDef `parser:" @@"`
Entity *EntityDef `parser:"| @@"`
Relation *RelationDef `parser:"| @@"`
Struct *StructDefP `parser:"| @@"`
Fun *FunDef `parser:"| @@"`
}
SimpleDef represents a single top-level definition within a TypeQL define block.
type StructDefP ¶
type StructDefP struct {
Name string `parser:"'struct' @Ident (':' | ',')"`
Fields []StructFieldP `parser:"@@ (',' @@)* ','? ';'"`
}
StructDefP parses: struct name (: | ,) field [, field]* [,] ; Supports both official TypeQL syntax (`:` separator, `name value type`) and legacy syntax (`,` separator, `value name type`).
type StructFieldP ¶
type StructFieldP struct {
FieldName string `parser:"( @Ident 'value' | 'value' @Ident )"`
ValueType string `parser:"@Ident"`
Optional bool `parser:"@'?'?"`
}
StructFieldP parses a struct field in either official or legacy order:
- Official: field-name value type[?]
- Legacy: value field-name type[?]
type StructFieldSpec ¶
type StructFieldSpec struct {
// Name is the name of the field.
Name string
// ValueType is the TypeQL type of the field's value.
ValueType string
// Optional indicates whether the field is marked as optional in the schema.
Optional bool
}
StructFieldSpec describes a single field within a TypeQL struct.
type StructSpec ¶
type StructSpec struct {
// Name is the name of the struct type.
Name string
// Fields is a list of fields defined within the struct.
Fields []StructFieldSpec
}
StructSpec describes a TypeQL struct definition, which is a collection of named fields.
type SubClause ¶
type SubClause struct {
Parent string `parser:"'sub' @Ident"`
}
SubClause parses: sub parent-name
type TQLFileSimple ¶
type TQLFileSimple struct {
Define string `parser:"'define'"`
Definitions []SimpleDef `parser:"@@*"`
}
TQLFileSimple is the top-level grammar for a TypeQL define block.
type ValuesAnnot ¶
type ValuesAnnot struct {
Values []string `parser:"'@values' '(' @String ( ',' @String )* ')'"`
}
ValuesAnnot parses: @values("a", "b", ...)