2023-01-20 18:52:57 +01:00
package io.gitlab.jfronny.muscript.data.dynamic ;
2022-06-07 12:19:58 +02:00
2022-06-29 15:41:44 +02:00
import io.gitlab.jfronny.commons.StringFormatter ;
2023-03-12 15:28:44 +01:00
import io.gitlab.jfronny.muscript.StandardLib ;
2023-03-12 16:44:57 +01:00
import io.gitlab.jfronny.muscript.ast.DynamicExpr ;
2023-03-12 15:28:44 +01:00
import io.gitlab.jfronny.muscript.compiler.ExprWriter ;
import io.gitlab.jfronny.muscript.compiler.Parser ;
2023-03-12 16:44:57 +01:00
import io.gitlab.jfronny.muscript.data.dynamic.additional.* ;
2022-06-07 12:19:58 +02:00
2023-03-12 15:28:44 +01:00
import java.io.IOException ;
/ * *
* Represents a value of an unknown type
* Override toString ( StringBuilder ) to support custom serialization ( note : the serialized form is ran with muScript to generate the tree )
* @param < T > the type represented
* /
public sealed interface Dynamic < T > permits DBool , DNumber , DString , DObject , DList , DCallable , DNull , DContainer {
2023-03-12 16:44:57 +01:00
/ * *
* Deserialize simply dynamic values . DOES NOT SUPPORT CALLABLES !
* Use Parser . parse ( ) if you truly need to deserialize them , but be aware that that might enable DOS attacks
* /
2023-03-12 15:28:44 +01:00
static Dynamic < ? > deserialize ( String source ) {
2023-03-12 16:44:57 +01:00
DynamicExpr expr = Parser . parse ( source ) . asDynamicExpr ( ) ;
if ( ! DirectPreconditionVisitor . visit ( expr ) ) throw new IllegalArgumentException ( " This expression does not directly express a dynamic and may not be loaded this way " ) ;
return expr . get ( StandardLib . createScope ( ) ) ;
2023-03-12 15:28:44 +01:00
}
static String serialize ( Dynamic < ? > dynamic ) {
StringBuilder sb = new StringBuilder ( ) ;
2023-03-12 19:22:45 +01:00
try ( ExprWriter ew = new ExprWriter ( sb , true ) ) {
2023-03-12 15:28:44 +01:00
dynamic . serialize ( ew ) ;
} catch ( IOException e ) {
throw new RuntimeException ( " Could not stringify " , e ) ;
}
return sb . toString ( ) ;
}
void serialize ( ExprWriter writer ) throws IOException ;
2022-06-07 12:19:58 +02:00
T getValue ( ) ;
default DBool asBool ( ) {
if ( this instanceof DBool bool ) return bool ;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException ( " bool " ) ;
2022-06-07 12:19:58 +02:00
}
default DNumber asNumber ( ) {
if ( this instanceof DNumber number ) return number ;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException ( " number " ) ;
2022-06-07 12:19:58 +02:00
}
default DString asString ( ) {
if ( this instanceof DString string ) return string ;
else return DFinal . of ( StringFormatter . toString ( getValue ( ) ) ) ;
}
default DObject asObject ( ) {
if ( this instanceof DObject object ) return object ;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException ( " object " ) ;
2022-06-07 12:19:58 +02:00
}
default DList asList ( ) {
if ( this instanceof DList list ) return list ;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException ( " list " ) ;
2022-06-07 12:19:58 +02:00
}
default DCallable asCallable ( ) {
if ( this instanceof DCallable callable ) return callable ;
2022-06-13 13:31:54 +02:00
else throw new DynamicTypeConversionException ( " callable " ) ;
2022-06-07 12:19:58 +02:00
}
}