API Docs Help

Fonctions

Les fonctions dans SquidCore sont basés sur la classe Function de java standard
Comme tout système config dans SquidCore il est très connecté avec le système de Placeholder

Exemple simple en config

Pour cette exemple, nous allons utiliser la fonction "add" qui ajoute deux nombre

type: function function: "add" input: 1 number: 3

Cette fonction retournerais donc 4, simple, pas vrai?

Les différents types de fonction disponible

Toute les classes de fonction dépende de BaseFunction
Il existe aussi ComplexFunction, qui permet de faire une fonction en se basant sur une PlatformConfigurationSection

Voici un exemple du rendu en code d'une fonction Add qui supporterais que les nombres entiers

public class AddFunction implements ComplexFunction { @Override public Map<String, Object> extractParam(PlatformConfigurationSection section) throws FunctionParseException { Object input = section.get("input"); if (input == null) throw new FunctionParseException("Input is required"); Object number = section.get("number"); if (number == null) throw new FunctionParseException("Number is required"); PlaceholderWrapper<Integer> inputWrapper; try { inputWrapper = PlaceholderWrappers.wrap(input, Integer.class); } catch (Exception e) { throw new FunctionParseException("Failed to wrap input", e); } PlaceholderWrapper<Integer> numberWrapper; try { numberWrapper = PlaceholderWrappers.wrap(number, Integer.class); } catch (Exception e) { throw new FunctionParseException("Failed to wrap number", e); } return Map.of( "input", inputWrapper, "number", numberWrapper ); } @Override public Object run(Function<String, Object> parser, Map<String, Object> param) throws FunctionComputeException { //noinspection unchecked PlaceholderWrapper<Integer> input = (PlaceholderWrapper<Integer>) param.get("input"); //noinspection unchecked PlaceholderWrapper<Integer> number = (PlaceholderWrapper<Integer>) param.get("number"); int inputInt; int numberInt; try { inputInt = input.get(parser); } catch (Exception e) { throw new FunctionComputeException("Failed to get input", e); } try { numberInt = number.get(parser); } catch (Exception e) { throw new FunctionComputeException("Failed to get number", e); } return inputInt + numberInt; } }

Cela parait terrifiant à première vue, mais c'est plutôt simple une fois décortiquer
Voici les étapes prise pour parse:

  1. D'abord on vérifie si la configuration est valide à ce dont on s'attend

    Object input = section.get("input"); if (input == null) throw new FunctionParseException("Input is required"); Object number = section.get("number"); if (number == null) throw new FunctionParseException("Number is required");
  2. Ensuite input et number sont parse via PlaceholderWrappers, et rapporte l'exception si le wrap est impossible

    PlaceholderWrapper<Integer> inputWrapper; try { inputWrapper = PlaceholderWrappers.wrap(input, Integer.class); } catch (Exception e) { throw new FunctionParseException("Failed to wrap input", e); } PlaceholderWrapper<Integer> numberWrapper; try { numberWrapper = PlaceholderWrappers.wrap(number, Integer.class); } catch (Exception e) { throw new FunctionParseException("Failed to wrap number", e); }
  3. Les deux valeurs sont stockés dans une map

    return Map.of( "input", inputWrapper, "number", numberWrapper );

Voilà, nous avons désormais parse nos paramètres, place à l'exécution!

L'exécution:

  1. Les deux valeurs sont récupérés depuis la map (//noinspection unchecked pour éviter un warning intellij)

    //noinspection unchecked PlaceholderWrapper<Integer> input = (PlaceholderWrapper<Integer>) param.get("input"); //noinspection unchecked PlaceholderWrapper<Integer> number = (PlaceholderWrapper<Integer>) param.get("number");
  2. Ils sont ensuite parse avec la PlaceholderMap donné par l'appeleur de la fonction

    int inputInt; int numberInt; try { inputInt = input.get(parser); } catch (Exception e) { throw new FunctionComputeException("Failed to get input", e); } try { numberInt = number.get(parser); } catch (Exception e) { throw new FunctionComputeException("Failed to get number", e); }
  3. Les deux nombres sont maintenant additioné et le résultat est renvoyé

    return inputInt + numberInt;

Inscrire sa fonction dans le registre globale

Pour inscrire votre fonction dans le registre, vous devez simplement l'enregistrer avec un nom et l'instance

FunctionRegistry.getInstance().register("add", new AddFunction());

Supporter les fonctions dans une configuration

Les fonctions sont par défaut pris en charge si vous utilisez PlaceholderWrappers pour englober une valeur en config

Last modified: 17 septembre 2024