About the annotation

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view

About the annotation

I start a new post since this is also an introduction to my solution of the
annotation system.

I don't think it's good to handle the annotation in the C side, since  a
generic annotation system should provide supports for many scenarios.

I used an attribute system to handle features like type validation for both
parameters and return values, iterator generator, data table and field
binding, web route, web form validation and etc, they can be easily done in
the Lua side, but can hardly be done in the C side.

As an example, if we can bind basic types for function parameter, and let
the C side to handle it, it's not enough to cover all conditions. The system
can't handle types like enumeration values, fixed-structure data values and
etc, or we need have a new ML to do the job, that's against the Lua's
design(also we can't repeat to declare the same type in each annotation, so
we also need to bring in a new type system).

The key of my solution is the Lua environment and global variable, through a
private and well-controlled environment, we know every definition of global
variables in it, we have the target name, type and where it's defined, so we
also can apply annotations to the target, like wrap the target function for
type validation, register an object as route handlers. We can create our own
type system and annotation system without any changes of the Lua.

Here is an example to show the usage of it:

I choose the __XXX__ format since it's easily to let the editor to highlight
it. They are objects that can be created with many declaration styles. When
I need change the JSON output to a html string, I only need replace the
__Json__ to __View__ "template_path" so I don't need to modify the main
logic in the function.

For other common usages:

The solution of mine is not a simple project, and it may be more heavy for
Lua to provide an annotation system in the C side, I really enjoy the Lua's
simple state for now, so I hope it'd keep it in the future, since we can
finish the job from the outside.

And If anyone has interesting in my project, you can find it at

Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html

Reply | Threaded
Open this post in threaded view

Re: About the annotation

I'm still not familiar with the format, the code part is missing, so I post
it again:

The first example:

 >   -- Declare a web applcaiton as the private environment
 >   Application "TestWebApp" (function(_ENV)                    
 >       __Route__("/user", HttpMethod.GET)   -- Bind the route GET
/user?name=ann to the function                          
 >       __Form__{ name = { type = String, require = true } }  -- Bind the
form validation, the result will be passed to the form & err
 >       __Json__()   -- The return value will be serialized as JSON to the
 >       function JsonHandler(context, form, err)
 >           if err then return {} end
 >           local user
 >           -- Like the Using in C#, auto close the database connection
 >           with(UserDataContext())(function(ctx)
 >               -- Get the serializable data entity
 >               user = ctx.Users:Query{ name = form.name }:First() or {}
 >           end, function(err)
 >               -- skip
 >           end)
 >           return user or {}
 >       end
 >   end)

The second example:

 >   UnitTest "UnitTest.Example" (function(_ENV)
 >       __Test__"Should always succeed"
 >       function test1() end
 >       __Test__"Should always fail"
 >       function test2() Assert.Nil(1) end
 >   end)
 >   Module "Type.Validation" (function(_ENV)
 >       __Return__{ String }
 >       __Arguments__{ String, Number/1 }
 >       function rep(str, count)
 >           return string.rep(str, count)
 >       end
 >       print(rep("h"))     -- h
 >       print(rep("h", "")) -- Usage: rep(System.String, [System.Number =
1]) - the 2nd argument must be number, got string
 >   end)

Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html