rest-gen 0.11 → 0.12
raw patch · 3 files changed
+20/−10 lines, 3 files
Files
- CHANGELOG.md +4/−0
- rest-gen.cabal +1/−1
- src/Rest/Gen/Haskell/Generate.hs +15/−9
CHANGELOG.md view
@@ -1,5 +1,9 @@ # Changelog +### 0.12++* Haskell: Module rewrites such as `Data.Text.Internal` -> `Data.Text` now produces qualified imports `import qualified Data.Text as Data.Text` instead of `import qualified Data.Text as Data.Text.Lazy`. This prevents building against different versions of the same package that may have moved the internal module (as is the case with `text`) from generating different clients.+ ## 0.11 * Bugfix: Haskell: Resources without a getter now generate identifier arguments for other end points
rest-gen.cabal view
@@ -1,5 +1,5 @@ name: rest-gen-version: 0.11+version: 0.12 description: Documentation and client generation from rest definition. synopsis: Documentation and client generation from rest definition. maintainer: code@silk.co
src/Rest/Gen/Haskell/Generate.hs view
@@ -10,6 +10,7 @@ import Control.Monad import Data.Label (mkLabels, modify, set) import Data.List+import Data.List.Split import Data.Maybe import Prelude hiding (id, (.)) import Safe@@ -105,7 +106,7 @@ , mkStack funcs ] where- (funcs, mods) = unzip $ map (mkFunction (apiVersion ctx) $ resName node) $ resItems node+ (funcs, mods) = unzip $ map (mkFunction (rewrites ctx) (apiVersion ctx) $ resName node) $ resItems node rewrittenMods = rewriteModules (rewrites ctx) $ nub $ concat mods mkImports :: HaskellContext -> ApiResource -> [String] -> Code@@ -122,9 +123,9 @@ dataImports = mkStack . map ("import qualified " <+>) $ datImp mkImport p = "import qualified" <++> qualModName (namespace ctx ++ p) <++> "as" <++> modName (last p) -mkFunction :: Version -> String -> ApiAction -> (Code, [String])-mkFunction ver res (ApiAction _ lnk ai) =- let mInp = fmap inputInfo $ chooseType $ inputs ai+mkFunction :: [(String,String)] -> Version -> String -> ApiAction -> (Code, [String])+mkFunction rewrits ver res (ApiAction _ lnk ai) =+ let mInp = fmap (inputInfo rewrits) $ chooseType $ inputs ai identMod = maybe [] Ident.haskellModule (ident ai) defaultErrorConversion = if fmap dataType (chooseType (outputs ai)) == Just JSON then "fromJSON" else "fromXML" (oMod, oType, oCType, oFunc) = maybe ([], "()", "text/plain", "(const ())") outputInfo $ chooseType $ outputs ai@@ -228,10 +229,15 @@ by = if target /= [] && (isJust (ident ai) || actionType ai == UpdateMany) then ["by"] else [] get = if isAccessor ai then [] else ["get"] +-- | Rewrites multiple flattened module names rewriteModules :: [(String, String)] -> [String] -> [String] rewriteModules _ [] = []-rewriteModules rw (v : vs) = maybe v (++ (" as " ++ v)) (lookup v rw) : rewriteModules rw vs+rewriteModules rw (v : vs) = maybe v (\m -> m ++ " as " ++ m) (lookup v rw) : rewriteModules rw vs +-- | Rewrites a single module name+rewriteModule :: [(String, String)] -> [String] -> [String]+rewriteModule rewrits m = maybe m (splitOn ".") (lookup (intercalate "." $ m) rewrits)+ hsName :: [String] -> String hsName [] = "" hsName (x : xs) = clean $ downFirst x ++ concatMap upFirst xs@@ -254,12 +260,12 @@ dataName :: String -> String dataName = modName -inputInfo :: DataDescription -> ([String], String, String, String)-inputInfo ds =+inputInfo :: [(String,String)] -> DataDescription -> ([String], String, String, String)+inputInfo rewrits ds = case dataType ds of String -> ([], "String", "text/plain", "fromString")- XML -> (haskellModule ds, haskellType ds, "text/xml", "toXML")- JSON -> (haskellModule ds, haskellType ds, "text/json", "toJSON")+ XML -> (rewriteModule rewrits (haskellModule ds), haskellType ds, "text/xml", "toXML")+ JSON -> (rewriteModule rewrits (haskellModule ds), haskellType ds, "text/json", "toJSON") File -> ([], "ByteString", "application/octet-stream", "id") Other -> ([], "ByteString", "text/plain", "id")