|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package executor |
| 4 | + |
| 5 | +import "strings" |
| 6 | + |
| 7 | +// ============================================================================= |
| 8 | +// MDL keyword dispatch — native vs pluggable widgets, version-aware |
| 9 | +// ============================================================================= |
| 10 | +// |
| 11 | +// Some MDL widget keywords map to different Mendix widgets depending on the |
| 12 | +// project's Mendix version. The headline case is DATAGRID: |
| 13 | +// |
| 14 | +// Mendix 9.x – 10.x: native Forms$DataGrid |
| 15 | +// Mendix 11.0+: pluggable com.mendix.widget.web.datagrid.Datagrid (React) |
| 16 | +// |
| 17 | +// Studio Pro's project upgrade does NOT auto-convert native widgets to their |
| 18 | +// pluggable replacements. Migrated 11+ projects can have both stacks coexisting |
| 19 | +// on the same page. The grammar therefore exposes both stacks via distinct |
| 20 | +// keywords: |
| 21 | +// |
| 22 | +// DATAGRID — version-default (auto-picks per Mendix version) |
| 23 | +// LEGACYDATAGRID — always native (deprecated on 11+, still allowed) |
| 24 | +// |
| 25 | +// (PLUGGABLEWIDGET 'com.mendix.widget...' name remains the explicit escape hatch.) |
| 26 | +// |
| 27 | +// This table is hand-maintained editorial policy data — it expresses our |
| 28 | +// preference for which stack a generic keyword resolves to per Mendix version, |
| 29 | +// not a Mendix-published mapping. |
| 30 | + |
| 31 | +// keywordBindingKind names the runtime stack a binding produces. |
| 32 | +type keywordBindingKind string |
| 33 | + |
| 34 | +const ( |
| 35 | + bindingKindNative keywordBindingKind = "native" |
| 36 | + bindingKindPluggable keywordBindingKind = "pluggable" |
| 37 | +) |
| 38 | + |
| 39 | +// keywordBinding is one (version-range, target) entry for a keyword. |
| 40 | +type keywordBinding struct { |
| 41 | + // MinVersion is the inclusive lower bound (e.g. "11.0.0"). Empty means |
| 42 | + // "all versions ≤ MaxVersion". |
| 43 | + MinVersion string |
| 44 | + // MaxVersion is the inclusive upper bound (e.g. "10.99.99"). Empty means |
| 45 | + // "all versions ≥ MinVersion". |
| 46 | + MaxVersion string |
| 47 | + // Kind is "native" or "pluggable". |
| 48 | + Kind keywordBindingKind |
| 49 | + // WidgetID is the pluggable widget id (only set when Kind == pluggable). |
| 50 | + WidgetID string |
| 51 | + // DeprecatedFrom marks the version (inclusive) at which this binding |
| 52 | + // becomes deprecated. Empty means "not deprecated". Used by mxcli check |
| 53 | + // --post-migration to flag legacy-stack widgets on newer projects. |
| 54 | + DeprecatedFrom string |
| 55 | +} |
| 56 | + |
| 57 | +// keywordMapping is the list of bindings for one MDL keyword. |
| 58 | +type keywordMapping struct { |
| 59 | + Keyword string |
| 60 | + Bindings []keywordBinding |
| 61 | +} |
| 62 | + |
| 63 | +// keywordDispatchTable is the editorial policy data for native-vs-pluggable |
| 64 | +// keyword resolution. Maintained by hand; updated when a Mendix version |
| 65 | +// promotes a new pluggable widget to be the default for a generic keyword. |
| 66 | +// |
| 67 | +// Today the entries are minimal: DATAGRID always resolves to pluggable |
| 68 | +// (Datagrid 2.x has been the default since well before 11.0). LEGACYDATAGRID |
| 69 | +// is reserved for an explicit native-stack request, tracked separately under |
| 70 | +// Phase 2.1; the buildWidgetV3 switch returns a clear "not yet implemented" |
| 71 | +// for it until a native builder lands. |
| 72 | +// |
| 73 | +// The table structure leaves room for richer dispatch rules — future |
| 74 | +// version-aware splits, additional dual-stack widgets — without rewriting |
| 75 | +// the lookup logic. |
| 76 | +var keywordDispatchTable = []keywordMapping{ |
| 77 | + { |
| 78 | + Keyword: "DATAGRID", |
| 79 | + Bindings: []keywordBinding{ |
| 80 | + // Pluggable Datagrid 2.x has been the default since 9.18+; we |
| 81 | + // intentionally don't downgrade older versions to a hypothetical |
| 82 | + // native binding because we have no native builder to dispatch to. |
| 83 | + {MinVersion: "9.0.0", Kind: bindingKindPluggable, WidgetID: "com.mendix.widget.web.datagrid.Datagrid"}, |
| 84 | + }, |
| 85 | + }, |
| 86 | +} |
| 87 | + |
| 88 | +// resolveKeywordBinding returns the binding for the given keyword and project |
| 89 | +// version, or (nil, false) when the keyword has no entry in the dispatch |
| 90 | +// table. Callers fall back to existing per-keyword handling when no binding |
| 91 | +// is returned. |
| 92 | +// |
| 93 | +// Comparison is uppercase-insensitive on the keyword. Version comparison uses |
| 94 | +// SemVer-style segments (no pre-release / build metadata handling). |
| 95 | +func resolveKeywordBinding(keyword, version string) (*keywordBinding, bool) { |
| 96 | + upper := strings.ToUpper(keyword) |
| 97 | + for _, mapping := range keywordDispatchTable { |
| 98 | + if mapping.Keyword != upper { |
| 99 | + continue |
| 100 | + } |
| 101 | + for i := range mapping.Bindings { |
| 102 | + b := &mapping.Bindings[i] |
| 103 | + if versionInRange(version, b.MinVersion, b.MaxVersion) { |
| 104 | + return b, true |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return nil, false |
| 109 | +} |
| 110 | + |
| 111 | +// versionInRange returns true when version is within [min, max] (inclusive). |
| 112 | +// Empty min or max means "unbounded" on that side. Empty version returns true |
| 113 | +// only if both min and max are empty (a wildcard binding). |
| 114 | +func versionInRange(version, min, max string) bool { |
| 115 | + if version == "" { |
| 116 | + return min == "" && max == "" |
| 117 | + } |
| 118 | + if min != "" && compareVersion(version, min) < 0 { |
| 119 | + return false |
| 120 | + } |
| 121 | + if max != "" && compareVersion(version, max) > 0 { |
| 122 | + return false |
| 123 | + } |
| 124 | + return true |
| 125 | +} |
| 126 | + |
| 127 | +// compareVersion returns -1, 0, +1 by lexicographic comparison of dotted |
| 128 | +// integer segments (e.g. "10.18.0" vs "11.0.0"). Non-numeric segments fall |
| 129 | +// back to string comparison. Handles segment-count differences by treating |
| 130 | +// missing segments as zero. |
| 131 | +func compareVersion(a, b string) int { |
| 132 | + aParts := strings.Split(a, ".") |
| 133 | + bParts := strings.Split(b, ".") |
| 134 | + n := len(aParts) |
| 135 | + if len(bParts) > n { |
| 136 | + n = len(bParts) |
| 137 | + } |
| 138 | + for i := 0; i < n; i++ { |
| 139 | + var ai, bi int |
| 140 | + if i < len(aParts) { |
| 141 | + ai = parseIntOrZero(aParts[i]) |
| 142 | + } |
| 143 | + if i < len(bParts) { |
| 144 | + bi = parseIntOrZero(bParts[i]) |
| 145 | + } |
| 146 | + if ai < bi { |
| 147 | + return -1 |
| 148 | + } |
| 149 | + if ai > bi { |
| 150 | + return 1 |
| 151 | + } |
| 152 | + } |
| 153 | + return 0 |
| 154 | +} |
| 155 | + |
| 156 | +// parseIntOrZero parses a version segment as a non-negative integer. |
| 157 | +// Returns 0 for non-numeric or negative inputs (rare in real Mendix versions). |
| 158 | +func parseIntOrZero(s string) int { |
| 159 | + n := 0 |
| 160 | + for _, ch := range s { |
| 161 | + if ch < '0' || ch > '9' { |
| 162 | + return 0 |
| 163 | + } |
| 164 | + n = n*10 + int(ch-'0') |
| 165 | + } |
| 166 | + return n |
| 167 | +} |
| 168 | + |
| 169 | +// ============================================================================= |
| 170 | +// Public resolution API (consumed by inspection / DESCRIBE-side commands) |
| 171 | +// ============================================================================= |
| 172 | +// |
| 173 | +// Write-side routing in buildWidgetV3 stays in the switch — the existing |
| 174 | +// hand-coded builders already produce correct BSON for each keyword. The |
| 175 | +// dispatch table is consumed by: |
| 176 | +// - mxcli schema show <KEYWORD> (future inspection command) |
| 177 | +// - DESCRIBE-side BSON storage type → keyword resolution |
| 178 | +// - check --post-migration to flag legacy-stack widgets |
| 179 | + |
| 180 | +// KeywordResolution describes how an MDL keyword resolves for a given |
| 181 | +// Mendix version. Stable enough for tests and inspection commands. |
| 182 | +type KeywordResolution struct { |
| 183 | + Keyword string |
| 184 | + Version string |
| 185 | + Kind string // "native" or "pluggable" |
| 186 | + WidgetID string // pluggable widget id, empty for native |
| 187 | + DeprecatedFrom string // version where the binding becomes deprecated, empty otherwise |
| 188 | +} |
| 189 | + |
| 190 | +// ResolveKeyword returns how the given keyword resolves for the given |
| 191 | +// Mendix version, or (nil, false) when the keyword has no dispatch entry. |
| 192 | +func ResolveKeyword(keyword, version string) (*KeywordResolution, bool) { |
| 193 | + binding, ok := resolveKeywordBinding(keyword, version) |
| 194 | + if !ok { |
| 195 | + return nil, false |
| 196 | + } |
| 197 | + return &KeywordResolution{ |
| 198 | + Keyword: strings.ToUpper(keyword), |
| 199 | + Version: version, |
| 200 | + Kind: string(binding.Kind), |
| 201 | + WidgetID: binding.WidgetID, |
| 202 | + DeprecatedFrom: binding.DeprecatedFrom, |
| 203 | + }, true |
| 204 | +} |
0 commit comments