diff --git a/aqua/examples/options/option_gen.aqua b/aqua/examples/options/option_gen.aqua new file mode 100644 index 0000000..394e96c --- /dev/null +++ b/aqua/examples/options/option_gen.aqua @@ -0,0 +1,21 @@ +service OptionString("opt_str"): + checkOption(str: ?string) -> string + +func emptyString() -> ?string: + valueEmpty: ?string + <- valueEmpty + +func checkEmpty() -> string: + empty <- emptyString() + res <- OptionString.checkOption(empty) + <- res + +func stringAsOption(str: string) -> ?string: + valueEmpty: ?string + valueEmpty <<- str + <- valueEmpty + +func checkNoneEmpty(str: string) -> string: + nonEmpty <- stringAsOption(str) + res <- OptionString.checkOption(nonEmpty) + <- res diff --git a/src/compiled/examples/options/option_gen.ts b/src/compiled/examples/options/option_gen.ts new file mode 100644 index 0000000..93e3e70 --- /dev/null +++ b/src/compiled/examples/options/option_gen.ts @@ -0,0 +1,362 @@ +/** + * + * This file is auto-generated. Do not edit manually: changes may be erased. + * Generated by Aqua compiler: https://github.com/fluencelabs/aqua/. + * If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues + * Aqua version: 0.3.0-222 + * + */ +import { FluencePeer } from '@fluencelabs/fluence'; +import { + ResultCodes, + RequestFlow, + RequestFlowBuilder, + CallParams, +} from '@fluencelabs/fluence/dist/internal/compilerSupport/v1'; + + +// Services + + export interface OptionStringDef { + checkOption: (str: string | null, callParams: CallParams<'str'>) => string; + } + + export function registerOptionString(service: OptionStringDef): void; +export function registerOptionString(serviceId: string, service: OptionStringDef): void; +export function registerOptionString(peer: FluencePeer, service: OptionStringDef): void; +export function registerOptionString(peer: FluencePeer, serviceId: string, service: OptionStringDef): void; + export function registerOptionString(...args) { + let peer: FluencePeer; + let serviceId; + let service; + if (args[0] instanceof FluencePeer) { + peer = args[0]; + } else { + peer = FluencePeer.default; + } + + if (typeof args[0] === 'string') { + serviceId = args[0]; + } else if (typeof args[1] === 'string') { + serviceId = args[1]; + } + else { + serviceId = "opt_str" +} + + if (!(args[0] instanceof FluencePeer) && typeof args[0] === 'object') { + service = args[0]; + } else if (typeof args[1] === 'object') { + service = args[1]; + } else { + service = args[2]; + } + + peer.internals.callServiceHandler.use((req, resp, next) => { + if (req.serviceId !== serviceId) { + next(); + return; + } + + + if (req.fnName === 'checkOption') { + + const callParams = { + ...req.particleContext, + tetraplets: { + str: req.tetraplets[0] + }, + }; + resp.retCode = ResultCodes.success; + resp.result = service.checkOption(req.args[0], callParams) + + } + + + next(); + }); + } + + +// Functions + + export function stringAsOption(str: string, config?: {ttl?: number}) : Promise; + export function stringAsOption(peer: FluencePeer, str: string, config?: {ttl?: number}) : Promise; + export function stringAsOption(...args) { + let peer: FluencePeer; + let str; + let config; + if (args[0] instanceof FluencePeer) { + peer = args[0]; + str = args[1]; +config = args[2]; + } else { + peer = FluencePeer.default; + str = args[0]; +config = args[1]; + } + + let request: RequestFlow; + const promise = new Promise((resolve, reject) => { + const r = new RequestFlowBuilder() + .disableInjections() + .withRawScript( + ` + (xor + (seq + (seq + (seq + (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-) + (call %init_peer_id% ("getDataSrv" "str") [] str) + ) + (ap str $valueEmpty) + ) + (xor + (call %init_peer_id% ("callbackSrv" "response") [$valueEmpty]) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1]) + ) + ) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2]) +) + + `, + ) + .configHandler((h) => { + h.on('getDataSrv', '-relay-', () => { + return peer.connectionInfo.connectedRelay ; + }); + h.on('getDataSrv', 'str', () => {return str;}); + h.onEvent('callbackSrv', 'response', (args) => { + let [opt] = args; + if (Array.isArray(opt)) { + if (opt.length === 0) { resolve(null); } + opt = opt[0]; + } + return resolve(opt); +}); + + h.onEvent('errorHandlingSrv', 'error', (args) => { + const [err] = args; + reject(err); + }); + }) + .handleScriptError(reject) + .handleTimeout(() => { + reject('Request timed out for stringAsOption'); + }) + if(config && config.ttl) { + r.withTTL(config.ttl) + } + request = r.build(); + }); + peer.internals.initiateFlow(request!); + return promise; +} + + + + export function emptyString(config?: {ttl?: number}) : Promise; + export function emptyString(peer: FluencePeer, config?: {ttl?: number}) : Promise; + export function emptyString(...args) { + let peer: FluencePeer; + + let config; + if (args[0] instanceof FluencePeer) { + peer = args[0]; + config = args[1]; + } else { + peer = FluencePeer.default; + config = args[0]; + } + + let request: RequestFlow; + const promise = new Promise((resolve, reject) => { + const r = new RequestFlowBuilder() + .disableInjections() + .withRawScript( + ` + (xor + (seq + (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-) + (xor + (call %init_peer_id% ("callbackSrv" "response") [$valueEmpty]) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1]) + ) + ) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2]) +) + + `, + ) + .configHandler((h) => { + h.on('getDataSrv', '-relay-', () => { + return peer.connectionInfo.connectedRelay ; + }); + + h.onEvent('callbackSrv', 'response', (args) => { + let [opt] = args; + if (Array.isArray(opt)) { + if (opt.length === 0) { resolve(null); } + opt = opt[0]; + } + return resolve(opt); +}); + + h.onEvent('errorHandlingSrv', 'error', (args) => { + const [err] = args; + reject(err); + }); + }) + .handleScriptError(reject) + .handleTimeout(() => { + reject('Request timed out for emptyString'); + }) + if(config && config.ttl) { + r.withTTL(config.ttl) + } + request = r.build(); + }); + peer.internals.initiateFlow(request!); + return promise; +} + + + + export function checkEmpty(config?: {ttl?: number}) : Promise; + export function checkEmpty(peer: FluencePeer, config?: {ttl?: number}) : Promise; + export function checkEmpty(...args) { + let peer: FluencePeer; + + let config; + if (args[0] instanceof FluencePeer) { + peer = args[0]; + config = args[1]; + } else { + peer = FluencePeer.default; + config = args[0]; + } + + let request: RequestFlow; + const promise = new Promise((resolve, reject) => { + const r = new RequestFlowBuilder() + .disableInjections() + .withRawScript( + ` + (xor + (seq + (seq + (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-) + (call %init_peer_id% ("opt_str" "checkOption") [$valueEmpty] res) + ) + (xor + (call %init_peer_id% ("callbackSrv" "response") [res]) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1]) + ) + ) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2]) +) + + `, + ) + .configHandler((h) => { + h.on('getDataSrv', '-relay-', () => { + return peer.connectionInfo.connectedRelay ; + }); + + h.onEvent('callbackSrv', 'response', (args) => { + const [res] = args; + resolve(res); +}); + + h.onEvent('errorHandlingSrv', 'error', (args) => { + const [err] = args; + reject(err); + }); + }) + .handleScriptError(reject) + .handleTimeout(() => { + reject('Request timed out for checkEmpty'); + }) + if(config && config.ttl) { + r.withTTL(config.ttl) + } + request = r.build(); + }); + peer.internals.initiateFlow(request!); + return promise; +} + + + + export function checkNoneEmpty(str: string, config?: {ttl?: number}) : Promise; + export function checkNoneEmpty(peer: FluencePeer, str: string, config?: {ttl?: number}) : Promise; + export function checkNoneEmpty(...args) { + let peer: FluencePeer; + let str; + let config; + if (args[0] instanceof FluencePeer) { + peer = args[0]; + str = args[1]; +config = args[2]; + } else { + peer = FluencePeer.default; + str = args[0]; +config = args[1]; + } + + let request: RequestFlow; + const promise = new Promise((resolve, reject) => { + const r = new RequestFlowBuilder() + .disableInjections() + .withRawScript( + ` + (xor + (seq + (seq + (seq + (seq + (call %init_peer_id% ("getDataSrv" "-relay-") [] -relay-) + (call %init_peer_id% ("getDataSrv" "str") [] str) + ) + (ap str $valueEmpty) + ) + (call %init_peer_id% ("opt_str" "checkOption") [$valueEmpty] res) + ) + (xor + (call %init_peer_id% ("callbackSrv" "response") [res]) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 1]) + ) + ) + (call %init_peer_id% ("errorHandlingSrv" "error") [%last_error% 2]) +) + + `, + ) + .configHandler((h) => { + h.on('getDataSrv', '-relay-', () => { + return peer.connectionInfo.connectedRelay ; + }); + h.on('getDataSrv', 'str', () => {return str;}); + h.onEvent('callbackSrv', 'response', (args) => { + const [res] = args; + resolve(res); +}); + + h.onEvent('errorHandlingSrv', 'error', (args) => { + const [err] = args; + reject(err); + }); + }) + .handleScriptError(reject) + .handleTimeout(() => { + reject('Request timed out for checkNoneEmpty'); + }) + if(config && config.ttl) { + r.withTTL(config.ttl) + } + request = r.build(); + }); + peer.internals.initiateFlow(request!); + return promise; +} + diff --git a/src/examples/optionsCall.ts b/src/examples/optionsCall.ts new file mode 100644 index 0000000..63e2000 --- /dev/null +++ b/src/examples/optionsCall.ts @@ -0,0 +1,17 @@ +import {checkEmpty, checkNoneEmpty, registerOptionString} from "../compiled/examples/options/option_gen"; + +export async function genOptions(): Promise<[string, string]> { + registerOptionString({ + checkOption: (str: string | null) => { + if (str) { + return "some" + } else { + return "none" + } + } + }) + + const a = await checkEmpty(); + const b = await checkNoneEmpty("some_string"); + return [a, b] +} \ No newline at end of file diff --git a/src/run-examples.ts b/src/run-examples.ts index 391c48c..0c3629f 100644 --- a/src/run-examples.ts +++ b/src/run-examples.ts @@ -30,6 +30,7 @@ import { pushToStreamCall } from './examples/pushToStreamCall'; import { literalCall } from './examples/returnLiteralCall'; import { multiReturnCall } from './examples/multiReturnCall'; import { declareCall } from './examples/declareCall'; +import {genOptions} from "./examples/optionsCall"; let deepEqual = require('deep-equal'); function checkCall(name: string, actual: any, expected: any, callBackOnError: () => void) { @@ -217,6 +218,10 @@ const main = async () => { cb, ); + // option_gen.aqua + let optionGenResult = await genOptions(); + checkCall('optionGenResult', optionGenResult, ['none', 'some'], cb); + await FluencePeer.default.uninit(); await peer2.uninit();