All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jio.console.JsConsole Maven / Gradle / Ivy

package jio.console;

import jio.IO;
import jio.Lambda;
import jio.RetryPolicies;
import jsonvalues.JsNothing;
import jsonvalues.JsPath;
import jsonvalues.JsValue;
import jsonvalues.spec.JsParserException;
import jsonvalues.spec.JsSpec;

import java.util.Objects;

import static jio.console.Functions.indent;

/**
 * Represents a lambda that takes a JsPath and returns a JIO effect that executes an interactive program to compose the
 * associated JsValue to that path.
 * 

* Use the static method {@link #of(JsSpec)} to create JsConsole programs from the spec the value introduced by the use * has to conform to. * * @param type of the JsValue returned * @see JsObjConsole * @see JsTupleConsole */ public interface JsConsole extends Lambda { /** * Factory method to create console programs that ask for the user to type in a json value that conforms to the given * spec * * @param spec the spec the value has to conform to * @return JsConsole program */ static JsConsole of(final JsSpec spec) { Objects.requireNonNull(spec); return path -> Programs.PRINT_LINE(String.format("%s%s -> ", indent(path), path ) ) .then(__ -> Programs.READ_LINE) .then(s -> { try { if (s.isEmpty()) { return IO.succeed(JsNothing.NOTHING); } return IO.succeed(spec.parse(s)); } catch (JsParserException e) { return IO.fail(e); } } ) .peekFailure(exc -> System.out.println(indent(path) + "Error: " + exc.getMessage())) .retry(RetryPolicies.limitRetries(3)); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy