Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Layer Diagram

Detailed description of each architectural layer in ModelSDK Go.

1. Command Layer (cmd/)

PackagePurpose
cmd/mxcliCLI entry point using Cobra framework; includes LSP server, Docker integration, diagnostics
cmd/codegenMetamodel code generator from reflection data

Key CLI subcommands:

SubcommandFilePurpose
execcmd_exec.goExecute MDL script files
checkcmd_check.goSyntax and reference validation
lintcmd_lint.goRun linting rules
reportcmd_report.goBest practices report
testcmd_test_run.goRun .test.mdl / .test.md tests
diffcmd_diff.goCompare script against project
sqlcmd_sql.goExternal SQL queries
lsplsp.goLanguage Server Protocol server
initinit.goProject initialization
dockerdocker.goDocker build/check/OQL integration
diagdiag.goSession logs, bug report bundles

2. MDL Layer (mdl/)

The MDL (Mendix Definition Language) layer provides a SQL-like interface for querying and modifying Mendix models.

sequenceDiagram
    participant User
    participant REPL
    participant Parser
    participant Visitor
    participant Executor
    participant SDK

    User->>REPL: "SHOW ENTITIES IN MyModule"
    REPL->>Parser: Parse MDL command
    Parser->>Visitor: Walk parse tree
    Visitor->>Executor: Build AST node
    Executor->>SDK: ListDomainModels()
    SDK-->>Executor: []*DomainModel
    Executor-->>REPL: Formatted output
    REPL-->>User: Display table
PackagePurpose
mdl/grammarANTLR4 lexer/parser (generated from MDLLexer.g4 + MDLParser.g4)
mdl/astAST node types for MDL statements
mdl/visitorANTLR listener that builds AST from parse tree
mdl/executorExecutes AST nodes against the SDK (~45k lines across 40+ files)
mdl/catalogSQLite-based catalog for querying project metadata
mdl/linterExtensible linting framework with built-in rules and Starlark scripting
mdl/replInteractive REPL interface

3. High-Level API Layer (api/)

The api/ package provides a simplified, fluent builder API inspired by the Mendix Web Extensibility Model API.

classDiagram
    class ModelAPI {
        +writer *Writer
        +reader *Reader
        +currentModule *Module
        +DomainModels *DomainModelsAPI
        +Microflows *MicroflowsAPI
        +Pages *PagesAPI
        +Enumerations *EnumerationsAPI
        +Modules *ModulesAPI
        +New(writer) *ModelAPI
        +SetModule(module) *ModelAPI
    }

    class EntityBuilder {
        +Persistent() *EntityBuilder
        +NonPersistent() *EntityBuilder
        +WithStringAttribute(name, length) *EntityBuilder
        +WithIntegerAttribute(name) *EntityBuilder
        +Build() (*Entity, error)
    }

    class MicroflowBuilder {
        +WithParameter(name, entity) *MicroflowBuilder
        +WithStringParameter(name) *MicroflowBuilder
        +ReturnsBoolean() *MicroflowBuilder
        +Build() (*Microflow, error)
    }

    ModelAPI --> EntityBuilder : creates
    ModelAPI --> MicroflowBuilder : creates
FilePurpose
api/api.goModelAPI entry point with namespace access
api/domainmodels.goEntityBuilder, AssociationBuilder, AttributeBuilder
api/enumerations.goEnumerationBuilder, EnumValueBuilder
api/microflows.goMicroflowBuilder with parameters and return types
api/pages.goPageBuilder, widget builders
api/modules.goModulesAPI for module retrieval

4. SDK Layer (sdk/)

The SDK layer provides Go types and APIs for Mendix model elements.

classDiagram
    class Reader {
        +Open(path) Reader
        +ListModules() []*Module
        +ListDomainModels() []*DomainModel
        +ListMicroflows() []*Microflow
        +ListPages() []*Page
        +FindCustomWidgetType(widgetID) *RawCustomWidgetType
        +Close()
    }

    class Writer {
        +OpenForWriting(path) Writer
        +AddEntity(dm, entity)
        +AddAssociation(dm, assoc)
        +Save()
        +Close()
    }

    class DomainModel {
        +ID
        +ContainerID
        +Entities []*Entity
        +Associations []*Association
    }

    class Entity {
        +ID
        +Name
        +Attributes []*Attribute
        +Source string
        +OqlQuery string
    }

    Reader --> DomainModel
    Writer --> DomainModel
    DomainModel --> Entity
