pinch-gen 0.4.1.0 → 0.4.2.0
raw patch · 5 files changed
+46/−5 lines, 5 files
Files
- CHANGELOG.md +6/−0
- README.md +1/−1
- pinch-gen.cabal +1/−1
- src/Pinch/Generate.hs +36/−3
- src/Pinch/Generate/Pretty.hs +2/−0
CHANGELOG.md view
@@ -1,3 +1,9 @@+0.4.2 (2021-05-31)+==================++* Support for constants (thanks to @alexbiehl)+* Fix compilation error in generated code due to missing OverloadedStrings pragma (thanks to @alexbiehl)+ 0.4.1 (2021-04-26) ==================
README.md view
@@ -36,7 +36,7 @@ | pinch version | pinch-gen version | |---------------|-------------------|-| 0.4 | 0.4 |+| 0.4 | 0.4.* | Example
pinch-gen.cabal view
@@ -1,7 +1,7 @@ cabal-version: >=1.10 name: pinch-gen-version: 0.4.1.0+version: 0.4.2.0 -- synopsis: synopsis: A code generator for the pinch Thrift library. homepage: https://github.com/phile314/pinch-gen
src/Pinch/Generate.hs view
@@ -88,7 +88,7 @@ let tyMap = Map.unions tyMaps let (typeDecls, clientDecls, serverDecls) = unzip3 $ runReader (traverse gDefinition defs) $ Context tyMap s let mkMod suffix = H.Module (H.ModuleName $ modBaseName <> suffix)- [ H.PragmaLanguage "TypeFamilies, DeriveGeneric, TypeApplications"+ [ H.PragmaLanguage "TypeFamilies, DeriveGeneric, TypeApplications, OverloadedStrings" , H.PragmaOptsGhc "-fno-warn-unused-imports -fno-warn-name-shadowing -fno-warn-unused-matches" ] pure $ [ -- types@@ -159,10 +159,43 @@ gDefinition :: Definition SourcePos -> GenerateM ([H.Decl], [H.Decl], [H.Decl]) gDefinition def = case def of- ConstDefinition _ -> pure ([], [], [])+ ConstDefinition c -> (\x -> (x, [], [])) <$> gConst c TypeDefinition ty -> (\x -> (x, [], [])) <$> gType ty ServiceDefinition s -> gService s +gConst :: A.Const SourcePos -> GenerateM [H.Decl]+gConst const = do+ tyRef <- gTypeReference (constValueType const)+ value <- gConstValue (constValue const)+ pure+ [ H.TypeSigDecl name tyRef+ , H.FunBind [H.Match name [] value]+ ]+ where+ name = decapitalize (constName const)++gConstValue :: A.ConstValue SourcePos -> GenerateM H.Exp+gConstValue val = case val of+ ConstInt n _ -> pure (H.ELit (H.LInt n))+ ConstFloat n _ -> pure (H.ELit (H.LFloat n))+ ConstLiteral s _ -> pure (H.ELit (H.LString s))+ ConstIdentifier id _+ | xs @(_:_:_) <- T.splitOn "." id -> do+ map <- asks cModuleMap+ case Map.lookup (mconcat $ init xs) map of+ Nothing ->+ -- TODO this should probably be an error+ pure (H.EVar (decapitalize id))+ Just (H.ModuleName n) ->+ pure $ H.EVar (n <> "." <> decapitalize (last xs))+ | otherwise -> pure $ H.EVar (decapitalize id)+ ConstList xs _ -> do+ elems <- traverse gConstValue xs+ pure (H.EApp "Data.Vector.fromList" [H.EList elems])+ ConstMap xs _ -> do+ tuples <- traverse (\(k, v) -> H.ETuple <$> traverse gConstValue [k, v]) xs+ pure (H.EApp "Data.HashMap.Strict.fromList" [H.EList tuples])+ gType :: Type SourcePos -> GenerateM [H.Decl] gType ty = case ty of TypedefType t -> gTypedef t@@ -392,7 +425,7 @@ , H.TypeSigDecl (prefix <> "_mkServer") (H.TyLam [H.TyCon serviceConName] (H.TyCon "Pinch.Server.ThriftServer")) , H.FunBind [ H.Match (prefix <> "_mkServer") [H.PVar "server"]- ( H.ELet "functions" + ( H.ELet "functions" (H.EApp "Data.HashMap.Strict.fromList" [ H.EList handlers ] ) ( H.EApp "Pinch.Server.createServer" [ (H.ELam ["nm"]
src/Pinch/Generate/Pretty.hs view
@@ -104,6 +104,7 @@ data Lit = LInt Integer+ | LFloat Double | LString T.Text deriving (Show) @@ -204,6 +205,7 @@ instance Pretty Lit where pretty l = case l of LInt i -> pretty i+ LFloat f -> pretty f LString t -> "\"" <> pretty t <> "\"" cList = concatWith (surround (comma <> space))