@mathrunet/masamune
Version:
Manages packages for the server portion (NodeJS) of the Masamune framework.
145 lines • 5.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FirestoreModelLocalizedValueConverter = exports.ModelLocalizedValueConverter = void 0;
const model_field_value_converter_1 = require("../model_field_value_converter");
const utils_1 = require("../../utils");
const model_field_value_1 = require("../model_field_value");
/**
* ModelLocalizedValue ModelFieldValueConverter.
*
* ModelLocalizedValue用のModelFieldValueConverter。
*/
class ModelLocalizedValueConverter extends model_field_value_converter_1.ModelFieldValueConverter {
/**
* ModelLocalizedValue ModelFieldValueConverter.
*
* ModelLocalizedValue用のModelFieldValueConverter。
*/
constructor() {
super();
}
type = "ModelLocalizedValue";
convertFrom(key, value, original) {
if (value !== null && typeof value === "object" && "@type" in value && value["@type"] === this.type) {
const localized = value["@localized"] ?? {};
const localizedValues = [];
for (const locale in localized) {
const split = locale.split("_");
const language = split[0];
const country = split.length > 1 ? split[1] : undefined;
localizedValues.push(new model_field_value_1.ModelLocalizedLocaleVaue({
language: language,
country: country,
value: localized[locale],
}));
}
return {
[key]: new model_field_value_1.ModelLocalizedValue(localizedValues, "server"),
};
}
return null;
}
convertTo(key, value, original) {
if (value instanceof model_field_value_1.ModelLocalizedValue) {
const loalizedMap = {};
for (const locale of value["@localized"]) {
loalizedMap[`${locale.language}${locale.country ? `_${locale.country}` : ""}`] = locale.value;
}
return {
[key]: {
"@type": this.type,
"@localized": loalizedMap,
"@source": value["@source"],
},
};
}
return null;
}
}
exports.ModelLocalizedValueConverter = ModelLocalizedValueConverter;
/**
* FirestoreConverter for [ModelLocalizedValue].
*
* [ModelLocalizedValue]用のFirestoreConverter。
*/
class FirestoreModelLocalizedValueConverter extends model_field_value_converter_1.FirestoreModelFieldValueConverter {
/**
* FirestoreConverter for [ModelLocalizedValue].
*
* [ModelLocalizedValue]用のFirestoreConverter。
*/
constructor() {
super();
}
type = "ModelLocalizedValue";
convertFrom(key, value, original, firestoreInstance) {
if (Array.isArray(value)) {
const targetKey = `#${key}`;
const targetMap = original[targetKey] ?? {};
const type = targetMap["@type"] ?? "";
if (type == this.type) {
const val = targetMap["@localized"] ?? {};
return {
[key]: {
"@type": this.type,
"@localized": val,
},
[targetKey]: null,
};
}
}
return null;
}
convertTo(key, value, original, firestoreInstance) {
if (value != null && typeof value === "object" && "@type" in value) {
const type = value["@type"] ?? "";
if (type === this.type) {
const fromUser = (value["@source"] ?? "") === "user";
const val = value["@localized"] ?? {};
const targetKey = `#${key}`;
const localizedMap = {};
for (const locale in val) {
localizedMap[locale] = val[locale];
}
const result = {
[targetKey]: {
"@type": this.type,
"@localized": localizedMap,
"@target": key,
},
[key]: val,
};
if (fromUser) {
// Convert to array format expected by Firestore for localized values
const localizedArray = [];
for (const locale in val) {
localizedArray.push(`${locale}:${val[locale]}`);
}
result[key] = localizedArray;
}
return result;
}
}
else if (Array.isArray(value)) {
const list = value.filter((e) => e != null && typeof e === "object" && "@type" in e);
if (list.length > 0 && list.every((e) => e["@type"] === this.type)) {
throw new Error("ModelLocalizedValue cannot be included in a listing or map. It must be placed in the top field.");
}
}
else if ((0, utils_1.isDynamicMap)(value)) {
const map = {};
for (const k in value) {
const v = value[k];
if (v != null && typeof v === "object" && "@type" in v) {
map[k] = v;
}
}
if (Object.keys(map).length > 0 && Object.values(map).every((e) => e["@type"] === this.type)) {
throw new Error("ModelLocalizedValue cannot be included in a listing or map. It must be placed in the top field.");
}
}
return null;
}
}
exports.FirestoreModelLocalizedValueConverter = FirestoreModelLocalizedValueConverter;
//# sourceMappingURL=model_localized_value_converter.js.map