Skip to content

Commit 439c8a6

Browse files
committed
Allow template to access non-object array values
1 parent e47d873 commit 439c8a6

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

spec/Common.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ fauxRequester _ "/false" [] =
218218
toWPResp $ enc [boolFieldFalse]
219219
fauxRequester _ "/true" [] =
220220
toWPResp $ enc [boolFieldTrue]
221+
fauxRequester _ "/object_array" [] =
222+
toWPResp $ enc $
223+
object ["some_array" .=
224+
[ object ["object_key" .= ("object value 1" :: Text)]
225+
, object ["object_key" .= ("object value 2" :: Text)]]]
226+
fauxRequester _ "/number_array" [] = toWPResp $ enc $ object ["some_array" .= [1 :: Int, 2 :: Int, 3 :: Int]]
227+
fauxRequester _ "/string_array" [] = toWPResp $ enc $ object ["some_array" .= ["a" :: Text, "b" :: Text, "c" :: Text]]
221228
fauxRequester _ "/many-pages" [] =
222229
return $ Right $ WPResponse [(CI.mk "X-WP-TotalPages", "478")] (enc [article1])
223230
fauxRequester mRecord rqPath rqParams = do

spec/Main.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ larcenyFillTests = do
168168
"<wpCustom endpoint=\"true\"><wpNotThere><wpPerson><wpName /></wpPerson></wpNotThere></wpCustom>"
169169
`shouldRender` ""
170170

171+
it "should render arrays of objects correctly" $
172+
"<wpCustom endpoint=\"object_array\"><wpSomeArray><wpObjectKey /></wpSomeArray></wpCustom>"
173+
`shouldRender` "object value 1 object value 2"
174+
175+
it "should render arrays of strings correctly" $
176+
"<wpCustom endpoint=\"string_array\"><wpSomeArray>The letter <wpArrayItem /></wpSomeArray></wpCustom>"
177+
`shouldRender` "The letter a The letter b The letter c"
178+
179+
it "should render arrays of numbers correctly" $
180+
"<wpCustom endpoint=\"number_array\"><wpSomeArray>The number <wpArrayItem /></wpSomeArray></wpCustom>"
181+
`shouldRender` "The number 1 The number 2 The number 3"
182+
171183

172184
describe "<wpCustomDate>" $ do
173185
it "should parse a date field with the format string it's given" $

src/Web/Offset/Splices.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ jsonToFill (Object o) =
102102
(M.keys o)
103103
jsonToFill (Array v) =
104104
Fill $ \attrs (path, tpl) lib ->
105-
V.foldr mappend "" <$> V.mapM (\e -> unFill (jsonToFill e) attrs (path, tpl) lib) v
105+
V.foldr mappend "" <$> V.mapM (\e -> unFill (jsonToFillArrayItem e) attrs (path, tpl) lib) v
106106
jsonToFill (String s) = rawTextFill s
107107
jsonToFill (Number n) = case floatingOrInteger n of
108108
Left r -> rawTextFill $ tshow (r :: Double)
@@ -111,6 +111,18 @@ jsonToFill (Bool True) = rawTextFill $ tshow True
111111
jsonToFill (Bool False) = rawTextFill "<!-- JSON field found, but value is false. -->"
112112
jsonToFill (Null) = rawTextFill "<!-- JSON field found, but value is null. -->"
113113

114+
jsonToFillArrayItem :: Value -> Fill s
115+
jsonToFillArrayItem o@(Object _) = jsonToFill o
116+
jsonToFillArrayItem a@(Array _) = jsonToFill a
117+
jsonToFillArrayItem (String s) = fillChildrenWith $ subs [("wpArrayItem", rawTextFill s)]
118+
jsonToFillArrayItem (Number n) =
119+
case floatingOrInteger n of
120+
Left r -> fillChildrenWith $ subs [("wpArrayItem", textFill $ tshow (r :: Double))]
121+
Right i -> fillChildrenWith $ subs [("wpArrayItem", textFill $ tshow (i :: Integer))]
122+
jsonToFillArrayItem b@(Bool True) = jsonToFill b
123+
jsonToFillArrayItem b@(Bool False) = jsonToFill b
124+
jsonToFillArrayItem n@(Null) = jsonToFill n
125+
114126
wpCustomAggregateFill :: Wordpress b -> Fill s
115127
wpCustomAggregateFill wp =
116128
useAttrs (a "endpoint") (customAggregateFill wp)

0 commit comments

Comments
 (0)