From a29cdf7e6889d104e2b25c89a366486d23a81b33 Mon Sep 17 00:00:00 2001 From: Libby Horacek Date: Tue, 24 Jan 2017 13:13:54 -0500 Subject: [PATCH 1/3] Add an 'I' Field Some fields in the JSON are numbers -- and numbers in JSON are all one type. Aeson sees them as this "Scientific" number -- a float or double? So if WordPress has a post with an "id" 34234, then Offset fills it as "34234.0". Which is bad when we're creating links to "author/${wpId}". The problem seems to be that Fields have some different types, like objects or lists, but there's no "type" that says anything about the content of a field. Ideally, it would be getting a JSON Number, then trying to convert that into an Integer. But what the "build splices" --- spec/Main.hs | 2 +- src/Web/Offset/Field.hs | 15 +++++++++++---- src/Web/Offset/Splices.hs | 7 +++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/spec/Main.hs b/spec/Main.hs index dc91e3a..663e7fa 100644 --- a/spec/Main.hs +++ b/spec/Main.hs @@ -223,7 +223,7 @@ larcenyFillTests = do describe "" $ it "should show the title, id, and excerpt" $ do "" `shouldRender` "Foo bar" - "" `shouldRender` "1.0" + "" `shouldRender` "1" "" `shouldRender` "summary" describe "" $ it "should show the content" $ diff --git a/src/Web/Offset/Field.hs b/src/Web/Offset/Field.hs index dbd954b..7699cb7 100644 --- a/src/Web/Offset/Field.hs +++ b/src/Web/Offset/Field.hs @@ -5,13 +5,18 @@ module Web.Offset.Field where import Control.Applicative ((<$>)) import Control.Monad.State +import Data.Maybe (fromMaybe) import Data.Monoid ((<>)) import Data.Text (Text) import qualified Data.Text as T +import Text.Read (readMaybe) import Web.Larceny +import Web.Offset.Utils + -- TODO(dbp 2014-10-14): date should be parsed and nested. data Field s = F Text -- A single flat field + | I Text -- A single flat field that should be an integer | P Text (Text -> Fill s) -- A customly parsed flat field | N Text [Field s] -- A nested object field | C Text [Text] -- A nested text field that is found by following the specified path @@ -29,6 +34,7 @@ mergeFields fo (f:fs) = mergeFields (overrideInList False f fo) fs else m : overrideInList v fl ms matchesName a b = getName a == getName b getName (F t) = t + getName (I t) = t getName (P t _) = t getName (N t _) = t getName (C t _) = t @@ -42,6 +48,7 @@ mergeFields fo (f:fs) = mergeFields (overrideInList False f fo) fs instance Show (Field s) where show (F t) = "F(" <> T.unpack t <> ")" + show (I t) = "I(" <> T.unpack t <> ")" show (P t _) = "P(" <> T.unpack t <> ",{code})" show (N t n) = "N(" <> T.unpack t <> "," <> show n <> ")" show (C t p) = "C(" <> T.unpack t <> ":" <> T.unpack (T.intercalate "/" p) <> ")" @@ -49,7 +56,7 @@ instance Show (Field s) where show (M t m) = "M(" <> T.unpack t <> "," <> show m <> ")" postFields :: [Field s] -postFields = [F "id" +postFields = [I "id" ,C "title" ["title", "rendered"] ,F "status" ,F "type" @@ -67,8 +74,8 @@ postFields = [F "id" ,F "height" ,F "url"] ]]] - ,N "terms" [M "category" [F "id", F "name", F "slug", F "count"] - ,M "post_tag" [F "id", F "name", F "slug", F "count"]] + ,N "terms" [M "category" [I "id", F "name", F "slug", F "count"] + ,M "post_tag" [I "id", F "name", F "slug", F "count"]] ] dateSplice :: Text -> Fill s @@ -79,4 +86,4 @@ dateSplice date = , ("wpDay", textFill d)] where parseDate :: Text -> (Text,Text,Text) parseDate = tuplify . T.splitOn "-" . T.takeWhile (/= 'T') - tuplify (y:m:d:_) = (y,m,d) + tuplify (y:m:d:_) = (y,m,d) \ No newline at end of file diff --git a/src/Web/Offset/Splices.hs b/src/Web/Offset/Splices.hs index 98828cb..a9fd01d 100644 --- a/src/Web/Offset/Splices.hs +++ b/src/Web/Offset/Splices.hs @@ -193,6 +193,8 @@ postSubs :: [Field s] -> Object -> Substitutions s postSubs extra object = subs (map (buildSplice object) (mergeFields postFields extra)) where buildSplice o (F n) = (transformName n, textFill $ getText n o) + buildSplice o (I n) = + (transformName n, textFill $ maybeInteger (getText n o)) buildSplice o (P n fill') = (transformName n, fill' $ getText n o) buildSplice o (N n fs) = @@ -217,6 +219,11 @@ postSubs extra object = subs (map (buildSplice object) (mergeFields postFields e Just (String t) -> t Just (Number i) -> T.pack $ show i _ -> "" + maybeInteger :: Text -> Text + maybeInteger integerField = + let mInteger = T.stripSuffix ".0" integerField in + fromMaybe integerField mInteger + -- * -- Internal -- * -- From 8483ce391a151dacf07f29ff9e765943f2af909c Mon Sep 17 00:00:00 2001 From: Libby Horacek Date: Tue, 24 Jan 2017 13:33:09 -0500 Subject: [PATCH 2/3] Revert "Add an 'I' Field" This reverts commit a29cdf7e6889d104e2b25c89a366486d23a81b33. --- spec/Main.hs | 2 +- src/Web/Offset/Field.hs | 15 ++++----------- src/Web/Offset/Splices.hs | 7 ------- 3 files changed, 5 insertions(+), 19 deletions(-) diff --git a/spec/Main.hs b/spec/Main.hs index 663e7fa..dc91e3a 100644 --- a/spec/Main.hs +++ b/spec/Main.hs @@ -223,7 +223,7 @@ larcenyFillTests = do describe "" $ it "should show the title, id, and excerpt" $ do "" `shouldRender` "Foo bar" - "" `shouldRender` "1" + "" `shouldRender` "1.0" "" `shouldRender` "summary" describe "" $ it "should show the content" $ diff --git a/src/Web/Offset/Field.hs b/src/Web/Offset/Field.hs index 7699cb7..dbd954b 100644 --- a/src/Web/Offset/Field.hs +++ b/src/Web/Offset/Field.hs @@ -5,18 +5,13 @@ module Web.Offset.Field where import Control.Applicative ((<$>)) import Control.Monad.State -import Data.Maybe (fromMaybe) import Data.Monoid ((<>)) import Data.Text (Text) import qualified Data.Text as T -import Text.Read (readMaybe) import Web.Larceny -import Web.Offset.Utils - -- TODO(dbp 2014-10-14): date should be parsed and nested. data Field s = F Text -- A single flat field - | I Text -- A single flat field that should be an integer | P Text (Text -> Fill s) -- A customly parsed flat field | N Text [Field s] -- A nested object field | C Text [Text] -- A nested text field that is found by following the specified path @@ -34,7 +29,6 @@ mergeFields fo (f:fs) = mergeFields (overrideInList False f fo) fs else m : overrideInList v fl ms matchesName a b = getName a == getName b getName (F t) = t - getName (I t) = t getName (P t _) = t getName (N t _) = t getName (C t _) = t @@ -48,7 +42,6 @@ mergeFields fo (f:fs) = mergeFields (overrideInList False f fo) fs instance Show (Field s) where show (F t) = "F(" <> T.unpack t <> ")" - show (I t) = "I(" <> T.unpack t <> ")" show (P t _) = "P(" <> T.unpack t <> ",{code})" show (N t n) = "N(" <> T.unpack t <> "," <> show n <> ")" show (C t p) = "C(" <> T.unpack t <> ":" <> T.unpack (T.intercalate "/" p) <> ")" @@ -56,7 +49,7 @@ instance Show (Field s) where show (M t m) = "M(" <> T.unpack t <> "," <> show m <> ")" postFields :: [Field s] -postFields = [I "id" +postFields = [F "id" ,C "title" ["title", "rendered"] ,F "status" ,F "type" @@ -74,8 +67,8 @@ postFields = [I "id" ,F "height" ,F "url"] ]]] - ,N "terms" [M "category" [I "id", F "name", F "slug", F "count"] - ,M "post_tag" [I "id", F "name", F "slug", F "count"]] + ,N "terms" [M "category" [F "id", F "name", F "slug", F "count"] + ,M "post_tag" [F "id", F "name", F "slug", F "count"]] ] dateSplice :: Text -> Fill s @@ -86,4 +79,4 @@ dateSplice date = , ("wpDay", textFill d)] where parseDate :: Text -> (Text,Text,Text) parseDate = tuplify . T.splitOn "-" . T.takeWhile (/= 'T') - tuplify (y:m:d:_) = (y,m,d) \ No newline at end of file + tuplify (y:m:d:_) = (y,m,d) diff --git a/src/Web/Offset/Splices.hs b/src/Web/Offset/Splices.hs index a9fd01d..98828cb 100644 --- a/src/Web/Offset/Splices.hs +++ b/src/Web/Offset/Splices.hs @@ -193,8 +193,6 @@ postSubs :: [Field s] -> Object -> Substitutions s postSubs extra object = subs (map (buildSplice object) (mergeFields postFields extra)) where buildSplice o (F n) = (transformName n, textFill $ getText n o) - buildSplice o (I n) = - (transformName n, textFill $ maybeInteger (getText n o)) buildSplice o (P n fill') = (transformName n, fill' $ getText n o) buildSplice o (N n fs) = @@ -219,11 +217,6 @@ postSubs extra object = subs (map (buildSplice object) (mergeFields postFields e Just (String t) -> t Just (Number i) -> T.pack $ show i _ -> "" - maybeInteger :: Text -> Text - maybeInteger integerField = - let mInteger = T.stripSuffix ".0" integerField in - fromMaybe integerField mInteger - -- * -- Internal -- * -- From f42cbe9d486be538df21d808cb4f71524f965c1c Mon Sep 17 00:00:00 2001 From: Libby Horacek Date: Tue, 24 Jan 2017 13:35:38 -0500 Subject: [PATCH 3/3] Show integers differently from floats --- spec/Main.hs | 2 +- src/Web/Offset/Splices.hs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/Main.hs b/spec/Main.hs index dc91e3a..663e7fa 100644 --- a/spec/Main.hs +++ b/spec/Main.hs @@ -223,7 +223,7 @@ larcenyFillTests = do describe "" $ it "should show the title, id, and excerpt" $ do "" `shouldRender` "Foo bar" - "" `shouldRender` "1.0" + "" `shouldRender` "1" "" `shouldRender` "summary" describe "" $ it "should show the content" $ diff --git a/src/Web/Offset/Splices.hs b/src/Web/Offset/Splices.hs index 98828cb..4269881 100644 --- a/src/Web/Offset/Splices.hs +++ b/src/Web/Offset/Splices.hs @@ -215,7 +215,9 @@ postSubs extra object = subs (map (buildSplice object) (mergeFields postFields e traverseObject pth o = foldl (\o x -> unObj . M.lookup x $ o) o pth getText n o = case M.lookup n o of Just (String t) -> t - Just (Number i) -> T.pack $ show i + Just (Number i) -> case floatingOrInteger i of + Right r -> tshow r + Left i -> tshow i _ -> "" -- * -- Internal -- * --