Skip to content

Commit

Permalink
add model builtin instance function
Browse files Browse the repository at this point in the history
  • Loading branch information
peze authored and JacksonTian committed May 13, 2024
1 parent 0d31dfe commit a72d925
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
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

0 comments on commit a72d925

Please sign in to comment.