jdesgats / ljsonschema

Pure Lua JSON schema validator
MIT License
46 stars 21 forks source link

Version between github and luarocks are different #5

Open QimatLuo opened 6 years ago

QimatLuo commented 6 years ago

I installed this library and run the example code in readme, but get faild.

lua: /usr/local/share/lua/5.1/jsonschema/init.lua:161: bad argument #1 to 'load' (function expected, got string)                                                                                                  
stack traceback:
        [C]: in function 'load'
        /usr/local/share/lua/5.1/jsonschema/init.lua:161: in function </usr/local/share/lua/5.1/jsonschema/init.lua:160>                                                                                          
        (tail call): ?
        a.lua:5: in main chunk
        [C]: ?

I also find some differences between the actual code I installed and github. For example, local coro_yield = coroutine.yield is not on luarocks version. But it's in github source code.

jdesgats commented 6 years ago

Thanks for the report. What happened is that I never considered this library is finished, or stable. Therefore I never bother publishing it to Luarocks, @3scale did before I had the opportunity to do it. So I don't control that package and it turns out they made changes to it:

diff -u ljsonschema/jsonschema/init.lua luarocks/ljsonschema/jsonschema/init.lua
--- ljsonschema/jsonschema/init.lua 2018-07-20 10:49:01.563281856 +0100
+++ luarocks/ljsonschema/jsonschema/init.lua    1979-12-31 00:00:00.000000000 +0000
@@ -6,8 +6,7 @@
 local sformat = string.format
 local mmax, mmodf = math.max, math.modf
 local tconcat = table.concat
-local coro_wrap = coroutine.wrap
-local coro_yield = coroutine.yield
+local insert = table.insert
 local DEBUG = os and os.getenv and os.getenv('DEBUG') == '1'

 -- default null token
@@ -107,53 +106,46 @@
   self._body[#self._body+1] = '\n'
 end

--- load doesn't like at all empty string, but sometimes it is easier to add
--- some in the chunk buffer
-local function yield_chunk(chunk)
-  if chunk and chunk ~= '' then
-    coro_yield(chunk)
-  end
-end
-
 function codectx_mt:_generate()
+  local res = {}
   local indent = ''
   if self._root == self then
     for _, stmt in ipairs(self._preface) do
-      yield_chunk(indent)
+      insert(res, indent)
       if getmetatable(stmt) == codectx_mt then
-        stmt:_generate()
+        insert(res, stmt:_generate())
       else
-        yield_chunk(stmt)
+        insert(res, stmt)
       end
     end
   else
-    coro_yield('function(')
+    insert(res, 'function(')
     for i=1, self._nparams do
-      yield_chunk('p_' .. i)
-      if i ~= self._nparams then yield_chunk(', ') end
+      insert(res, 'p_' .. i)
+      if i ~= self._nparams then insert(res, ', ') end
     end
-    yield_chunk(')\n')
+    insert(res, ')\n')
     indent = string.rep('  ', self._idx)
   end

   for _, stmt in ipairs(self._body) do
-    yield_chunk(indent)
+    insert(res, indent)
     if getmetatable(stmt) == codectx_mt then
-      stmt:_generate()
+      insert(res, stmt:_generate())
     else
-      yield_chunk(stmt)
+      insert(res, stmt)
     end
   end

   if self._root ~= self then
-    yield_chunk('end')
+    insert(res, 'end')
   end
+
+  return tconcat(res)
 end

 function codectx_mt:_get_loader()
-  return coro_wrap(function()
-    self:_generate()
-  end)
+  return self:_generate()
 end

 function codectx_mt:as_string()

The best thing I can do on my side as long as I don't control that package is to remove any mention of the Luarocks install method on the readme (that I inserted when I expected to publish it quickly).

QimatLuo commented 6 years ago

@jdesgats - I'll just use the code on GitHub instead of luarocks install for now. Thanks for your response.