Skip to content

Instantly share code, notes, and snippets.

@rstiller
Created September 7, 2012 11:11
Show Gist options
  • Save rstiller/3665199 to your computer and use it in GitHub Desktop.
Save rstiller/3665199 to your computer and use it in GitHub Desktop.
ModelToREST
{
'User': {
'meta': { // the basic information for source code generation
'package': 'mtr.model',
'extends': 'BaseObject', // an entity can extend every non-final type
},
'endpoints': {
'path': '/users/', /* exposes some endpoints relative to this path:
*
* - <path>/<ObjectKey>/link<ReferenceId>/<KeyOfReferencesEntity> - PUT (201)
* - <path>/<ObjectKey>/unlink<ReferenceId>/<KeyOfReferencesEntity> - DELETE (202)
* - <path>/<ObjectKey>/get<ReferenceId> - GET (200)
*
* - <path>/<ObjectKey> - GET (200)
* - <path>/<ObjectKey> - DELETE (202)
* - <path>/<ObjectKey> - PUT (200)
* - <path> - POST (201)
*/
'auth': { // the authorization information
'provider': 'mtr.auth.MyAuthorizationManager', // the fully qualified classname of the auth-manager
'methods': {
'GET': {'roles': ['*']}, // every one can access the user data
'PUT': {'roles': ['ADMIN', 'OWNER']}, // only admins and data-owners can access resources via PUT
'*': {'roles': []} // no access for all other methods
},
'paths': { // path-based rules relative to the base path
'/user1': {'roles': ['ADMIN']}, // user data of user1 can only be accessed by an admin
'/.*/linkFiends/.*': {'roles': ['ADMIN'], 'methods': ['PUT']} // friend relations can only be linked by admins and only PUT requests are allowed
}
}
},
'persistence': { // Entity specific persistence config (overrides default - inherits default)
'provider': 'infinispan', // provider id
'config': {} // additional parameters
},
'references': [ // logical links to other entities
'friends': { // id for a relationship
'referTo': 'User', // entity to refer to
'type': 'N-M', // type of relationship - many to many
'onDelete': 'CASCADE' // CASCADE | RESTRICT | KEEP - what to do when an instance of this entity was deleted
},
'boss': {
'referTo': 'User',
'type': 'N-1', // type of relationship - many to one
'onDelete': 'RESTRICT'
},
'employees': {
'referTo': 'User',
'type': '1-N', // type of relationship - one to many
'onDelete': 'KEEP'
},
'partners': {
'referTo': 'User',
'type': '1-1', // type of relationship - one to one
'onDelete': 'KEEP'
}
],
'fields': [ // the attributes of the entity
'id': {
'type': 'String',
'key': true // marks the 'id' field as identifier
},
'id2': {
'type': 'int', // primitive types are also allowed
'key': true // even splitkeys are allowed
},
'lastname': { // the name of the attribute
'type': 'String', // the (java) type of the attribute
'access': 'default', // the access modifier (default | public | protected | private)
'accessors': 'GET', // which accessor methods to implement (GET | SET | BOTH)
'default': '' // the default value
},
'firstname': {
'type': 'String',
'access': 'default',
'accessors': 'SET',
'default': '${mtr.constants.MyConstants.DEFAULT_FIRSTNAME}', // even constants are allowed (or simply expressions: '${ ((1 + 2) * (3 - 4)) / 5 % -(6) + mtr.constants.MyConstants.MAGIC_NUMBER }')
'validation': { // validation settings
'field': { // settings for the actual attribute itself
'size-max': 128, // the maximal length of the firstname
'size-min': 2, // the minimum length of the firstname
'pattern': '[\w\d]{2,128}', // the pattern of the firstname
'not-null': true, // have to got a value
'not-empty': true, // have to be not empty
'not-whitespace-only': true // // have to got a value other than just whitespaces
}
},
'views': ['default', 'myProtectedView'] // views / filters this attribute should be rendered in ('default' is reserved)
},
'nicknames': {
'type': 'List',
'valueType': 'String', // the type of the list-elements
'access': 'default',
'accessors': 'BOTH',
'validation': {
'field': {}, // validation rules for the list itself
'elements': {} // validation rules for the elements in this list
},
'views': ['default', 'myProtectedView', 'myFriendsView']
},
'languageSkills': {
'type': 'Map',
'keyType': 'java.util.Locale', // the type of the keys
'valueType': 'Integer', // the type of the values
'access': 'default',
'accessors': 'BOTH',
'validation': {
'field': {}, // validation rules for the map itself
'elements': { // validation rules for the values
'value-max': 10, // the maximum value of a skill
'value-min': 1 // the minimum value of a skill
},
'keys': {} // validation rules for the keys
},
'views': [] // will not be rendered, since it got no assigned views
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment