2020-08-30 01:13:18 +02:00
import { AndOrTagConfigJson } from "./TagConfigJson" ;
2020-08-31 02:59:47 +02:00
import { Utils } from "../../Utils" ;
2021-03-29 00:41:53 +02:00
import { RegexTag } from "../../Logic/Tags/RegexTag" ;
import { Or } from "../../Logic/Tags/Or" ;
import { And } from "../../Logic/Tags/And" ;
import { Tag } from "../../Logic/Tags/Tag" ;
import { TagsFilter } from "../../Logic/Tags/TagsFilter" ;
2021-03-29 01:20:06 +02:00
import SubstitutingTag from "../../Logic/Tags/SubstitutingTag" ;
2020-08-30 01:13:18 +02:00
export class FromJSON {
public static SimpleTag ( json : string ) : Tag {
2020-08-31 02:59:47 +02:00
const tag = Utils . SplitFirst ( json , "=" ) ;
2020-08-30 01:13:18 +02:00
return new Tag ( tag [ 0 ] , tag [ 1 ] ) ;
}
2021-03-29 01:20:06 +02:00
2020-09-09 18:42:13 +02:00
public static Tag ( json : AndOrTagConfigJson | string , context : string = "" ) : TagsFilter {
2021-03-29 01:20:06 +02:00
try {
2021-01-09 02:11:43 +01:00
return this . TagUnsafe ( json , context ) ;
2021-03-29 01:20:06 +02:00
} catch ( e ) {
console . error ( "Could not parse tag" , json , "in context" , context , "due to " , e )
2021-01-09 02:11:43 +01:00
throw e ;
}
}
private static TagUnsafe ( json : AndOrTagConfigJson | string , context : string = "" ) : TagsFilter {
2021-03-29 01:20:06 +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
}
2020-08-30 01:13:18 +02:00
if ( typeof ( json ) == "string" ) {
const tag = json as string ;
if ( tag . indexOf ( "!~" ) >= 0 ) {
2020-08-31 02:59:47 +02:00
const split = Utils . SplitFirst ( tag , "!~" ) ;
if ( split [ 1 ] === "*" ) {
2020-09-08 00:33:05 +02:00
throw ` Don't use 'key!~*' - use 'key=' instead (empty string as value (in the tag ${ tag } while parsing ${ context } ) `
2020-08-30 01:13:18 +02:00
}
return new RegexTag (
2020-08-31 02:59:47 +02:00
split [ 0 ] ,
new RegExp ( "^" + split [ 1 ] + "$" ) ,
2020-08-30 01:13:18 +02:00
true
) ;
}
2020-09-08 00:33:05 +02:00
if ( tag . indexOf ( "~~" ) >= 0 ) {
const split = Utils . SplitFirst ( tag , "~~" ) ;
if ( split [ 1 ] === "*" ) {
split [ 1 ] = "..*"
}
return new RegexTag (
2021-01-04 04:06:21 +01:00
new RegExp ( "^" + split [ 0 ] + "$" ) ,
2020-09-08 00:33:05 +02:00
new RegExp ( "^" + split [ 1 ] + "$" )
) ;
}
2021-03-29 01:20:06 +02:00
if ( tag . indexOf ( ":=" ) >= 0 ) {
const split = Utils . SplitFirst ( tag , ":=" ) ;
return new SubstitutingTag ( split [ 0 ] , split [ 1 ] ) ;
}
2020-08-30 01:13:18 +02:00
if ( tag . indexOf ( "!=" ) >= 0 ) {
2020-08-31 02:59:47 +02:00
const split = Utils . SplitFirst ( tag , "!=" ) ;
if ( split [ 1 ] === "*" ) {
2020-09-03 02:05:09 +02:00
split [ 1 ] = "..*"
2020-08-31 02:59:47 +02:00
}
2020-08-30 01:13:18 +02:00
return new RegexTag (
2020-08-31 02:59:47 +02:00
split [ 0 ] ,
new RegExp ( "^" + split [ 1 ] + "$" ) ,
2020-08-30 01:13:18 +02:00
true
) ;
}
2021-06-21 03:08:33 +02:00
if ( tag . indexOf ( "!~" ) >= 0 ) {
const split = Utils . SplitFirst ( tag , "!~" ) ;
if ( split [ 1 ] === "*" ) {
split [ 1 ] = "..*"
}
return new RegexTag (
split [ 0 ] ,
new RegExp ( "^" + split [ 1 ] + "$" ) ,
true
) ;
}
2020-08-30 01:13:18 +02:00
if ( tag . indexOf ( "~" ) >= 0 ) {
2020-08-31 02:59:47 +02:00
const split = Utils . SplitFirst ( tag , "~" ) ;
if ( split [ 1 ] === "*" ) {
2020-09-03 02:05:09 +02:00
split [ 1 ] = "..*"
2020-08-30 01:13:18 +02:00
}
return new RegexTag (
2020-08-31 02:59:47 +02:00
split [ 0 ] ,
new RegExp ( "^" + split [ 1 ] + "$" )
2020-08-30 01:13:18 +02:00
) ;
}
2021-01-04 04:06:21 +01:00
if ( tag . indexOf ( "=" ) >= 0 ) {
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 ] )
2020-09-08 00:33:05 +02:00
}
2021-01-04 04:06:21 +01:00
throw ` Error while parsing tag ' ${ tag } ' in ${ context } : no key part and value part were found `
2020-08-30 01:13:18 +02:00
}
if ( json . and !== undefined ) {
2020-09-08 00:33:05 +02:00
return new And ( json . and . map ( t = > FromJSON . Tag ( t , context ) ) ) ;
2020-08-30 01:13:18 +02:00
}
if ( json . or !== undefined ) {
2020-09-08 00:33:05 +02:00
return new Or ( json . or . map ( t = > FromJSON . Tag ( t , context ) ) ) ;
2020-08-30 01:13:18 +02:00
}
}
}