-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ public enum InputFormat { | |
TOML, | ||
PLAIN, | ||
BASE64, | ||
PYTHON, | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/gg/amy/utt/transform/impl/PythonTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package gg.amy.utt.transform.impl; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import gg.amy.utt.transform.TransformationContext; | ||
import gg.amy.utt.transform.Transformer; | ||
import org.python.util.PythonInterpreter; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* @author amy | ||
* @since 3/11/22. | ||
*/ | ||
public class PythonTransformer implements Transformer { | ||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
@Nonnull | ||
@Override | ||
public Object transformInput(@Nonnull final TransformationContext ctx, @Nonnull final String input) { | ||
final var interpreter = new PythonInterpreter(); | ||
interpreter.set("input", input); | ||
interpreter.exec("import ast, json; output = json.dumps(ast.literal_eval(input))"); | ||
final var output = interpreter.get("output"); | ||
try { | ||
return MAPPER.readValue(output.asString(), Object.class); | ||
} catch(@Nonnull final JsonProcessingException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String transformOutput(@Nonnull final TransformationContext ctx, @Nonnull final Object input) { | ||
throw new UnsupportedOperationException("Python objects are not a valid output format!"); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/test/java/gg/amy/utt/transform/impl/PythonTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package gg.amy.utt.transform.impl; | ||
|
||
import gg.amy.utt.data.InputFormat; | ||
import gg.amy.utt.data.OutputFormat; | ||
import gg.amy.utt.transform.TransformationContext; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* @author amy | ||
* @since 3/11/22. | ||
*/ | ||
public class PythonTransformerTest { | ||
@Test | ||
public void testPythonToJsonTransformation() { | ||
final var input = """ | ||
{'key': 'value', 'list': [1, 2, 3]} | ||
"""; | ||
final var ctx = new TransformationContext(InputFormat.PYTHON, OutputFormat.JSON, null, null, false, false); | ||
final var out = new JsonTransformer().transformOutput(ctx, new PythonTransformer().transformInput(ctx, input)); | ||
assertEquals("{\"list\":[1,2,3],\"key\":\"value\"}", out); | ||
} | ||
} |