mirror of
https://github.com/fluencelabs/aqua-playground
synced 2025-03-15 21:51:04 +00:00
options test
This commit is contained in:
parent
c9872cdc51
commit
6b094e14ed
21
aqua/examples/options/option_gen.aqua
Normal file
21
aqua/examples/options/option_gen.aqua
Normal file
@ -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
|
362
src/compiled/examples/options/option_gen.ts
Normal file
362
src/compiled/examples/options/option_gen.ts
Normal file
@ -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<string | null>;
|
||||||
|
export function stringAsOption(peer: FluencePeer, str: string, config?: {ttl?: number}) : Promise<string | null>;
|
||||||
|
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<string | null>((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<string | null>;
|
||||||
|
export function emptyString(peer: FluencePeer, config?: {ttl?: number}) : Promise<string | null>;
|
||||||
|
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<string | null>((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<string>;
|
||||||
|
export function checkEmpty(peer: FluencePeer, config?: {ttl?: number}) : Promise<string>;
|
||||||
|
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<string>((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<string>;
|
||||||
|
export function checkNoneEmpty(peer: FluencePeer, str: string, config?: {ttl?: number}) : Promise<string>;
|
||||||
|
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<string>((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;
|
||||||
|
}
|
||||||
|
|
17
src/examples/optionsCall.ts
Normal file
17
src/examples/optionsCall.ts
Normal file
@ -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]
|
||||||
|
}
|
@ -30,6 +30,7 @@ import { pushToStreamCall } from './examples/pushToStreamCall';
|
|||||||
import { literalCall } from './examples/returnLiteralCall';
|
import { literalCall } from './examples/returnLiteralCall';
|
||||||
import { multiReturnCall } from './examples/multiReturnCall';
|
import { multiReturnCall } from './examples/multiReturnCall';
|
||||||
import { declareCall } from './examples/declareCall';
|
import { declareCall } from './examples/declareCall';
|
||||||
|
import {genOptions} from "./examples/optionsCall";
|
||||||
let deepEqual = require('deep-equal');
|
let deepEqual = require('deep-equal');
|
||||||
|
|
||||||
function checkCall(name: string, actual: any, expected: any, callBackOnError: () => void) {
|
function checkCall(name: string, actual: any, expected: any, callBackOnError: () => void) {
|
||||||
@ -217,6 +218,10 @@ const main = async () => {
|
|||||||
cb,
|
cb,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// option_gen.aqua
|
||||||
|
let optionGenResult = await genOptions();
|
||||||
|
checkCall('optionGenResult', optionGenResult, ['none', 'some'], cb);
|
||||||
|
|
||||||
await FluencePeer.default.uninit();
|
await FluencePeer.default.uninit();
|
||||||
await peer2.uninit();
|
await peer2.uninit();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user