Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model builtin #194

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions builtin/model.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
init(data: map[string]any){}

/**
* Validate model
* @return void
*/
function validate() throws: void;

/**
* Model transforms to map[string]any
* @return map[string]any
*/
function toMap(): map[string]any;

/**
* deep copy the Model without stream type
* @return $type
*/
function copyWithouStream(): $type;
2 changes: 2 additions & 0 deletions lib/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ builtin.set('$Env', _module('env'));
// TODO
//builtin.set('Crypto', _module('crypto'));

builtin.set('$ModelInstance', _module('model'));

builtin.set('$Stream', _module('stream'));

builtin.set('$Date', _module('date'));
Expand Down
11 changes: 8 additions & 3 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2237,9 +2237,13 @@ class TypeChecker {
} else if(type.type === 'basic' && isBasicType(type.name)) {
moduleName = this.getBuiltinModule(type.name);
ast.builtinModule = moduleName;
}
} else if(type.type === 'model') {
moduleName = '$ModelInstance';
ast.builtinModule = moduleName;
}

}

const checker = this.getChecker(moduleName);
const method = ast.left.propertyPath[0];
const name = method.lexeme;
Expand Down Expand Up @@ -2686,7 +2690,7 @@ class TypeChecker {
getInstanceType(id, env) {
const type = this.getVariableType(id, env);

if(!isBasicType(type.type)) {
if(!isBasicType(type.type) && type.type !== 'model') {
this.error('$type can only be used as basic type instacne call.', type);
}
if(type.type === 'array') {
Expand All @@ -2700,6 +2704,7 @@ class TypeChecker {
if(type.type === 'entry') {
return type.valueType;
}

return type;
}

Expand Down
26 changes: 26 additions & 0 deletions test/fixtures/builtin_module/model.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
model M {
name: string,
age: number,
}

static async function main(args: [string]): void {
var m = new M{
name = 'test',
age = 123
};
m.validate();
var mmap = m.toMap();
var name = $string(mmap['name']);
if(name == 'test') {
$Logger.info(name);
}


var m2: M = m.copyWithouStream();
var mmap2 = m2.toMap();
var name2 = $string(mmap2['name']);

if(name2 == 'test') {
$Logger.info('copyWithouStream' + name2);
}
}
6 changes: 6 additions & 0 deletions test/semantic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7222,6 +7222,12 @@ describe('semantic', function () {
}).to.not.throwException();
});

it('use model builtin module shoule be ok', function(){
expect(function () {
readAndParse('fixtures/builtin_module/model.dara');
}).to.not.throwException();
});

it('use builtin functions shoule be ok', function(){
expect(function () {
readAndParse('fixtures/builtin_function/function.dara');
Expand Down
Loading