mapcomplete/Customizations/JSON/FromJSON.ts

100 lines
3.6 KiB
TypeScript
Raw Normal View History

import {AndOrTagConfigJson} from "./TagConfigJson";
2020-09-02 11:37:34 +02:00
import {And, Or, RegexTag, Tag, TagsFilter} from "../../Logic/Tags";
2020-10-27 01:01:34 +01:00
import {Utils} from "../../Utils";
2020-11-06 01:58:26 +01:00
import {Translation} from "../../UI/i18n/Translation";
export class FromJSON {
2020-09-12 23:15:17 +02:00
public static Translation(json: string | any): Translation {
if (json === undefined) {
return undefined;
}
if (typeof (json) === "string") {
2020-09-12 23:15:17 +02:00
return new Translation({"*": json});
}
if(json.render !== undefined){
console.error("Using a 'render' where a translation is expected. Content is", json.render);
throw "ERROR: using a 'render' where none is expected"
}
const tr = {};
2020-09-02 11:37:34 +02:00
let keyCount = 0;
for (let key in json) {
2020-09-12 23:15:17 +02:00
keyCount++;
tr[key] = json[key]; // I'm doing this wrong, I know
}
2020-09-02 11:37:34 +02:00
if(keyCount == 0){
return undefined;
}
2020-09-12 23:15:17 +02:00
const transl = new Translation(tr);
return transl;
}
public static SimpleTag(json: string): Tag {
const tag = Utils.SplitFirst(json, "=");
return new Tag(tag[0], tag[1]);
}
2020-09-09 18:42:13 +02:00
public static Tag(json: AndOrTagConfigJson | string, context: string = ""): TagsFilter {
2020-09-02 11:37:34 +02:00
if(json === undefined){
2020-10-27 01:01:34 +01:00
throw `Error while parsing a tag: 'json' is undefined in ${context}. Make sure all the tags are defined and at least one tag is present in a complex expression`
2020-09-02 11:37:34 +02:00
}
if (typeof (json) == "string") {
const tag = json as string;
if (tag.indexOf("!~") >= 0) {
const split = Utils.SplitFirst(tag, "!~");
if (split[1] === "*") {
throw `Don't use 'key!~*' - use 'key=' instead (empty string as value (in the tag ${tag} while parsing ${context})`
}
return new RegexTag(
split[0],
new RegExp("^" + split[1] + "$"),
true
);
}
if (tag.indexOf("~~") >= 0) {
const split = Utils.SplitFirst(tag, "~~");
if (split[1] === "*") {
split[1] = "..*"
}
return new RegexTag(
new RegExp("^" + split[0] + "$"),
new RegExp("^" + split[1] + "$")
);
}
if (tag.indexOf("!=") >= 0) {
const split = Utils.SplitFirst(tag, "!=");
if (split[1] === "*") {
split[1] = "..*"
}
return new RegexTag(
split[0],
new RegExp("^" + split[1] + "$"),
true
);
}
if (tag.indexOf("~") >= 0) {
const split = Utils.SplitFirst(tag, "~");
if (split[1] === "*") {
split[1] = "..*"
}
return new RegexTag(
split[0],
new RegExp("^" + split[1] + "$")
);
}
const split = Utils.SplitFirst(tag, "=");
if(split[1] == "*"){
throw `Error while parsing tag '${tag}' in ${context}: detected a wildcard on a normal value. Use a regex pattern instead`
}
return new Tag(split[0], split[1])
}
if (json.and !== undefined) {
return new And(json.and.map(t => FromJSON.Tag(t, context)));
}
if (json.or !== undefined) {
return new Or(json.or.map(t => FromJSON.Tag(t, context)));
}
}
}