api-field-json-th (empty) → 0.1.0.0
raw patch · 5 files changed
+136/−0 lines, 5 filesdep +HUnitdep +aesondep +api-field-json-thsetup-changed
Dependencies added: HUnit, aeson, api-field-json-th, base, lens, split, template-haskell, text
Files
- Data/Aeson/APIFieldJsonTH.hs +43/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- api-field-json-th.cabal +40/−0
- test/Spec.hs +21/−0
+ Data/Aeson/APIFieldJsonTH.hs view
@@ -0,0 +1,43 @@+-- |+--+-- Utils for using aeson's deriveJSON with lens's makeFields++module Data.Aeson.APIFieldJsonTH (+ -- * How to use this library+ -- $use+ deriveApiFieldJSON+ ) where++import Prelude+import Data.Aeson.TH+import Control.Lens+import qualified Data.Char as C (toLower)+import Language.Haskell.TH (Q, Dec, Name)+import Data.List.Split++-- $use+--+-- > data SomeQuery = SomeQuery {+-- > _someQueryPage :: Int+-- > , _someQueryText :: String+-- > } deriving (Eq, Show)+-- > makeFields ''SomeQuery+-- > deriveApiFieldJSON ''SomeQuery+--+-- This is compatible with the next json+--+-- > {"page": 3, "text": "foo"}+--++-- | レコード名を落としてjsonインスタンス化+deriveApiFieldJSON :: Name -> Q[Dec]+deriveApiFieldJSON name = deriveJSON defaultOptions{fieldLabelModifier = firstLower . dropPrefix name} name++firstLower :: String -> String+firstLower (x:xs) = (C.toLower x):xs++typeName :: String -> String+typeName = view _last . splitOn "."++dropPrefix :: Name -> String -> String+dropPrefix = drop . (1 + ) . length . typeName . show
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2016++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Author name here nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ api-field-json-th.cabal view
@@ -0,0 +1,40 @@+name: api-field-json-th+version: 0.1.0.0+synopsis: Initial project template from stack+description: Please see README.md+homepage: https://github.com/githubuser/api-field-json-th#readme+license: BSD3+license-file: LICENSE+author: Author name here+maintainer: example@example.com+copyright: 2016 Author name here+category: Web+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ exposed-modules: Data.Aeson.APIFieldJsonTH+ build-depends: base >= 4.7 && < 5+ , text+ , aeson+ , lens+ , split+ , template-haskell+ default-language: Haskell2010++test-suite api-field-json-th-test+ type: exitcode-stdio-1.0+ hs-source-dirs: test+ main-is: Spec.hs+ build-depends: base+ , api-field-json-th+ , HUnit+ , aeson+ , lens+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/githubuser/api-field-json-th
+ test/Spec.hs view
@@ -0,0 +1,21 @@+{-# LANGUAGE TemplateHaskell, OverloadedStrings, MultiParamTypeClasses, FunctionalDependencies, TypeSynonymInstances, FlexibleInstances #-}+import Data.Aeson.APIFieldJsonTH+import Test.HUnit+import Data.Aeson+import Control.Lens++data SomeQuery = SomeQuery {+ _someQueryPage :: Int+ , _someQueryText :: String+} deriving (Eq, Show)+makeFields ''SomeQuery+deriveApiFieldJSON ''SomeQuery++fromJson = "fromJson example" ~: dec ~?= (Just $ SomeQuery 1 "hoge")+ where dec = decode "{\"page\": 1, \"text\": \"hoge\"}"++lensExample = "lens makeFields example" ~: (r ^. page) ~?= 3+ where r = SomeQuery 3 "hoge"++-- main :: IO ()+main = runTestTT $ TestList [fromJson, lensExample]