opral / inlang-sdk

0 stars 0 forks source link

data model around selectors and `variant.match` are ambiguous #198

Closed samuelstroschein closed 1 month ago

samuelstroschein commented 1 month ago

Problem

Matchers reference a key (name) to match against but selectors are expressions and expressions have no key to reference from.

Understanding Match and Selector Definitions in Data Structures 🧩

const declarations = [
  {
    "type": "input", 
    "name": "photoCount"
  }
]

const selectors = [
  { 
    type: "expression",
    arg: "photoCount",
    annotation: "plural"
  } 
]

const variant = {
  match: {
    // 💥 photoCount is matching the input variable 'photoCount'
    //    not the selector that references photo counnt
    "photoCount": "many"
  }
}
samuelstroschein commented 1 month ago

The straightforward solution is to always match against a declaration, not an expression.

const declarations = [
  {
    "type": "input", 
    "name": "photoCount"
  },
+  {
+    "type": "local", 
+    "name": "photoCountPlural",
+    "value": "photoCount: plural"
+  }
]

-const selectors = [
-  { 
-    type: "expression",
-    arg: "photoCount",
-    annotation: "plural"
-  } 
-]

const variant = {
  match: {
-    "photoCount": "many"
+    "photoCountPlural": "many",

     // even using both matchers in combination is possible! 
     "photoCount": "16"

  }
}
samuelstroschein commented 1 month ago

The UI ran into this ambiguity bug (cc @nils.jacobsen), confirming this problem.

CleanShot 2024-09-18 at 12.36.50.mp4

Having matchOrder: ["photoCountPlural"] would still be required. As a nice side effect, re-ordering in the UI becomes trivial. One can drag & drop one matcher from left to right.

samuelstroschein commented 1 month ago

Closed in https://github.com/opral/monorepo/commit/62db9ca11b4f8830a33c22bf8700505ce5acd585

The change was trivial. Selectors stay as they are but don't use an expression, only a variable reference.

CleanShot 2024-09-20 at 22.26.40@2x.png