aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarco Palladino <marco@mashape.com>2015-11-09 10:41:29 -0800
committerMarco Palladino <marco@mashape.com>2015-11-09 10:41:29 -0800
commitc451d2262211af9331de5997a24d8ec4bacbef1e (patch)
tree671174dcc5898f12e798af456749a7ae259a94c1
parentdc4c9f1d2b51a72cbd66ddeff1a2880d6b99d57c (diff)
parent03617b9a8cede011d5dbd7009bf7dab00ba67806 (diff)
downloadlua_uuid-c451d2262211af9331de5997a24d8ec4bacbef1e.tar.gz
lua_uuid-c451d2262211af9331de5997a24d8ec4bacbef1e.tar.bz2
lua_uuid-c451d2262211af9331de5997a24d8ec4bacbef1e.zip
Merge pull request #1 from Mashape/feat/direct-function
feat: made the module return a function
-rw-r--r--Makefile3
-rw-r--r--README.md12
-rw-r--r--lua_uuid.c28
-rw-r--r--test/lua_uuid_test.lua13
4 files changed, 23 insertions, 33 deletions
diff --git a/Makefile b/Makefile
index 0b872e0..cb124b3 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,6 @@
LUA ?= lua5.1
LUA_PC ?= lua5.1
+LUA_LIBS ?= $(shell pkg-config $(LUA_PC) --libs)
LUA_CFLAGS = $(shell pkg-config $(LUA_PC) --cflags)
CFLAGS ?= -O3 -Wall -Werror
@@ -10,7 +11,7 @@ all: lua_uuid.so
$(CC) -c $(CFLAGS) -fPIC $(LUA_CFLAGS) -o $@ $<
lua_uuid.so: lua_uuid.o test/lua_uuid_test.lua
- $(CC) -shared lua_uuid.o -o $@
+ $(CC) -shared lua_uuid.o $(LUA_LIBS) -o $@
$(LUA) test/lua_uuid_test.lua
clean:
diff --git a/README.md b/README.md
index cfa7c92..ee80f5d 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,14 @@
# lua_uuid
-Lua library that generate UUIDs leveraging libuuid.
+Lua library that generate UUIDs leveraging [libuuid](http://linux.die.net/man/3/libuuid).
## Usage
-To generate an unique UUID string:
+To generate a new UUID string:
```lua
-local lua_uuid = require "lua_uuid"
-local uuid_str = lua_uuid.generate()
+local uuid = require "lua_uuid"
+local uuid_str = uuid()
-print("The UUID is "..uuid_str)
-``` \ No newline at end of file
+print("New UUID: "..uuid_str)
+```
diff --git a/lua_uuid.c b/lua_uuid.c
index e0eca64..94d76b1 100644
--- a/lua_uuid.c
+++ b/lua_uuid.c
@@ -1,42 +1,28 @@
/***
-Utility for generating UUIDs
+Utility for generating UUIDs by wrapping libuuid's generate().
@license MIT
@module lua_uuid
*/
-#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>
#include <uuid/uuid.h>
-#if LUA_VERSION_NUM < 502
-# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
-# define lua_rawlen lua_objlen
-#endif
-
-/// Generate an UUID
+/// Generate a UUID
// @return uuid_str
// @function generate()
static int generate(lua_State *L) {
-
- // Generate UUID
uuid_t uuid;
- uuid_generate(uuid);
+ char uuid_str[37];
- // Unparse to a string
- char uuid_str[37]; // For example: "1b4e28ba-2fa1-11d2-883f-0016d3cca427" + "\0"
+ uuid_generate(uuid);
uuid_unparse_lower(uuid, uuid_str);
lua_pushstring(L, uuid_str);
return 1;
}
-static const luaL_Reg lua_uuid[] = {
- {"generate", generate},
- {NULL, NULL}
-};
-
-int luaopen_lua_uuid(lua_State *L){
- luaL_newlib(L, lua_uuid);
+int luaopen_lua_uuid(lua_State *L) {
+ lua_pushcfunction(L, generate);
return 1;
-} \ No newline at end of file
+}
diff --git a/test/lua_uuid_test.lua b/test/lua_uuid_test.lua
index 3cc44c2..19ebf67 100644
--- a/test/lua_uuid_test.lua
+++ b/test/lua_uuid_test.lua
@@ -1,8 +1,11 @@
-local lua_uuid = require "lua_uuid"
+package.path = package.path..";./?.lua"
-assert(lua_uuid.generate())
+local uuid = require "lua_uuid"
-local first = lua_uuid.generate()
-local second = lua_uuid.generate()
+assert(type(uuid) == "function")
-assert(first ~= second) \ No newline at end of file
+local first = uuid()
+local second = uuid()
+
+assert(first ~= second)
+assert(type(uuid()) == "string")