如何避免不同“字符串”之间的转换使用 snapframework 在 haskell 中输入?
我想生成 POST 数据的解码结果。许多代码在转换“字符串”时被“浪费”。这使得代码变得丑陋。还有更好的解决方案吗?
import Codec.Binary.Url (decode')
import qualified Data.ByteString.Lazy.Char8 as L (unpack)
import qualified Data.ByteString.Char8 as S (unpack, pack)
import qualified Data.ByteString.Lazy as LBS (pack)
decodeUrlHandler :: Snap()
decodeUrlHandler = do
body <- readRequestBody (maxBound :: Int64)
writeLBS $ LBS.pack $ map (fromMaybe 0) $ decode' $ L.unpack body
为此目的您的代码是什么?
I want to produce the decoded result for POST data. Much code is 'wasted' in converting 'string'. That makes code ugly. Any better solutions?
import Codec.Binary.Url (decode')
import qualified Data.ByteString.Lazy.Char8 as L (unpack)
import qualified Data.ByteString.Char8 as S (unpack, pack)
import qualified Data.ByteString.Lazy as LBS (pack)
decodeUrlHandler :: Snap()
decodeUrlHandler = do
body <- readRequestBody (maxBound :: Int64)
writeLBS $ LBS.pack $ map (fromMaybe 0) $ decode' $ L.unpack body
What would your code for this purpose be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Snap 自动解码请求并通过 请求数据类型。它提供了函数 getRequest和 withRequest 用于检索请求并用于获取各种零件的许多其他访问器函数。
还有一些常用操作的便利功能。要获取 POST 或 GET 参数,请参阅 获取参数。
Snap 将其作为 ByteString 提供给您,因为该 API 处于相当低的抽象级别,让用户自行决定如何处理文本编码等事务。我建议您使用更高效的文本类型而不是字符串。 Readable 类型类还提供一种消除这些转换的一些样板的机制。数字和文本的默认实例采用 UTF8 编码。
Snap automatically decodes the request and makes it available to you through the Request data type. It provides functions getRequest and withRequest for retrieving the request and a number of other accessor functions for getting various parts.
There are also convenience functions for common operations. To get a POST or GET parameter see getParam.
Snap gives it to you as a ByteString because this API sits at a fairly low level of abstraction, leaving it up to the user how to handle things like text encoding. I would recommend that you use the much more efficient Text type instead of String. The Readable type class also provides a mechanism for eliminating some of the boilerplate of these conversions. The default instances for numbers and Text assume UTF8 encoding.