跳至內容
Astera Technology

輸出欄位改版後重新驗證接收器

按接收器版本核確切鍵集合,不把不兼容欄位補成零。

下載完整個案資料及參考答案 ↓

訂明本例驗收任務

假設接收器只支援兩個本機格式:v1 用 qty,v2 用 quantity。各須只有該一個鍵,值為非負整數;呼叫者列接收器版本。另一版鍵名屬不兼容,不補零;版本未知不猜。

  1. 用明示接收器版本
  2. 核確切回應鍵集合
  3. 格式兼容才讀值

格式不兼容,須記錄接收器及版本決定。

使用已填個案

原創假設輸入及確切答案鍵
個案已提供輸入預期結構化結果判斷及原因
v1 接收器遇 v2 鍵{"revision":"v1","response":{"quantity":3}}{"status":"incompatible"}欠 qty 不是已知零值,已訂版本不符。
v2 格式相符{"revision":"v2","response":{"quantity":3}}{"status":"accepted","quantity":3}確切 v2 鍵及整數值符合本規則。
兩版鍵同時提供{"revision":"v2","response":{"qty":3,"quantity":3}}{"status":"incompatible"}即使值相同,一鍵封套也不接雙重表示。
下載可編輯 CSV ↓

逐步處理 both-revision-keys 個案,須得到甚麼結果?原因是甚麼?

查看參考答案

{"status":"incompatible"}。即使值相同,一鍵封套也不接雙重表示。

執行完整本機驗算

另存為 .mjs,以 Node.js 執行;只驗提供的個案
import assert from 'node:assert/strict';

function evaluate(x){
 const key=x.revision==='v1'?'qty':x.revision==='v2'?'quantity':null;if(!key)return {status:'unknown-revision'};
 if(Object.keys(x.response).length!==1||!Object.hasOwn(x.response,key))return {status:'incompatible'};
 const value=x.response[key];return Number.isInteger(value)&&value>=0?{status:'accepted',quantity:value}:{status:'invalid-value'};
}

const cases = [
  {
    "id": "v1-meets-v2-response",
    "input": {
      "revision": "v1",
      "response": {
        "quantity": 3
      }
    },
    "expected": {
      "status": "incompatible"
    }
  },
  {
    "id": "v2-matched",
    "input": {
      "revision": "v2",
      "response": {
        "quantity": 3
      }
    },
    "expected": {
      "status": "accepted",
      "quantity": 3
    }
  },
  {
    "id": "both-revision-keys",
    "input": {
      "revision": "v2",
      "response": {
        "qty": 3,
        "quantity": 3
      }
    },
    "expected": {
      "status": "incompatible"
    }
  }
];
for (const c of cases) assert.deepEqual(evaluate(c.input), c.expected, c.id);
console.log(JSON.stringify({checked:cases.length,caseIds:cases.map(c=>c.id)}));
下載可執行例子 ↓

每題保存版本及原回應。此例不提供推出次序、不聲稱供應商支援這些鍵,也不批准寫入。

相關資源

系統開發服務 →