APPCUBE-什么是脚本:样例代码解读

时间:2023-11-01 16:19:14

样例代码解读

您可以通过以下详细的脚本代码内容解读,对脚本有一个更具体的认识。

一般情况下,编写脚本的大致流程为:

  1. 按需引入平台标准库。
    图12 引入平台标准库
  2. 定义出参、入参结构。
    图13 定义入参
    图14 定义出参
  3. 定义方法以及使用的对象。
    图15 定义方法及使用对象
  4. 进行数据库操作。
    图16 数据库相关操作

以下将通过解读一个脚本样例,带您了解脚本的总体结构框架及编写要求。

样例脚本:

import * as decimal from 'decimal';@action.object({type: "param"})export class ActionInput {    @action.param({type: 'String', required: true, label: 'your name', description: 'please input your name'})    name: string;    @action.param({type: 'Number', required: true, min: 1, max: 100, message: 'age must during [1, 100]'})    age: decimal.Decimal;    @action.param({type: 'Date', pattern: 'yyyy-MM-dd'})    birthday: Date;    @action.param({type: 'String', isCollection: true})    schools: string[];    @action.param({type: 'Boolean'})    married: boolean;    @action.param({type: 'MyObject'})    obj: MyObject;}@action.object({type: "param"})export class MyObject {    @action.param({type: 'String'})    something: string;     @action.param({type: 'Number'})    otherthing: decimal.Decimal;}@action.object({type: "param"})export class ActionOutput {    @action.param({type: 'String', isCollection: true})    greets: string[];}@action.object({type: "method"})export class ActionDemo {    @action.method({ label: 'greeting something', description: 'greeting something.', input: 'ActionInput', output: 'ActionOutput' })    public greet(inarg: ActionInput): ActionOutput {        console.log('name = ', inarg.name);        console.log('age = ', inarg.age);        console.log('birthday = ', inarg.birthday);        console.log('schools = ', inarg.schools);        console.log('married = ', inarg.married);        console.log('obj = ', inarg.obj);        let out = new ActionOutput();        out.greets = ['hello', 'hi', 'how are you', 'how old are you', 'long time no see'];        return out;    }}

这个脚本分为3部分:

  • 导入标准库或者其他模块。
    上例中第1行表示使用平台提供的decimal库。
    import * as decimal from 'decimal';

    除了平台预置标准库,您还可以声明对其他自定义模块的引用。例如已经提前开发了一个脚本cirle,您可以用如下方式加载它。

    import * as circle from './circle';
  • 定义输入、输出变量。

    脚本可以有多个输入、输出参数,也可以没有。所有的输入或输出参数必须封装在一个class中,作为实例成员。

    本例中,脚本有6个输入参数,被封装为ActionInput。每个参数都必须定义其参数类型,同时还可以定义是否必填、标签、最大值、最小值等可选属性。

    @action.object({type: "param"})export class ActionInput {    @action.param({type: 'String', required: true, label: 'your name', description: 'please input your name'})    name: string;    @action.param({type: 'Number', required: true, min: 1, max: 100, message: 'age must during [1, 100]'})    age: decimal.Decimal;    @action.param({type: 'Date', pattern: 'yyyy-MM-dd'})    birthday: Date;    @action.param({type: 'String', isCollection: true})    schools: string[];    @action.param({type: 'Boolean'})    married: boolean;    @action.param({type: 'MyObject'})    obj: MyObject;}

    因为第6个输入参数“obj”的参数类型为自定义对象,所以还需要给出“MyObject”的定义。

    @action.object({type: "param"})export class MyObject {    @action.param({type: 'String'})    something: string;     @action.param({type: 'Number'})    otherthing: decimal.Decimal;}

    脚本有1个输出参数,被封装为ActionOutput。

    @action.object({type: "param"})export class ActionOutput {    @action.param({type: 'String', isCollection: true})    greets: string[];}
  • 定义方法。

    样例中,ActionDemo是外部调用的class,使用export导出。ActionDemo定义了一个action method,使用action.method装饰,表明调用脚本时从此方法入口。greet是class的实例方法,其输入、输出参数就是前面定义的ActionInput和ActionOutput。

    在一个脚本文件里面,action.method只能使用一次。

    @action.object({type: "method"})export class ActionDemo {    @action.method({ label: 'greeting something', description: 'greeting something.', input: 'ActionInput', output: 'ActionOutput' })    public greet(inarg: ActionInput): ActionOutput {        console.log('name = ', inarg.name);        console.log('age = ', inarg.age);        console.log('birthday = ', inarg.birthday);        console.log('schools = ', inarg.schools);        console.log('married = ', inarg.married);        console.log('obj = ', inarg.obj);        let out = new ActionOutput();        out.greets = ['hello', 'hi', 'how are you', 'how old are you', 'long time no see'];        return out;    }}

    脚本编辑页面不支持单步调试,样例里的console.log可实现在日志里打印过程输出,方便代码调试。

support.huaweicloud.com/usermanual-appcube/appcube_05_0046.html