假设我的PureScript代码中的用户记录具有以下类型:
{ id :: Number
, username :: String
, email :: Maybe String
, isActive :: Boolean
}
CommonJS模块源自PureScript代码。导出的用户相关函数将从外部JavaScript代码调用。
在JavaScript代码中,"用户"可以表示为:
var alice = {id: 123, username: 'alice', email: 'alice@example.com', isActive: true};
email
可能是null
:
var alice = {id: 123, username: 'alice', email: null, isActive: true};
email
可能会被省略:
var alice = {id: 123, username: 'alice', isActive: true};
isActive
可能会被省略,在这种情况下,它会被假定为true
:
var alice = {id: 123, username: 'alice'};
遗憾的是, id
有时是数字字符串:
var alice = {id: '123', username: 'alice'};
上面的五个JavaScript表示是等效的,应该生成等效的PureScript记录。
如何编写一个带有JavaScript对象并返回用户记录的函数?它将使用null /省略的可选字段的默认值,强制转换为字符串{{1}如果缺少必填字段或者值的类型错误,则抛出一个数字。
我可以看到的两种方法是在PureScript模块中使用FFI或在外部JavaScript代码中定义转换函数。后者看起来很毛茸茸:
id
我不确定FFI版本是如何工作的。我还没有使用过效果。
对不起,这个问题不是很清楚。我还没有足够的理解来确切知道我想知道的是什么。
答案 0 :(得分:8)
我已经制定了一个解决方案。我确信可以改进很多功能,例如将toUser
的类型更改为Json -> Either String User
并保留错误信息。如果您能看到改进此代码的任何方法,请发表评论。 :)
除了一些核心模块之外,此解决方案还使用PureScript-Argonaut。
module Main
( User()
, toEmail
, toId
, toIsActive
, toUser
, toUsername
) where
import Control.Alt ((<|>))
import Data.Argonaut ((.?), toObject)
import Data.Argonaut.Core (JNumber(), JObject(), Json())
import Data.Either (Either(..), either)
import Data.Maybe (Maybe(..))
import Global (isNaN, readFloat)
type User = { id :: Number
, username :: String
, email :: Maybe String
, isActive :: Boolean
}
hush :: forall a b. Either a b -> Maybe b
hush = either (const Nothing) Just
toId :: JObject -> Maybe Number
toId obj = fromNumber <|> fromString
where
fromNumber = (hush $ obj .? "id")
fromString = (hush $ obj .? "id") >>= \s ->
let id = readFloat s in if isNaN id then Nothing else Just id
toUsername :: JObject -> Maybe String
toUsername obj = hush $ obj .? "username"
toEmail :: JObject -> Maybe String
toEmail obj = hush $ obj .? "email"
toIsActive :: JObject -> Maybe Boolean
toIsActive obj = (hush $ obj .? "isActive") <|> Just true
toUser :: Json -> Maybe User
toUser json = do
obj <- toObject json
id <- toId obj
username <- toUsername obj
isActive <- toIsActive obj
return { id: id
, username: username
, email: toEmail obj
, isActive: isActive
}
更新:我已根据Ben Kolera的gist对上述代码进行了改进。
答案 1 :(得分:4)
你看过purescript-foreign
(https://github.com/purescript/purescript-foreign)了吗?我认为这就是你在这里寻找的东西。
答案 2 :(得分:1)
再多一点ffi
module User where
import Data.Maybe
import Data.Function
foreign import data UserExternal :: *
type User =
{
id :: Number,
username :: String,
email :: Maybe String,
isActive :: Boolean
}
type MbUser =
{
id :: Maybe Number,
username :: Maybe String,
email :: Maybe String,
isActive :: Maybe Boolean
}
foreign import toMbUserImpl """
function toMbUserImpl(nothing, just, user) {
var result = {},
properties = ['username', 'email', 'isActive'];
var i, prop;
for (i = 0; i < properties.length; i++) {
prop = properties[i];
if (user.hasOwnProperty(prop)) {
result[prop] = just(user[prop]);
} else {
result[prop] = nothing;
}
}
if (!user.hasOwnProperty('id') || isNaN(parseInt(user.id))) {
result.id = nothing;
} else {
result.id = just(user.id);
}
return result;
}
""" :: forall a. Fn3 (Maybe a) (a -> Maybe a) UserExternal MbUser
toMbUser :: UserExternal -> MbUser
toMbUser ext = runFn3 toMbUserImpl Nothing Just ext
defaultId = 0
defaultName = "anonymous"
defaultActive = false
userFromMbUser :: MbUser -> User
userFromMbUser mbUser =
{
id: fromMaybe defaultId mbUser.id,
username: fromMaybe defaultName mbUser.username,
email: mbUser.email,
isActive: fromMaybe defaultActive mbUser.isActive
}
userFromExternal :: UserExternal -> User
userFromExternal ext = userFromMbUser $ toMbUser ext
答案 3 :(得分:0)
作为gb。写道,这正是构建Foreign
数据类型的原因。在我的头顶:
convert :: Foreign -> F User
convert f = do
id <- f ! "id" >>= readNumber
name <- f ! "name" >>= readString
email <- (f ! "email" >>= readNull >>= traverse readString) <|> pure Nothing
isActive <- (f ! "isActive" >>= readBoolean) <|> pure true
return { id, name, email, isActive }