https://github.com/fugerit-org/fj-script-helper
Fugerit Script Helper
https://github.com/fugerit-org/fj-script-helper
Last synced: 3 months ago
JSON representation
Fugerit Script Helper
- Host: GitHub
- URL: https://github.com/fugerit-org/fj-script-helper
- Owner: fugerit-org
- License: mit
- Created: 2024-10-23T09:47:08.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-10-24T20:44:38.000Z (9 months ago)
- Last Synced: 2025-02-15T23:43:23.474Z (5 months ago)
- Language: Java
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Fugerit Language Script Helper
This project provides just a convenient interface for evaluating scripts with java script engine.
[](CHANGELOG.md)
[](https://central.sonatype.com/artifact/org.fugerit.java/fj-script-helper)
[](https://javadoc.io/doc/org.fugerit.java/fj-script-helper)
[](https://opensource.org/license/mit)
[](https://github.com/fugerit-org/fj-universe/blob/main/CODE_OF_CONDUCT.md)
[](https://sonarcloud.io/summary/new_code?id=fugerit-org_fj-script-helper)
[](https://sonarcloud.io/summary/new_code?id=fugerit-org_fj-script-helper)[](https://universe.fugerit.org/src/docs/versions/java11.html)
[](https://universe.fugerit.org/src/docs/versions/java17.html)
[](https://universe.fugerit.org/src/docs/versions/maven3_9.html)
[](https://universe.fugerit.org/src/docs/conventions/index.html)## Quickstart
### 1. Add dependency
```xml
org.fugerit.java
fj-script-helper
${fj-script-helper-version}```
### 2. Usage
Sample code :
```java
// this will create a EvalScript instance for :
// kotlin script engine (kts)
// data model will be bound as "data" (if not provided this is the default binding name)
EvalScript evalKts = new EvalScriptWithDataModel( "kts", "data" );
try (Reader reader = [reader on kotlin script]) {
Map dataModel = new HashMap<>();
dataModel.put( "docTitle", "My custom title" );
Object result = evalKts.evalKts( reader, dataModel );
log.info( "my result : {}", result );
}
```It is possible to wrap the EvalScript with some decorators, for instance :
```java
EvalScript evalKts = EvalScriptWithJsonDataModel( new EvalScriptWithDataModel( "kts", "data" ) );
```Will wrap the EvalScript with EvalScriptWithJsonDataModel decorator.
EvalScriptWithJsonDataModel will transform a Map data model to a json data model style. Only getter and setter will be considered and made into basic types :* String
* Number
* Boolean
* Object as Map
* ArrayFor instance this code :
```java
class Vehicle {
private String plate;
private int age;
public Vehicle(int age, String plate) {
this.age = age;
this.plate = plate;
}
public String getPlate() { return plate; }
public int getAge() { return age; }
@Override
public String toString() {
return "Vehicle{age="+age+", plate='"+plate+"'}";
}
}
Map dataModel = new HashMap<>();
dataModel.put( "vehicle", new Vehicle( 10, "AA780BB" ) );
Map jsonStyleDataModel = EvalScriptWithJsonDataModel.defaultDataModelConversion( dataModel );
log.info( "originalDataModel : {}", dataModel );
log.info( "jsonStyleDataModel : {}", jsonStyleDataModel );
```would result in this conversion :
```text
originalDataModel : {vehicle=Vehicle{age=10, plate='AA780BB'}}
jsonStyleDataModel : {vehicle={plate=AA780BB, age=10}}
```