2022-06-04 19:43:49 +02:00
package io.gitlab.jfronny.respackopts.gson ;
import io.gitlab.jfronny.gson.* ;
2023-02-23 18:15:37 +01:00
import io.gitlab.jfronny.muscript.StarScriptIngester ;
import io.gitlab.jfronny.muscript.ast.Expr ;
import io.gitlab.jfronny.muscript.compiler.Parser ;
2023-09-22 19:19:55 +02:00
import io.gitlab.jfronny.respackopts.muscript.ScopeVersion ;
2022-06-04 19:43:49 +02:00
import java.lang.reflect.Type ;
2023-02-23 18:15:37 +01:00
import java.util.HashMap ;
import java.util.Map ;
2022-06-04 19:43:49 +02:00
public class ExprDeserializer implements JsonDeserializer < Expr < ? > > {
private static final Map < String , Expr < ? > > compiledScripts = new HashMap < > ( ) ;
@Override
public Expr < ? > deserialize ( JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
if ( json . isJsonPrimitive ( ) & & json . getAsJsonPrimitive ( ) . isString ( ) ) {
String s = json . getAsJsonPrimitive ( ) . getAsString ( ) ;
if ( compiledScripts . containsKey ( s ) )
return compiledScripts . get ( s ) ;
try {
2023-09-22 19:19:55 +02:00
int v = AttachmentHolder . getAttachedVersion ( ) ;
Expr < ? > expr = Parser . parse ( ScopeVersion . by ( v ) . muScriptVersion , v < = 7
2022-06-04 19:43:49 +02:00
? StarScriptIngester . starScriptToMu ( s )
: s ) ;
compiledScripts . put ( s , expr ) ;
return expr ;
} catch ( Parser . ParseException e ) {
throw new JsonParseException ( " Could not create script " , e ) ;
}
}
else {
2023-10-05 18:18:04 +02:00
if ( json . isJsonObject ( ) ) throw new JsonParseException ( " Could not parse script: Expected string but got object (did you forget to migrate this rpo to muScript?) " ) ;
2022-06-04 19:43:49 +02:00
throw new JsonParseException ( " Could not parse script: Expected string " ) ;
}
}
}