Fix supervised credentials and declare typed RPC records for canvas placement
This commit is contained in:
Vendored
+24
-2
@@ -7,8 +7,18 @@ export const qxDerived = (get) => ({ kind: "derived", get });
|
||||
export const decodeQxValue = (type, value, messages) => {
|
||||
if (type.kind === "builtin" && type.name === "unit")
|
||||
return null;
|
||||
if (type.kind === "optional" && !value)
|
||||
return null;
|
||||
if (!value)
|
||||
throw new Error("Missing QX wire value");
|
||||
if (type.kind === "record") {
|
||||
if (value.kind.case !== "objectValue")
|
||||
throw new Error("Expected QX record");
|
||||
const fields = value.kind.value.fields;
|
||||
if (Object.keys(fields).some((name) => !Object.hasOwn(type.fields, name)))
|
||||
throw new Error("Unexpected QX record field");
|
||||
return Object.fromEntries(Object.entries(type.fields).map(([name, field]) => [name, decodeQxValue(field, fields[name], messages)]));
|
||||
}
|
||||
if (type.kind === "optional")
|
||||
return value.kind.case === "nullValue" ? null : decodeQxValue(type.value, value, messages);
|
||||
if (type.kind === "list") {
|
||||
@@ -52,6 +62,18 @@ const requireMessage = (messages, id) => {
|
||||
export const encodeQxValue = (type, value, messages) => {
|
||||
if (type.kind === "builtin" && type.name === "unit")
|
||||
return jsToProtoValue(null);
|
||||
if (type.kind === "record") {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value))
|
||||
throw new Error("Expected QX record");
|
||||
if (Object.keys(value).some((name) => !Object.hasOwn(type.fields, name)))
|
||||
throw new Error("Unexpected QX record field");
|
||||
const fields = Object.fromEntries(Object.entries(type.fields).map(([name, field]) => {
|
||||
if (!Object.hasOwn(value, name) && field.kind !== "optional")
|
||||
throw new Error(`Missing QX record field ${name}`);
|
||||
return [name, encodeQxValue(field, value[name] ?? (field.kind === "optional" ? null : value[name]), messages)];
|
||||
}));
|
||||
return create(ValueSchema, { kind: { case: "objectValue", value: create(ObjectValueSchema, { fields }) } });
|
||||
}
|
||||
if (type.kind === "optional")
|
||||
return value === null ? jsToProtoValue(null) : encodeQxValue(type.value, value, messages);
|
||||
if (type.kind === "list")
|
||||
@@ -69,7 +91,7 @@ export const encodeQxValue = (type, value, messages) => {
|
||||
return jsToProtoValue(value);
|
||||
};
|
||||
const inputValue = (context, type) => {
|
||||
if (type.kind === "message")
|
||||
if (type.kind === "message" || type.kind === "record")
|
||||
return create(ValueSchema, { kind: { case: "objectValue",
|
||||
value: create(ObjectValueSchema, { fields: context.inputProto }) } });
|
||||
return context.inputProto.value;
|
||||
@@ -78,7 +100,7 @@ const inputFields = (type, value, messages) => {
|
||||
if (type.kind === "builtin" && type.name === "unit")
|
||||
return {};
|
||||
const encoded = encodeQxValue(type, value, messages);
|
||||
if (type.kind === "message") {
|
||||
if (type.kind === "message" || type.kind === "record") {
|
||||
if (encoded.kind.case !== "objectValue")
|
||||
throw new Error("Message inputs must encode an object value");
|
||||
return Object.fromEntries(Object.entries(encoded.kind.value.fields).map(([key, entry]) => [key, liveValue(entry)]));
|
||||
|
||||
Reference in New Issue
Block a user