Overview DSL Reference

Overview DSL Reference

 

High Level Constructs

 

unit

Units are groupings of functions and also variables.

They can be thought of as modules or namespaces. A Mezzanine script must have at least one unit.Unit must be followed with a unique unit name. Units are defined as follows.

 

  1. unit MyUnit;

  2. // Other code follows

  3.  

  4. string myVar;

  5.  

  6. int factorial(int x) {

  7.     if(== 0 || x == 1) {

  8.         return 1;

  9.     } else if (> 1) {

  10.         return factorial(- 1);

  11.     }

  12.  

  13.     return 0;

  14. }

Heads Up Objects, enums and validators are global, so they can optionally be in a separate source code file without a unit.

object

Indicates the start of an object declaration. Must be followed by a unique object name and attribute block

 

  1. object Person 

  2. {

  3.     string fname;

  4.     string sname;

  5.     int age;

  6. }

persistent

Used to indicate that an object is persistent, must precede the object definition

 

  1. persistent object Person

  2. {

  3.     string fname;

  4.     string sname;

  5.     int age;

  6. }

The _id attribute is also available from any persistent object. It is a uuid type.

enum

Used to declare an enumeration. Must be followed by an enumID (all caps and underscores)

 

 

  1. enum GENDER

  2. {

  3.     Male, Female

  4. }

 

validator

Used to declare a field validator

 

  1. validator FirstnameValidator 

  2. {

  3.     minlen(2); maxlen(250);

  4. }

  5. validator AgeValidator 

  6. {

  7.     minval(0);

  8. }

 

Flow control

return

Returns the value of the expression that follows from a function

 

  1. string getGreeting(string name)

  2. {

  3.     return Strings:concat("Hello, ", name);

  4. }

 

if / else

If statement

 

  1. if(name == "Tom")

  2. {

  3.     Mez:log("Hello Tom!");

  4. }

  5. else if (name == "Frank")

  6. {

  7.     Mez:log("Hello Frank!");

  8. }

  9. else

  10. {

  11.     Mez:log("Hi! Who are you?");

  12. }

 

for

For loop

 

  1. for(;;)

  2. {

  3.     // Technically not possible, as iterations

  4.     // have a limit.

  5.     Mez:log("Infinite loop!");

  6. }

  7.  

  8. for(int i = 1; i <= 10; i++)

  9. {

  10.     Mez:log(i);

  11. }

  12.  

  13. int i = 0;

  14. for(; i < 10;)

  15. {

  16.     Mez:log("While style loop!");

  17. }

 

Primitive types

Type

Description

Type

Description

int

Integers

string

Strings

decimal

Numeric type e.g. 3.14 or 123

bool

Boolean values e.g. true or false

date

Date

datetime

Date and time

uuid

UUID type 4 (36 characters)

void

Void type

blob

Binary values e.g. photos

Annotations

@Role

Indicates that the persistent object is associated with a user role (using this annotation on an object automatically makes it persistent, so you don’t have to explicitly use the persistent keyword)

 

  1. @Role("Manager")

  2. object ManagerProfile

  3. {

  4.     string fname;

  5.     string sname;

  6.     string title;

  7. }

 

 Heads UpAny persistent object that is annotated with @Role has these implicit attributes:

  • _firstNames

  • _nickName

  • _surname

@RoleName

Allows a custom role name to be displayed in the app that differs from role name specified in the data model and can be determined at runtime.

 

@RoleName string getGymEmployeeRoleName(GymEmployee employee){ EN_GYM_EMPLOYEE_ROLE_TYPE employeeRoleType = employee.employeeType; if(employeeRoleType == EN_GYM_EMPLOYEE_ROLE_TYPE.Gym_Trainer){ return "Trainer"; } else if(employeeRoleType == EN_GYM_EMPLOYEE_ROLE_TYPE.Gym_Admin){ return "Gym Admin"; } else{ return "Gym Employee"; } }

 

@Restrict

Use this for users of Helium Android to be able to access specific types. The selector defines which objects of the requested type are accessible to the specified role. Objects that have relationships to objects included by an @Restrict are not automatically synchronized, they must have their own @Restrict annotations in order for the relationship to remain valid across the network. This technique is especially useful with many-to-many relationships, which can be used to reduce the amount of data synchronized to Helium Android users, and improve the user-experience of the application.

"Manager" indicates for which roles(s) it applies and all() is the selector.

 

  1. @Restrict("Manager", all())

  2. object EmployeeRecord

  3. {

  4.     string fname;

  5.     string sname;

  6. }

 

Types without an @Restrict annotation will not be synchronized to Helium Android users, and may cause null-pointer errors in your application.

 

@RolesAllowed

Specifies the access rights for a specific role on an object (like the previous annotation, this annotation implicitly makes the object persistent)

 

  1. @RolesAllowed("Manager", "rw")

  2. object EmployeeRecord

  3. {

  4.     string fname;

  5.     string sname;

  6. }

 

@Scheduled

Specifies a scheduled task to run at a certain interval

 

  1. // Run at 2:15 AM every day

  2. @Scheduled("15 2 * * *")

  3. void foobar() { ... }

  4.  

  5. // Run at 06:00 every week-day  (days 1 to 5)

  6. @Scheduled("0 6 * * 1-5")

  7. void foobar() { ... }

  8.  

  9. // Run every ten minutes

  10. @Scheduled("*/10 * * * *")

  11. void foobar() { ... }

 

To summarize the schedule string format: 

"minute hour day-of-month month day-of-week"

 

@ReceiveSms

Indicates that a function can receive an SMS.

 

  1. @ReceiveSms("Test description")

  2. void receiveSmsNumberText(string number, string text) { ... }

  3.  

  4. @ReceiveSms("Test description")

  5. void receiveSmsObjectText(Nurse nurse, string text) { ... }

  6.  

  7. @ReceiveSms("Test description")

  8. void receiveSmsObjectNumberText(Nurse nurse, string number, string text) { ... }

 

@Test

Indicates that a function is a unit test.

 

  1. @Test

  2. void testIncrement(){

  3.     int i = 5;

  4.     i++;

  5.     Assert:isEqual(6, i, "Increment failed");

  6. }

 

@{ID} eg. @FirstnameValidator

Annotations that start with @ followed by an ID, and that are used on object attributes are validator annotations. The ID value must be the same as any validator that is defined in the application:

 

  1. object Person

  2. {

  3.     @FirstnameValidator("validr.msg.fname")

  4.     string fname;

  5.  

  6.     @SurnameValidator("validr.msg.sname")

  7.     string sname;

  8. }