Comments (3)
Actually, the generated code above is styled after that we generate for statically typed backends. We could take advantage of dynamically typed helper functions, and generate much less, something like:
//----------------------------------------------------------------------
// adl/primitives.js (hand written)
var String = {
validate : function(v) {return typeof(v) == "string";},
default : function() {return "";},
copy : function(v) {return v;},
equal : function(v1, v2) {return v1 == v2;}
};
var Int32 = {
validate : function(v) {return typeof(v) == "number";},
default : function() {return 0;},
copy : function(v) {return return v;},
equal : function(v1, v2) {return v1 == v2;}
};
function structValidate(fieldTypes) {
return function(v) {
for(var field in fieldTypes) {
if(!fieldTypes[field].validate(v[field])) {
return false;
}
}
return true;
};
}
function structDefault(fieldTypes) {
return function() {
var result = {};
for(var field in fieldTypes) {
result[field] = fieldTypes[field].default();
}
return result;
};
}
function structCopy(fieldTypes) {
return function(v) {
var result = {};
for(var field in fieldTypes) {
result[field] = fieldTypes[field].copy(v[field]);
}
return result;
};
}
function structEquals(fieldTypes) {
return function(v1,v2) {
for(var field in fieldTypes) {
if(!fieldTypes[field].equal(v1[field], v2[field])) {
return false;
}
}
return true;
};
}
//----------------------------------------------------------------------
// mymodel.js (machine generated)
var primitives = require('adl/primitives.js');
var User_fields = {
"name" : primitives.String,
"address" : primitives.String,
"age" : primitives.Int32
};
var User = {
validate : primitives.structValidate(User_fields),
default : primitives.structDefault(User_fields),
copy : primitives.structCopy(User_fields),
equals : primitives.structEquals(User_fields)
}
from adl.
Actually, generics will require functions, so probably best to use functions everywhere, ie this ADL:
module mymodel
{
struct User<T>
{
String name;
String address;
Int32 age;
T details;
};
};
would generate code like this:
//----------------------------------------------------------------------
// mymodel.js (machine generated)
var primitives = require('adl/primitives.js');
function User_fields(T) {
return {
"name" : primitives.String,
"address" : primitives.String,
"age" : primitives.Int32,
"details" : T
};
};
function User(T) {
var fieldTypes = User_fields(T);
return {
validate : primitives.structValidate(fieldTypes),
default : primitives.structDefault(fieldTypes),
copy : primitives.structCopy(fieldTypes),
equals : primitives.structEquals(fieldTypes)
};
}
and monomorphic types (ie those without type parameters) would be functions with no arguments.
from adl.
This has been done, although in a very different way. We generate a javascript translation of the ADL data model, which can be processed generically in javascript.
from adl.
Related Issues (20)
- Customizable code generation HOT 4
- Generated code is not UTF-8 encoded. HOT 1
- Incorrect typescript StringMap quoting HOT 1
- C++: Implement default values for custom type mappings HOT 1
- Generated more useful java constructors, and helper functions for default values. HOT 1
- Rename Void primitive to Unit HOT 3
- Make enums in typescript be type level strings HOT 2
- Remove Error<T> type from sys.types HOT 1
- Change sys.types.Map serialization to a list of map entries (not pairs) HOT 1
- TypeScript: newtype generates type alias and not proper newtype HOT 2
- Issues in java code generation HOT 1
- Full Example for type alias custom type HOT 14
- Robust serialisation of IEEE 754 NaN and Inf
- TypeScript: support "useUnknownInCatchVariables" compiler option HOT 5
- AdlVoid.java not included in generated runtime if Void not the source adl
- Support aeson 2.0
- Java gen $1 substitution in defaults HOT 1
- `@SerializedName` vs `annotation ... SerializedName` HOT 1
- Java custom types doesn't propagate generic arguments.
- ADL annotations shouldn't allow superfluous fields HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from adl.