Permalink
Browse files

share flatten implementation across sockets

  • Loading branch information...
1 parent 88da5e3 commit 80a1d0a4dad5d45505fb82f61775e2970429d80d @leafo committed Oct 21, 2016
Showing with 51 additions and 68 deletions.
  1. +1 −0 pgmoon-dev-1.rockspec
  2. +3 −19 pgmoon/cqueues.lua
  3. +2 −15 pgmoon/cqueues.moon
  4. +3 −19 pgmoon/socket.lua
  5. +2 −15 pgmoon/socket.moon
  6. +23 −0 pgmoon/util.lua
  7. +17 −0 pgmoon/util.moon
@@ -29,6 +29,7 @@ build = {
["pgmoon.hstore"] = "pgmoon/hstore.lua",
["pgmoon.json"] = "pgmoon/json.lua",
["pgmoon.socket"] = "pgmoon/socket.lua",
+ ["pgmoon.util"] = "pgmoon/util.lua",
},
}
View
@@ -1,21 +1,5 @@
-local __flatten
-__flatten = function(t, buffer)
- local _exp_0 = type(t)
- if "string" == _exp_0 then
- buffer[#buffer + 1] = t
- elseif "table" == _exp_0 then
- for _index_0 = 1, #t do
- local thing = t[_index_0]
- __flatten(thing, buffer)
- end
- end
-end
-local _flatten
-_flatten = function(t)
- local buffer = { }
- __flatten(t, buffer)
- return table.concat(buffer)
-end
+local flatten
+flatten = require("pgmoon.util").flatten
local CqueuesSocket
do
local _class_0
@@ -41,7 +25,7 @@ do
return self.sock:starttls()
end,
send = function(self, ...)
- return self.sock:write(_flatten(...))
+ return self.sock:write(flatten(...))
end,
receive = function(self, ...)
return self.sock:read(...)
View
@@ -1,18 +1,5 @@
--- make luasockets send behave like openresty's
-__flatten = (t, buffer) ->
- switch type(t)
- when "string"
- buffer[#buffer + 1] = t
- when "table"
- for thing in *t
- __flatten thing, buffer
-
-
-_flatten = (t) ->
- buffer = {}
- __flatten t, buffer
- table.concat buffer
+import flatten from require "pgmoon.util"
class CqueuesSocket
connect: (host, port, opts) =>
@@ -38,7 +25,7 @@ class CqueuesSocket
@sock\starttls!
send: (...) =>
- @sock\write _flatten ...
+ @sock\write flatten ...
receive: (...) =>
@sock\read ...
View
@@ -1,23 +1,7 @@
local luasocket
do
- local __flatten
- __flatten = function(t, buffer)
- local _exp_0 = type(t)
- if "string" == _exp_0 then
- buffer[#buffer + 1] = t
- elseif "table" == _exp_0 then
- for _index_0 = 1, #t do
- local thing = t[_index_0]
- __flatten(thing, buffer)
- end
- end
- end
- local _flatten
- _flatten = function(t)
- local buffer = { }
- __flatten(t, buffer)
- return table.concat(buffer)
- end
+ local flatten
+ flatten = require("pgmoon.util").flatten
local proxy_mt = {
__index = function(self, key)
local sock = self.sock
@@ -47,7 +31,7 @@ do
local proxy = setmetatable({
sock = sock,
send = function(self, ...)
- return self.sock:send(_flatten(...))
+ return self.sock:send(flatten(...))
end,
getreusedtimes = function(self)
return 0
View
@@ -1,19 +1,6 @@
luasocket = do
- -- make luasockets send behave like openresty's
- __flatten = (t, buffer) ->
- switch type(t)
- when "string"
- buffer[#buffer + 1] = t
- when "table"
- for thing in *t
- __flatten thing, buffer
-
-
- _flatten = (t) ->
- buffer = {}
- __flatten t, buffer
- table.concat buffer
+ import flatten from require "pgmoon.util"
proxy_mt = {
__index: (key) =>
@@ -41,7 +28,7 @@ luasocket = do
sock = socket.tcp ...
proxy = setmetatable {
:sock
- send: (...) => @sock\send _flatten ...
+ send: (...) => @sock\send flatten ...
getreusedtimes: => 0
settimeout: (t) =>
if t
View
@@ -0,0 +1,23 @@
+local flatten
+do
+ local __flatten
+ __flatten = function(t, buffer)
+ local _exp_0 = type(t)
+ if "string" == _exp_0 then
+ buffer[#buffer + 1] = t
+ elseif "table" == _exp_0 then
+ for _index_0 = 1, #t do
+ local thing = t[_index_0]
+ __flatten(thing, buffer)
+ end
+ end
+ end
+ flatten = function(t)
+ local buffer = { }
+ __flatten(t, buffer)
+ return table.concat(buffer)
+ end
+end
+return {
+ flatten = flatten
+}
View
@@ -0,0 +1,17 @@
+
+-- make luasockets send behave like openresty's
+flatten = do
+ __flatten = (t, buffer) ->
+ switch type(t)
+ when "string"
+ buffer[#buffer + 1] = t
+ when "table"
+ for thing in *t
+ __flatten thing, buffer
+
+ (t) ->
+ buffer = {}
+ __flatten t, buffer
+ table.concat buffer
+
+{:flatten}

0 comments on commit 80a1d0a

Please sign in to comment.