PackagePurpose
sdk/mpr/MPR file format handling (~18k lines across reader, writer, parser files split by domain)
sdk/domainmodelEntity, Attribute, Association types
sdk/microflowsMicroflow, Activity types (60+ types)
sdk/pagesPage, Widget types (50+ types)
sdk/widgetsEmbedded widget templates for pluggable widgets

The sdk/mpr/ package is split by domain for maintainability:

File PatternPurpose
reader.go, reader_*.goRead-only MPR access, split by element type
writer.go, writer_*.goRead-write MPR modification (domainmodel, microflow, security, widgets, etc.)
parser.go, parser_*.goBSON parsing and deserialization (domainmodel, microflow, etc.)
utils.goUUID generation utilities

5. Model Layer (model/)

Core types shared across the SDK:

classDiagram
    class ID {
        <<type alias>>
        string
    }

    class BaseElement {
        +ID ID
        +TypeName string
    }

    class QualifiedName {
        +Module string
        +Name string
    }

    class Module {
        +BaseElement
        +Name string
        +FromAppStore bool
    }

    class Text {
        +Translations map
        +GetTranslation(lang) string
    }

    BaseElement <|-- Module

6. External SQL Layer (sql/)

The sql/ package provides external database connectivity for querying PostgreSQL, Oracle, and SQL Server databases.

flowchart LR
    subgraph "MDL Commands"
        CONNECT["SQL CONNECT"]
        QUERY["SQL alias SELECT ..."]
        IMPORT["IMPORT FROM alias ..."]
        GENERATE["SQL alias GENERATE CONNECTOR"]
    end

    subgraph "sql/ Package"
        CONN[Connection Manager]
        QE[Query Engine]
        IMP[Import Pipeline]
        GEN[Connector Generator]
        META[Schema Introspection]
        FMT[Output Formatters]
    end

    subgraph "Databases"
        PG[(PostgreSQL)]
        ORA[(Oracle)]
        MSSQL[(SQL Server)]
    end

    CONNECT --> CONN
    QUERY --> QE
    IMPORT --> IMP
    GENERATE --> GEN

    CONN --> PG
    CONN --> ORA
    CONN --> MSSQL
    QE --> CONN
    IMP --> CONN
    GEN --> META
    META --> CONN
FilePurpose
driver.goDriverName type, ParseDriver()
connection.goManager, Connection, credential isolation
config.goDSN resolution (env vars, YAML config)
query.goExecute() – query via database/sql
meta.goShowTables(), DescribeTable() via information_schema
import.goIMPORT pipeline: batch insert, ID generation, sequence tracking
generate.goDatabase Connector MDL generation from external schema
typemap.goSQL to Mendix type mapping, DSN to JDBC URL conversion
mendix.goMendix DB DSN builder, table/column name helpers
format.goTable and JSON output formatters

7. VS Code Extension (vscode-mdl/)

The VS Code extension provides MDL language support via an LSP client that communicates with mxcli lsp --stdio.

graph TB
    subgraph "VS Code"
        EXT[extension.ts]
        TREE[Project Tree Provider]
        LINK[Terminal Link Provider]
        CONTENT[MDL Content Provider]
        PREVIEW[Preview Provider]
    end

    subgraph "Preview Renderers"
        DMRENDER[Domain Model]
        MFRENDER[Microflow]
        PGRENDER[Page Wireframe]
        MODRENDER[Module Overview]
        QPRENDER[Query Plan]
    end

    subgraph "mxcli"
        LSP[LSP Server]
    end

    EXT --> TREE
    EXT --> LINK
    EXT --> CONTENT
    EXT --> PREVIEW
    PREVIEW --> DMRENDER
    PREVIEW --> MFRENDER
    PREVIEW --> PGRENDER
    PREVIEW --> MODRENDER
    PREVIEW --> QPRENDER
    EXT -->|stdio| LSP

LSP features include syntax highlighting, parse/semantic diagnostics, completion, symbols, folding, hover, go-to-definition, clickable terminal links, and context menu commands.

8. LSP Server (cmd/mxcli/lsp*.go)

The LSP server is embedded in the mxcli binary:

FilePurpose
lsp.goMain LSP server, hover, go-to-definition
lsp_diagnostics.goParse and semantic error reporting
lsp_completion.goContext-aware completions
lsp_completions_gen.goGenerated completion data
lsp_symbols.goDocument symbols
lsp_folding.goCode folding ranges
lsp_hover.goHover information
lsp_helpers.goShared utilities