Как получить значение формы из поля Singleton в пищеварительных функторах?
Я пытаюсь использовать пользовательский тип данных в форме с пищеварительными функторами.
Этот тип данных относится к полю идентификатора базы данных. Я определил это как:
dbkey :: (Monad m, Monoid v) => Maybe DBKey -> Form v m DBKey
dbkey p = Pure . Singleton $ (fromMaybe (DBKey 0) p)
Теперь оставшаяся проблема заключается в получении этого значения в "форме". Я не могу использовать fieldInputText
потому что это ожидает Text
поле - commentId: expected (Text _), but got: (Singleton _)
,
Как можно достичь вышеизложенного? Приходится писать свою собственную версию fieldInputText
кажется довольно сложным.
Обновление У меня пока есть следующее:
fieldInputSingleton :: forall a v. Text -> View v -> Proxy a -> a
fieldInputSingleton ref (View _ _ form input _ method) p =
queryField path form eval'
where
path = toPath ref
givenInput = lookupInput path input
eval' field = case field of
Singleton t -> evalField method givenInput (Singleton t)
f -> error $ T.unpack ref ++ ": expected (Singleton _), " ++ "but got: (" ++ show f ++ ")"
Однако я сталкиваюсь с:
• Couldn't match type ‘a’ with ‘b’
‘a’ is a rigid type variable bound by
the type signature for:
fieldInputSingleton :: forall a v. Text -> View v -> Proxy a -> a
at /home/chris/Projects/Haskell/Blog/src/View2.hs:26:1-65
‘b’ is a rigid type variable bound by
a type expected by the context:
forall b. Field v b -> a
at /home/chris/Projects/Haskell/Blog/src/View2.hs:28:5-30
Expected type: Field v b -> a
Actual type: Field v a -> a
• In the third argument of ‘queryField’, namely ‘eval'’
In the expression: queryField path form eval'
In an equation for ‘fieldInputSingleton’:
fieldInputSingleton ref (View _ _ form input _ method) p
= queryField path form eval'
where
path = toPath ref
givenInput = lookupInput path input
eval' field
= case field of
Singleton t -> evalField method givenInput (Singleton t)
f -> error
$ T.unpack ref
++ ": expected (Singleton _), " ++ "but got: (" ++ show f ++ ")"
• Relevant bindings include
eval' :: Field v a -> a
(bound at /home/chris/Projects/Haskell/Blog/src/View2.hs:32:5)
p :: Proxy a
(bound at /home/chris/Projects/Haskell/Blog/src/View2.hs:27:56)
fieldInputSingleton :: Text -> View v -> Proxy a -> a
(bound at /home/chris/Projects/Haskell/Blog/src/View2.hs:27:1)
|
28 | queryField path form eval'
| ^^^^^