Created
December 15, 2014 19:41
-
-
Save smurphy8/e7af813d09303f5685f6 to your computer and use it in GitHub Desktop.
Data Template Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| * Form | |
| ** Company | |
| ** Address | |
| ** Rows | |
| *** Items | |
| **** ItemType | |
| ***** ItemLabel Label | |
| ****** "Name of Lease Operator" | |
| ***** ItemInput Input | |
| ****** InputTypeText "Scott" | |
| **** ItemType | |
| ***** ItemLabel Label | |
| ****** "Field Name_1" | |
| ***** ItemInput Input | |
| ****** InputTypeText "Ling's Oilfield" | |
| **** ItemType | |
| ***** ItemLabel Label | |
| ****** "Flowback Water_1" | |
| ***** ItemInput Input | |
| ****** InputTypeText "10" | |
| **** ItemType | |
| ***** ItemLabel Label | |
| ****** "Click to Submit" | |
| **** ItemType | |
| ***** ItemButton Button | |
| ****** | |
| ----------- | |
| Transforms to: | |
| * DataTemplate | |
| ** Company | |
| ** Address | |
| ** Item | |
| *** Label "Name of Lease Operator" | |
| *** ValueText "Scott" | |
| ** Item | |
| *** Label "Field Name_1" | |
| *** ValueText "Ling's Oil Field" | |
| ** Item | |
| *** Label "Flowback Water_1" | |
| *** ValueText "10" | |
| # Notice the Button Item has been filtered out | |
| DataTemplate Generates Code: | |
| ``` haskell | |
| data DataTemplate = DataTemplate { company::Company | |
| address :: Address | |
| templateItems :: [Item]} | |
| data Item = Item { | |
| label :: Text | |
| , value :: Input } | |
| -- | encode a list of items as a flat single object instead of as an array of objects | |
| encodeItemsAsObject :: [Item] -> Value | |
| encodeItemsAsObject items = object $ fmap objectMaker items | |
| where | |
| objectMaker (Item { l=label, | |
| v=value}) = (label .= value) | |
| decodeObjectAsItems :: Value -> [Item] | |
| decodeObjectAsItems (Object o) = ... | |
| instance ToJSON DataTemplate where | |
| toJSON (DataTemplate c a ts) = [("company".= c) | |
| ,("address" .= a) | |
| ,"data" .= encodeItemsAsObject ts] | |
| instance FromJSON DataTemplate where | |
| toJSON (Object o) = "company" <$> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment