Separate packages (#21)

* Separate Avro and ProtoBuf things in their own adapter packages
* Separate grpc packages in client and server

Fixes #19
This commit is contained in:
Alejandro Serrano 2019-11-18 12:23:57 +01:00 committed by GitHub
parent 1baa38c3fc
commit bff6493981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
73 changed files with 1029 additions and 348 deletions

View File

@ -0,0 +1,47 @@
cabal-version: >=1.10
name: mu-avro
version: 0.1.0.0
synopsis: Avro serialization support for Mu microservices
-- description:
-- bug-reports:
license: Apache-2.0
license-file: LICENSE
author: Alejandro Serrano, Flavio Corpa
maintainer: alejandro.serrano@47deg.com
-- copyright:
category: Network
build-type: Simple
library
exposed-modules: Mu.Adapter.Avro
, Mu.Adapter.Avro.Example
, Mu.Quasi.Avro
-- other-modules:
-- other-extensions:
build-depends: base >=4.12 && <5
, mu-schema
, avro
, tagged
, aeson
, text
, vector
, containers
, unordered-containers
, sop-core
, bytestring
, template-haskell >= 2.12
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall
-fprint-potential-instances
executable test-avro
main-is: Avro.hs
build-depends: base >=4.12 && <5
, mu-schema
, mu-avro
, avro
, bytestring
hs-source-dirs: test
default-language: Haskell2010
ghc-options: -Wall

View File

@ -5,7 +5,7 @@
MultiParamTypeClasses,
UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Mu.Schema.Adapter.Avro where
module Mu.Adapter.Avro where
import Control.Arrow ((***))
import qualified Data.Avro as A

View File

@ -1,10 +1,9 @@
{-# language DataKinds #-}
{-# language QuasiQuotes #-}
{-# OPTIONS_GHC -ddump-splices #-}
{-# language DataKinds #-}
{-# language QuasiQuotes #-}
module Mu.Schema.AvroExample where
module Mu.Adapter.Avro.Example where
import Mu.Schema.Quasi (avro, avroFile)
import Mu.Quasi.Avro (avro, avroFile)
type Example = [avro|
{

View File

@ -0,0 +1,115 @@
{-# language DataKinds #-}
{-# language LambdaCase #-}
{-# language NamedFieldPuns #-}
{-# language TemplateHaskell #-}
{-# language ViewPatterns #-}
module Mu.Quasi.Avro (
-- * Quasi-quoters for @.avsc@ files
avro
, avroFile
-- * Only for internal use
, schemaFromAvroType
) where
import Data.Aeson (decode)
import qualified Data.Avro.Schema as A
import qualified Data.ByteString as B
import Data.ByteString.Lazy.Char8 (pack)
import Data.Int
import qualified Data.Text as T
import Data.Vector (fromList, toList)
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Mu.Schema.Definition
-- | Imports an avro definition written in-line as a 'Schema'.
avro :: QuasiQuoter
avro =
QuasiQuoter
(const $ fail "cannot use as expression")
(const $ fail "cannot use as pattern")
schemaFromAvroString
(const $ fail "cannot use as declaration")
-- | Imports an avro definition from a file as a 'Schema'.
avroFile :: QuasiQuoter
avroFile = quoteFile avro
schemaFromAvroString :: String -> Q Type
schemaFromAvroString s =
case decode (pack s) of
Nothing -> fail "could not parse avro spec!"
Just (A.Union us) -> schemaFromAvro (toList us)
Just t -> schemaFromAvro [t]
where schemaFromAvro = (typesToList <$>) . mapM schemaDecFromAvroType . flattenAvroDecls
schemaDecFromAvroType :: A.Type -> Q Type
schemaDecFromAvroType (A.Record name _ _ _ fields) =
[t|'DRecord $(textToStrLit $ A.baseName name) '[] $(typesToList <$> mapM avroFieldToType fields)|]
where
avroFieldToType :: A.Field -> Q Type
avroFieldToType field =
[t|'FieldDef $(textToStrLit $ A.fldName field) '[] $(schemaFromAvroType $ A.fldType field)|]
schemaDecFromAvroType (A.Enum name _ _ symbols) =
[t|'DEnum $(textToStrLit $ A.baseName name) '[] $(typesToList <$> mapM avChoiceToType (toList symbols))|]
where
avChoiceToType :: T.Text -> Q Type
avChoiceToType c = [t|'ChoiceDef $(textToStrLit c) '[]|]
schemaDecFromAvroType t = [t| 'DSimple $(schemaFromAvroType t) |]
schemaFromAvroType :: A.Type -> Q Type
schemaFromAvroType = \case
A.Null -> [t|'TPrimitive 'TNull|]
A.Boolean -> [t|'TPrimitive Bool|]
A.Int -> [t|'TPrimitive Int32|]
A.Long -> [t|'TPrimitive Int64|]
A.Float -> [t|'TPrimitive Float|]
A.Double -> [t|'TPrimitive Double|]
A.Bytes -> [t|'TPrimitive B.ByteString|]
A.String -> [t|'TPrimitive T.Text|]
A.Array item -> [t|'TList $(schemaFromAvroType item)|]
A.Map values -> [t|'TMap T.Text $(schemaFromAvroType values)|]
A.NamedType typeName ->
[t|'TSchematic $(textToStrLit (A.baseName typeName))|]
A.Enum {} -> fail "should never happen, please, file an issue"
A.Record {} -> fail "should never happen, please, file an issue"
A.Union options ->
case toList options of
[A.Null, x] -> toOption x
[x, A.Null] -> toOption x
_ -> [t|'TUnion $(typesToList <$> mapM schemaFromAvroType (toList options))|]
where toOption x = [t|'TOption $(schemaFromAvroType x)|]
A.Fixed {} -> fail "fixed integers are not currently supported"
flattenAvroDecls :: [A.Type] -> [A.Type]
flattenAvroDecls = concatMap (uncurry (:) . flattenDecl)
where
flattenDecl :: A.Type -> (A.Type, [A.Type])
flattenDecl (A.Record name a d o fields) =
let (flds, tts) = unzip (flattenAvroField <$> fields)
in (A.Record name a d o flds, concat tts)
flattenDecl (A.Union _) = error "should never happen, please, file an issue"
flattenDecl t = (t, [])
flattenAvroType :: A.Type -> (A.Type, [A.Type])
flattenAvroType (A.Record name a d o fields) =
let (flds, tts) = unzip (flattenAvroField <$> fields)
in (A.NamedType name, A.Record name a d o flds : concat tts)
flattenAvroType (A.Union (toList -> ts)) =
let (us, tts) = unzip (map flattenAvroType ts)
in (A.Union $ fromList us, concat tts)
flattenAvroType e@A.Enum {A.name} = (A.NamedType name, [e])
flattenAvroType t = (t, [])
flattenAvroField :: A.Field -> (A.Field, [A.Type])
flattenAvroField f =
let (t, decs) = flattenAvroType (A.fldType f)
in (f {A.fldType = t}, decs)
typesToList :: [Type] -> Type
typesToList = foldr (\y ys -> AppT (AppT PromotedConsT y) ys) PromotedNilT
textToStrLit :: T.Text -> Q Type
textToStrLit s = return $ LitT $ StrTyLit $ T.unpack s

View File

@ -1,13 +1,15 @@
{-# language OverloadedStrings, TypeApplications,
NamedFieldPuns #-}
NamedFieldPuns, DataKinds,
StandaloneDeriving, DerivingVia #-}
{-# options_ghc -fno-warn-orphans #-}
module Main where
import Data.Avro
import qualified Data.ByteString.Lazy as BS
import System.Environment
import Mu.Schema ()
import Mu.Schema.Adapter.Avro ()
import Mu.Schema (WithSchema(..))
import Mu.Adapter.Avro ()
import Mu.Schema.Examples
exampleAddress :: Address
@ -17,6 +19,10 @@ examplePerson1, examplePerson2 :: Person
examplePerson1 = Person "Haskellio" "Gómez" (Just 30) (Just Male) exampleAddress
examplePerson2 = Person "Cuarenta" "Siete" Nothing Nothing exampleAddress
deriving via (WithSchema ExampleSchema "person" Person) instance HasAvroSchema Person
deriving via (WithSchema ExampleSchema "person" Person) instance FromAvro Person
deriving via (WithSchema ExampleSchema "person" Person) instance ToAvro Person
main :: IO ()
main = do -- Obtain the filenames
[genFile, conFile] <- getArgs

View File

@ -0,0 +1,50 @@
cabal-version: >=1.10
name: mu-protobuf
version: 0.1.0.0
synopsis: Protocol Buffers serialization and gRPC schema import for Mu microservices
-- description:
-- bug-reports:
license: Apache-2.0
license-file: LICENSE
author: Alejandro Serrano
maintainer: alejandro.serrano@47deg.com
-- copyright:
category: Network
build-type: Simple
library
exposed-modules: Mu.Adapter.ProtoBuf
, Mu.Adapter.ProtoBuf.Via
, Mu.Adapter.ProtoBuf.Example
, Mu.Quasi.ProtoBuf
, Mu.Quasi.GRpc
-- other-modules:
-- other-extensions:
build-depends: base >=4.12 && <5
, mu-schema
, mu-rpc
, text
, sop-core
, proto3-wire
, bytestring
, template-haskell >= 2.12
, language-protobuf
, compendium-client
, http-client
, servant-client-core
, http2-grpc-proto3-wire
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall
-fprint-potential-instances
executable test-protobuf
main-is: ProtoBuf.hs
build-depends: base >=4.12 && <5
, mu-schema
, mu-protobuf
, bytestring
, proto3-wire
hs-source-dirs: test
default-language: Haskell2010
ghc-options: -Wall -fprint-explicit-foralls

View File

@ -7,7 +7,7 @@
OverloadedStrings, ConstraintKinds,
AllowAmbiguousTypes #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Mu.Schema.Adapter.ProtoBuf (
module Mu.Adapter.ProtoBuf (
-- * Custom annotations
ProtoBufId
, ProtoBufOneOfIds
@ -35,16 +35,12 @@ import Proto3.Wire
import qualified Proto3.Wire.Encode as PBEnc
import qualified Proto3.Wire.Decode as PBDec
import Mu.Schema.Annotations
import Mu.Schema.Definition
import Mu.Schema.Interpretation
import Mu.Schema.Class
import qualified Mu.Schema.Registry as R
-- ANNOTATION FOR CONVERSION
data ProtoBufId (n :: Nat)
data ProtoBufOneOfIds (ns :: [Nat])
type family FindProtoBufId (f :: fn) (xs :: [Type]) :: Nat where
FindProtoBufId f '[]
= TypeError ('Text "protocol buffers id not available for field " ':<>: 'ShowType f)
@ -64,12 +60,12 @@ instance ProtoBridgeTerm sch (sch :/: sty) => IsProtoSchema sch sty
type HasProtoSchema sch sty a = (HasSchema sch sty a, IsProtoSchema sch sty)
toProtoViaSchema :: forall sch a sty.
toProtoViaSchema :: forall t f (sch :: Schema t f) a sty.
(HasProtoSchema sch sty a)
=> a -> PBEnc.MessageBuilder
toProtoViaSchema = termToProto . toSchema' @sch
fromProtoViaSchema :: forall sch a sty.
fromProtoViaSchema :: forall t f (sch :: Schema t f) a sty.
(HasProtoSchema sch sty a)
=> PBDec.Parser PBDec.RawMessage a
fromProtoViaSchema = fromSchema' @sch <$> protoToTerm
@ -77,7 +73,7 @@ fromProtoViaSchema = fromSchema' @sch <$> protoToTerm
parseProtoViaSchema :: forall sch a sty.
(HasProtoSchema sch sty a)
=> BS.ByteString -> Either PBDec.ParseError a
parseProtoViaSchema = PBDec.parse (fromProtoViaSchema @sch)
parseProtoViaSchema = PBDec.parse (fromProtoViaSchema @_ @_ @sch)
-- CONVERSION USING REGISTRY
@ -100,7 +96,7 @@ instance FromProtoBufRegistry '[] t where
fromProtoBufRegistry' _ = PBDec.Parser (\_ -> Left (PBDec.WireTypeError "no schema found in registry"))
instance (HasProtoSchema s sty t, FromProtoBufRegistry ms t)
=> FromProtoBufRegistry ( (n ':-> s) ': ms) t where
fromProtoBufRegistry' _ = fromProtoViaSchema @s <|> fromProtoBufRegistry' (Proxy @ms)
fromProtoBufRegistry' _ = fromProtoViaSchema @_ @_ @s <|> fromProtoBufRegistry' (Proxy @ms)
-- =======================================

View File

@ -0,0 +1,17 @@
{-# language QuasiQuotes, DataKinds #-}
module Mu.Adapter.ProtoBuf.Example where
import Mu.Quasi.ProtoBuf
type ExampleProtoBufSchema = [protobuf|
enum gender {
male = 1;
female = 2;
nonbinary = 3;
}
message person {
repeated string names = 1;
int age = 2;
gender gender = 3;
}
|]

View File

@ -4,7 +4,7 @@
FlexibleInstances, FlexibleContexts,
UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-simplifiable-class-constraints -fno-warn-orphans #-}
module Mu.GRpc.Shared where
module Mu.Adapter.ProtoBuf.Via where
import Network.GRPC.HTTP2.Proto3Wire
import qualified Proto3.Wire.Encode as PBEnc
@ -12,8 +12,7 @@ import qualified Proto3.Wire.Decode as PBDec
import Mu.Rpc
import Mu.Schema
import Mu.Schema.Adapter.ProtoBuf
import Mu.Adapter.ProtoBuf
newtype ViaProtoBufTypeRef (ref :: TypeRef) t
= ViaProtoBufTypeRef { unViaProtoBufTypeRef :: t }
@ -33,11 +32,11 @@ class ProtoBufTypeRef (ref :: TypeRef) t where
instance (HasProtoSchema sch sty t)
=> ProtoBufTypeRef ('FromSchema sch sty) t where
fromProtoBufTypeRef _ = fromProtoViaSchema @sch
toProtoBufTypeRef _ = toProtoViaSchema @sch
fromProtoBufTypeRef _ = fromProtoViaSchema @_ @_ @sch
toProtoBufTypeRef _ = toProtoViaSchema @_ @_ @sch
instance ( FromProtoBufRegistry r t
, HasProtoSchema (MappingRight r last) sty t)
=> ProtoBufTypeRef ('FromRegistry r t last) t where
fromProtoBufTypeRef _ = fromProtoBufWithRegistry @r
toProtoBufTypeRef _ = toProtoViaSchema @(MappingRight r last)
toProtoBufTypeRef _ = toProtoViaSchema @_ @_ @(MappingRight r last)

View File

@ -1,6 +1,6 @@
{-# language TemplateHaskell, DataKinds, OverloadedStrings #-}
-- | Read a @.proto@ file as a 'Service'
module Mu.Rpc.Quasi (
module Mu.Quasi.GRpc (
grpc
, compendium
) where
@ -13,7 +13,7 @@ import Language.ProtocolBuffers.Parser
import Network.HTTP.Client
import Servant.Client.Core.BaseUrl
import Mu.Schema.Quasi
import Mu.Quasi.ProtoBuf
import Mu.Rpc
import Compendium.Client

View File

@ -4,42 +4,25 @@
{-# language TemplateHaskell #-}
{-# language ViewPatterns #-}
module Mu.Schema.Quasi (
-- * Quasi-quoters for @.avsc@ files
avro
, avroFile
module Mu.Quasi.ProtoBuf (
-- * Quasi-quoters for @.proto@ files
, protobuf
protobuf
, protobufFile
-- * Only for internal use
, schemaFromAvroType
, schemaFromProtoBuf
) where
import Data.Aeson (decode)
import qualified Data.Avro.Schema as A
import qualified Data.ByteString as B
import Data.ByteString.Lazy.Char8 (pack)
import Data.Int
import qualified Data.Text as T
import Data.Vector (fromList, toList)
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Language.ProtocolBuffers.Parser
import qualified Language.ProtocolBuffers.Types as P
import Mu.Schema.Adapter.ProtoBuf
import Mu.Adapter.ProtoBuf
import Mu.Schema.Definition
-- | Imports an avro definition written in-line as a 'Schema'.
avro :: QuasiQuoter
avro =
QuasiQuoter
(const $ fail "cannot use as expression")
(const $ fail "cannot use as pattern")
schemaFromAvroString
(const $ fail "cannot use as declaration")
-- | Imports a protocol buffer definition written
-- in-line as a 'Schema'.
protobuf :: QuasiQuoter
@ -50,92 +33,17 @@ protobuf =
schemaFromProtoBufString
(const $ fail "cannot use as declaration")
-- | Imports an avro definition from a file as a 'Schema'.
avroFile :: QuasiQuoter
avroFile = quoteFile avro
-- | Imports a protocol buffer definition from a file
-- as a 'Schema'.
protobufFile :: QuasiQuoter
protobufFile = quoteFile protobuf
schemaFromAvroString :: String -> Q Type
schemaFromAvroString s =
case decode (pack s) of
Nothing -> fail "could not parse avro spec!"
Just (A.Union us) -> schemaFromAvro (toList us)
Just t -> schemaFromAvro [t]
where schemaFromAvro = (typesToList <$>) . mapM schemaDecFromAvroType . flattenAvroDecls
schemaDecFromAvroType :: A.Type -> Q Type
schemaDecFromAvroType (A.Record name _ _ _ fields) =
[t|'DRecord $(textToStrLit $ A.baseName name) '[] $(typesToList <$> mapM avroFieldToType fields)|]
where
avroFieldToType :: A.Field -> Q Type
avroFieldToType field =
[t|'FieldDef $(textToStrLit $ A.fldName field) '[] $(schemaFromAvroType $ A.fldType field)|]
schemaDecFromAvroType (A.Enum name _ _ symbols) =
[t|'DEnum $(textToStrLit $ A.baseName name) '[] $(typesToList <$> mapM avChoiceToType (toList symbols))|]
where
avChoiceToType :: T.Text -> Q Type
avChoiceToType c = [t|'ChoiceDef $(textToStrLit c) '[]|]
schemaDecFromAvroType t = [t| 'DSimple $(schemaFromAvroType t) |]
schemaFromAvroType :: A.Type -> Q Type
schemaFromAvroType = \case
A.Null -> [t|'TPrimitive 'TNull|]
A.Boolean -> [t|'TPrimitive Bool|]
A.Int -> [t|'TPrimitive Int32|]
A.Long -> [t|'TPrimitive Int64|]
A.Float -> [t|'TPrimitive Float|]
A.Double -> [t|'TPrimitive Double|]
A.Bytes -> [t|'TPrimitive B.ByteString|]
A.String -> [t|'TPrimitive T.Text|]
A.Array item -> [t|'TList $(schemaFromAvroType item)|]
A.Map values -> [t|'TMap T.Text $(schemaFromAvroType values)|]
A.NamedType typeName ->
[t|'TSchematic $(textToStrLit (A.baseName typeName))|]
A.Enum {} -> fail "should never happen, please, file an issue"
A.Record {} -> fail "should never happen, please, file an issue"
A.Union options ->
case toList options of
[A.Null, x] -> toOption x
[x, A.Null] -> toOption x
_ -> [t|'TUnion $(typesToList <$> mapM schemaFromAvroType (toList options))|]
where toOption x = [t|'TOption $(schemaFromAvroType x)|]
A.Fixed {} -> fail "fixed integers are not currently supported"
schemaFromProtoBufString :: String -> Q Type
schemaFromProtoBufString ts =
case parseProtoBuf (T.pack ts) of
Left e -> fail ("could not parse protocol buffers spec: " ++ show e)
Right p -> schemaFromProtoBuf p
flattenAvroDecls :: [A.Type] -> [A.Type]
flattenAvroDecls = concatMap (uncurry (:) . flattenDecl)
where
flattenDecl :: A.Type -> (A.Type, [A.Type])
flattenDecl (A.Record name a d o fields) =
let (flds, tts) = unzip (flattenAvroField <$> fields)
in (A.Record name a d o flds, concat tts)
flattenDecl (A.Union _) = error "should never happen, please, file an issue"
flattenDecl t = (t, [])
flattenAvroType :: A.Type -> (A.Type, [A.Type])
flattenAvroType (A.Record name a d o fields) =
let (flds, tts) = unzip (flattenAvroField <$> fields)
in (A.NamedType name, A.Record name a d o flds : concat tts)
flattenAvroType (A.Union (toList -> ts)) =
let (us, tts) = unzip (map flattenAvroType ts)
in (A.Union $ fromList us, concat tts)
flattenAvroType e@A.Enum {A.name} = (A.NamedType name, [e])
flattenAvroType t = (t, [])
flattenAvroField :: A.Field -> (A.Field, [A.Type])
flattenAvroField f =
let (t, decs) = flattenAvroType (A.fldType f)
in (f {A.fldType = t}, decs)
schemaFromProtoBuf :: P.ProtoBuf -> Q Type
schemaFromProtoBuf P.ProtoBuf {P.types = tys} =
let decls = flattenDecls tys

View File

@ -1,5 +1,4 @@
{-# language OverloadedStrings, TypeApplications,
NamedFieldPuns #-}
{-# language OverloadedStrings, TypeApplications, ScopedTypeVariables #-}
module Main where
import qualified Data.ByteString as BS
@ -9,7 +8,7 @@ import qualified Proto3.Wire.Encode as PBEnc
import System.Environment
import Mu.Schema ()
import Mu.Schema.Adapter.ProtoBuf ()
import Mu.Adapter.ProtoBuf
import Mu.Schema.Examples
exampleAddress :: Address
@ -25,10 +24,10 @@ main = do -- Obtain the filenames
-- Read the file produced by Python
putStrLn "haskell/consume"
cbs <- BS.readFile conFile
let Right people = PBDec.parse protoBufToPerson cbs
print people
let Right people = PBDec.parse (fromProtoViaSchema @_ @_ @ExampleSchema) cbs
print (people :: Person)
-- Encode a couple of values
putStrLn "haskell/generate"
print examplePerson1
let gbs = PBEnc.toLazyByteString (personToProtoBuf examplePerson1)
let gbs = PBEnc.toLazyByteString (toProtoViaSchema @_ @_ @ExampleSchema examplePerson1)
LBS.writeFile genFile gbs

View File

@ -19,16 +19,13 @@ extra-source-files: README.md, CHANGELOG.md
library
exposed-modules: Mu.Rpc,
Mu.Rpc.Quasi,
Mu.Server,
Mu.Rpc.Examples
-- other-modules:
-- other-extensions:
build-depends: base >=4.12 && <5, sop-core,
mu-schema, conduit, text,
template-haskell, language-protobuf,
compendium-client,
http-client, servant-client-core
template-haskell
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall -fprint-potential-instances

View File

@ -14,7 +14,6 @@ import GHC.Generics
import Mu.Schema
import Mu.Rpc
import Mu.Server
import Mu.Schema.Adapter.ProtoBuf
-- Defines the service from gRPC Quickstart
-- https://grpc.io/docs/quickstart/python/

202
core/schema/LICENSE Normal file
View File

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -25,53 +25,24 @@ library
, Mu.Schema.Interpretation.Anonymous
, Mu.Schema.Class
, Mu.Schema.Registry
, Mu.Schema.Adapter.Avro
, Mu.Schema.Adapter.ProtoBuf
, Mu.Schema.Adapter.Json
, Mu.Schema.Quasi
, Mu.Schema.Conversion.TypesToSchema
, Mu.Schema.Conversion.SchemaToTypes
, Mu.Schema.Examples
, Mu.Schema.AvroExample
, Mu.Schema.Annotations
, Mu.Adapter.Json
-- other-modules:
-- other-extensions:
build-depends: base >=4.12 && <5
, sop-core
, containers
, unordered-containers
, vector
, bytestring
, vector
, text
, avro
, tagged
, proto3-wire
, aeson
, template-haskell >= 2.12
, th-abstraction
, language-protobuf
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall
-fprint-potential-instances
executable test-avro
main-is: Avro.hs
build-depends: base >=4.12 && <5
, sop-core
, mu-schema
, avro
, bytestring
hs-source-dirs: test
default-language: Haskell2010
ghc-options: -Wall
executable test-protobuf
main-is: ProtoBuf.hs
build-depends: base >=4.12 && <5
, sop-core
, mu-schema
, proto3-wire
, bytestring
hs-source-dirs: test
default-language: Haskell2010
ghc-options: -Wall
-fprint-potential-instances

View File

@ -5,7 +5,7 @@
TypeApplications,
UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Mu.Schema.Adapter.Json where
module Mu.Adapter.Json where
import Control.Applicative ((<|>))
import Data.Aeson

View File

@ -1,10 +1,8 @@
{-# language DataKinds #-}
-- | Schemas for Mu microservices
module Mu.Schema (
-- * Quasi-quoters for schemas
protobuf, protobufFile
-- * Schema definition
, Schema, Schema'
Schema, Schema'
, Annotation, KnownName(..)
, TypeDef, TypeDefB(..)
, ChoiceDef(..)
@ -19,9 +17,11 @@ module Mu.Schema (
, WithSchema(..), HasSchema(..), toSchema', fromSchema'
-- ** Mappings between fields
, Mapping(..), Mappings, MappingRight, MappingLeft
-- ** Field annotations
, ProtoBufId, ProtoBufOneOfIds
) where
import Mu.Schema.Annotations
import Mu.Schema.Definition
import Mu.Schema.Interpretation
import Mu.Schema.Class
import Mu.Schema.Quasi
import Mu.Schema.Class

View File

@ -0,0 +1,9 @@
{-# language DataKinds, KindSignatures #-}
module Mu.Schema.Annotations where
import GHC.TypeLits
-- ANNOTATION FOR CONVERSION
data ProtoBufId (n :: Nat)
data ProtoBufOneOfIds (ns :: [Nat])

View File

@ -8,19 +8,12 @@
module Mu.Schema.Examples where
import qualified Data.Aeson as J
import qualified Data.Avro as A
import qualified Data.Text as T
import GHC.Generics
import Mu.Schema
import Mu.Schema.Adapter.Avro ()
import Mu.Schema.Adapter.ProtoBuf
import Mu.Schema.Adapter.Json ()
import Mu.Schema.Conversion.SchemaToTypes
import qualified Proto3.Wire.Encode as PBEnc
import qualified Proto3.Wire.Decode as PBDec
import Mu.Adapter.Json ()
data Person
= Person { firstName :: T.Text
@ -30,26 +23,20 @@ data Person
, address :: Address }
deriving (Eq, Show, Generic)
deriving (HasSchema ExampleSchema "person")
deriving (A.HasAvroSchema, A.FromAvro, A.ToAvro, J.ToJSON, J.FromJSON)
deriving (J.ToJSON, J.FromJSON)
via (WithSchema ExampleSchema "person" Person)
personToProtoBuf :: Person -> PBEnc.MessageBuilder
personToProtoBuf = toProtoViaSchema @ExampleSchema
protoBufToPerson :: PBDec.Parser PBDec.RawMessage Person
protoBufToPerson = fromProtoViaSchema @ExampleSchema
data Address
= Address { postcode :: T.Text
, country :: T.Text }
deriving (Eq, Show, Generic)
deriving (HasSchema ExampleSchema "address")
deriving (A.HasAvroSchema, A.FromAvro, A.ToAvro, J.ToJSON, J.FromJSON)
deriving (J.ToJSON, J.FromJSON)
via (WithSchema ExampleSchema "address" Address)
data Gender = Male | Female | NonBinary
deriving (Eq, Show, Generic)
deriving (A.HasAvroSchema, A.FromAvro, A.ToAvro, J.ToJSON, J.FromJSON)
deriving (J.ToJSON, J.FromJSON)
via (WithSchema ExampleSchema "gender" Gender)
-- Schema for these data types
@ -57,7 +44,7 @@ type ExampleSchema
= '[ 'DEnum "gender" '[]
'[ 'ChoiceDef "male" '[ ProtoBufId 1 ]
, 'ChoiceDef "female" '[ ProtoBufId 2 ]
, 'ChoiceDef "nb" '[ ProtoBufId 0 ] ]
, 'ChoiceDef "nb" '[ ProtoBufId 3 ] ]
, 'DRecord "address" '[]
'[ 'FieldDef "postcode" '[ ProtoBufId 1 ] ('TPrimitive T.Text)
, 'FieldDef "country" '[ ProtoBufId 2 ] ('TPrimitive T.Text) ]
@ -90,7 +77,7 @@ type ExampleSchema2
= '[ 'DEnum "gender" '[]
'[ 'ChoiceDef "Male" '[ ProtoBufId 1 ]
, 'ChoiceDef "Female" '[ ProtoBufId 2 ]
, 'ChoiceDef "NonBinary" '[ ProtoBufId 0 ] ]
, 'ChoiceDef "NonBinary" '[ ProtoBufId 3 ] ]
, 'DRecord "address" '[]
'[ 'FieldDef "postcode" '[ ProtoBufId 1 ] ('TPrimitive T.Text)
, 'FieldDef "country" '[ ProtoBufId 2 ] ('TPrimitive T.Text) ]
@ -103,17 +90,4 @@ type ExampleSchema2
]
type ExampleRegistry
= '[ 2 ':-> ExampleSchema2, 1 ':-> ExampleSchema]
type ExampleSchema3 = [protobuf|
enum gender {
male = 1;
female = 2;
nonbinary = 3;
}
message person {
repeated string names = 1;
int age = 2;
gender gender = 3;
}
|]
= '[ 2 ':-> ExampleSchema2, 1 ':-> ExampleSchema]

View File

@ -19,7 +19,7 @@ build-type: Simple
library
exposed-modules: Definition
build-depends: base >=4.12 && <5, text,
mu-schema, mu-rpc, mu-grpc,
mu-schema, mu-rpc, mu-protobuf,
stm, stm-containers,
conduit, stm-conduit,
deferred-folds
@ -31,7 +31,8 @@ executable health-server
main-is: Server.hs
other-modules: Definition
build-depends: base >=4.12 && <5, text,
mu-schema, mu-rpc, mu-grpc,
mu-schema, mu-rpc, mu-protobuf,
mu-grpc-server,
stm, stm-containers,
conduit, stm-conduit,
deferred-folds
@ -43,8 +44,8 @@ executable health-client-tyapps
main-is: ClientTyApps.hs
other-modules: Definition
build-depends: base >=4.12 && <5, text,
mu-schema, mu-rpc, mu-grpc,
conduit
mu-schema, mu-rpc, mu-protobuf,
mu-grpc-client, conduit
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall
@ -53,8 +54,8 @@ executable health-client-record
main-is: ClientRecord.hs
other-modules: Definition
build-depends: base >=4.12 && <5, text,
mu-schema, mu-rpc, mu-grpc,
conduit
mu-schema, mu-rpc, mu-protobuf,
mu-grpc-client, conduit
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall

View File

@ -10,7 +10,7 @@ import qualified Data.Text as T
import GHC.Generics (Generic)
import System.Environment
import Mu.Client.GRpc.Record
import Mu.GRpc.Client.Record
import Definition

View File

@ -9,7 +9,7 @@ import qualified Data.Conduit.Combinators as C
import qualified Data.Text as T
import System.Environment
import Mu.Client.GRpc.TyApps
import Mu.GRpc.Client.TyApps
import Definition

View File

@ -10,7 +10,7 @@ import GHC.Generics
import Data.Text as T
import Mu.Schema
import Mu.Rpc.Quasi
import Mu.Quasi.GRpc
$(grpc "HealthCheckSchema" id "healthcheck.proto")

View File

@ -11,7 +11,7 @@ import DeferredFolds.UnfoldlM
import qualified StmContainers.Map as M
import Mu.Server
import Mu.Server.GRpc
import Mu.GRpc.Server
import Definition

View File

@ -19,7 +19,7 @@ build-type: Simple
library
exposed-modules: Definition
build-depends: base >=4.12 && <5, text,
mu-schema, mu-rpc, mu-grpc,
mu-schema, mu-rpc, mu-protobuf,
hashable
hs-source-dirs: src
default-language: Haskell2010
@ -29,7 +29,8 @@ executable route-guide-server
main-is: Server.hs
other-modules: Definition
build-depends: base >=4.12 && <5, text,
mu-schema, mu-rpc, mu-grpc,
mu-schema, mu-rpc, mu-protobuf,
mu-grpc-server,
stm, stm-chans, hashable,
conduit, AC-Angle, time, async
hs-source-dirs: src

View File

@ -11,9 +11,7 @@ import Data.Int
import Data.Text as T
import Mu.Schema
import Mu.Schema.Adapter.ProtoBuf
import Mu.Rpc
import Mu.Rpc.Quasi
import Mu.Quasi.GRpc
$(grpc "RouteGuideSchema" id "routeguide.proto")

View File

@ -17,7 +17,7 @@ import Data.Maybe
import Data.Time.Clock
import Mu.Server
import Mu.Server.GRpc
import Mu.GRpc.Server
import Definition

202
grpc/client/LICENSE Normal file
View File

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -0,0 +1,35 @@
cabal-version: >=1.10
-- Initial package description 'mu-haskell.cabal' generated by 'cabal
-- init'. For further documentation, see
-- http://haskell.org/cabal/users-guide/
name: mu-grpc-client
version: 0.1.0.0
synopsis: gRPC clients from Mu definitions
-- description:
-- bug-reports:
license: Apache-2.0
license-file: LICENSE
author: Alejandro Serrano
maintainer: alejandro.serrano@47deg.com
-- copyright:
category: Network
build-type: Simple
extra-source-files: CHANGELOG.md
library
exposed-modules: Mu.GRpc.Client.TyApps,
Mu.GRpc.Client.Record,
Mu.GRpc.Client.Examples
other-modules: Mu.GRpc.Client.Internal
-- other-extensions:
build-depends: base >=4.12 && <5, sop-core,
bytestring, async, text,
mu-schema, mu-rpc, mu-protobuf,
http2, http2-client, http2-client-grpc,
http2-grpc-proto3-wire,
conduit, stm, stm-chans, stm-conduit,
template-haskell >= 2.12, th-abstraction
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall -fprint-potential-instances

View File

@ -1,5 +1,5 @@
{-# language DataKinds, TypeApplications #-}
module Mu.Client.GRpc.Examples where
module Mu.GRpc.Client.Examples where
import Data.Conduit
import Data.Conduit.Combinators as C
@ -7,7 +7,7 @@ import Data.Conduit.List (consume)
import qualified Data.Text as T
import Network.HTTP2.Client (HostName, PortNumber)
import Mu.Client.GRpc.TyApps
import Mu.GRpc.Client.TyApps
import Mu.Rpc.Examples
sayHello' :: HostName -> PortNumber -> T.Text -> IO (GRpcReply T.Text)

View File

@ -6,7 +6,7 @@
AllowAmbiguousTypes,
TupleSections, UndecidableInstances #-}
-- | Client for gRPC services defined using Mu 'Service'
module Mu.Client.GRpc.Internal where
module Mu.GRpc.Client.Internal where
import Control.Monad.IO.Class
import Control.Concurrent.Async
@ -27,8 +27,7 @@ import Network.GRPC.Client.Helpers
import Mu.Rpc
import Mu.Schema
import Mu.GRpc.Shared
import Mu.Adapter.ProtoBuf.Via
setupGrpcClient' :: GrpcClientConfig -> IO (Either ClientError GrpcClient)
setupGrpcClient' = runExceptT . setupGrpcClient

View File

@ -6,7 +6,7 @@
TemplateHaskell #-}
-- | Client for gRPC services defined using Mu 'Service'
-- using plain Haskell records of functions
module Mu.Client.GRpc.Record (
module Mu.GRpc.Client.Record (
-- * Initialization of the gRPC client
GrpcClient
, GrpcClientConfig
@ -32,7 +32,7 @@ import Language.Haskell.TH.Datatype
import Network.GRPC.Client (CompressMode(..))
import Network.GRPC.Client.Helpers
import Mu.Client.GRpc.Internal
import Mu.GRpc.Client.Internal
import Mu.Rpc
-- | Fills in a Haskell record of functions with the corresponding

View File

@ -4,7 +4,7 @@
TypeOperators, AllowAmbiguousTypes #-}
-- | Client for gRPC services defined using Mu 'Service'
-- using 'TypeApplications'
module Mu.Client.GRpc.TyApps (
module Mu.GRpc.Client.TyApps (
-- * Initialization of the gRPC client
GrpcClient
, GrpcClientConfig
@ -22,7 +22,7 @@ import Network.GRPC.Client.Helpers
import Mu.Rpc
import Mu.Schema
import Mu.Client.GRpc.Internal
import Mu.GRpc.Client.Internal
-- | Call a method from a Mu definition.
-- This method is thought to be used with @TypeApplications@:

View File

@ -1,55 +0,0 @@
cabal-version: >=1.10
-- Initial package description 'mu-haskell.cabal' generated by 'cabal
-- init'. For further documentation, see
-- http://haskell.org/cabal/users-guide/
name: mu-grpc
version: 0.1.0.0
synopsis: gRPC servers and clients for Mu definitions
-- description:
-- bug-reports:
license: Apache-2.0
license-file: LICENSE
author: Alejandro Serrano
maintainer: alejandro.serrano@47deg.com
-- copyright:
category: Network
build-type: Simple
extra-source-files: CHANGELOG.md
library
exposed-modules: Mu.Server.GRpc,
Mu.Client.GRpc.Internal,
Mu.Client.GRpc.TyApps,
Mu.Client.GRpc.Record,
Mu.Client.GRpc.Examples
other-modules: Mu.GRpc.Shared
-- other-extensions:
build-depends: base >=4.12 && <5, sop-core,
mu-schema, mu-rpc, warp-grpc,
conduit, bytestring, text,
wai, warp, warp-tls,
async, stm, stm-conduit, stm-chans,
http2, http2-client,
http2-grpc-types, http2-client-grpc,
proto3-wire, http2-grpc-proto3-wire,
template-haskell, th-abstraction
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall -fprint-potential-instances
executable grpc-example-server
main-is: ExampleServer.hs
build-depends: base >=4.12 && <5, sop-core,
mu-schema, mu-rpc, warp-grpc,
conduit, bytestring, text,
wai, warp, warp-tls,
async, stm, stm-conduit, stm-chans,
http2, http2-client,
http2-grpc-types, http2-client-grpc,
proto3-wire, http2-grpc-proto3-wire,
template-haskell, th-abstraction
other-modules: Mu.GRpc.Shared, Mu.Server.GRpc
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall

5
grpc/server/CHANGELOG.md Normal file
View File

@ -0,0 +1,5 @@
# Revision history for mu-haskell
## 0.1.0.0 -- YYYY-mm-dd
* First version. Released on an unsuspecting world.

202
grpc/server/LICENSE Normal file
View File

@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

2
grpc/server/Setup.hs Normal file
View File

@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain

View File

@ -0,0 +1,44 @@
cabal-version: >=1.10
-- Initial package description 'mu-haskell.cabal' generated by 'cabal
-- init'. For further documentation, see
-- http://haskell.org/cabal/users-guide/
name: mu-grpc-server
version: 0.1.0.0
synopsis: gRPC servers for Mu definitions
-- description:
-- bug-reports:
license: Apache-2.0
license-file: LICENSE
author: Alejandro Serrano
maintainer: alejandro.serrano@47deg.com
-- copyright:
category: Network
build-type: Simple
extra-source-files: CHANGELOG.md
library
exposed-modules: Mu.GRpc.Server
-- other-extensions:
build-depends: base >=4.12 && <5, sop-core,
bytestring, async,
mu-schema, mu-rpc, mu-protobuf,
warp, warp-grpc, wai, warp-tls,
http2-grpc-types, http2-grpc-proto3-wire,
conduit, stm, stm-conduit
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall -fprint-potential-instances
executable grpc-example-server
main-is: ExampleServer.hs
other-modules: Mu.GRpc.Server
build-depends: base >=4.12 && <5, sop-core,
bytestring, async,
mu-schema, mu-rpc, mu-protobuf,
warp, warp-grpc, wai, warp-tls,
http2-grpc-types, http2-grpc-proto3-wire,
conduit, stm, stm-conduit
hs-source-dirs: src
default-language: Haskell2010
ghc-options: -Wall

View File

@ -1,7 +1,7 @@
{-# language OverloadedStrings #-}
module Main where
import Mu.Server.GRpc
import Mu.GRpc.Server
import Mu.Rpc.Examples
main :: IO ()

View File

@ -5,7 +5,7 @@
TypeApplications, TypeOperators,
ScopedTypeVariables #-}
-- | Execute a Mu 'Server' using gRPC as transport layer
module Mu.Server.GRpc (
module Mu.GRpc.Server (
-- * Run a 'Server' directly
runGRpcApp
, runGRpcAppSettings, Settings
@ -36,8 +36,7 @@ import Network.Wai.Handler.WarpTLS (TLSSettings, runTLS)
import Mu.Rpc
import Mu.Server
import Mu.Schema
import Mu.GRpc.Shared
import Mu.Adapter.ProtoBuf.Via
-- | Run a Mu 'Server' on the given port.
runGRpcApp

View File

@ -1,46 +0,0 @@
// Copyright 2015 gRPC authors.
// Modified 2019, by Alejandro Serrano.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
rpc SayHi (HiRequest) returns (stream HelloReply) {}
rpc SayManyHellos (stream HelloRequest) returns (stream HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The request message containing the amount of greetings.
message HiRequest {
int32 number = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}

View File

@ -1,10 +1,13 @@
resolver: nightly-2019-11-04
resolver: nightly-2019-11-17
allow-newer: true
packages:
- schema
- rpc
- grpc
- core/schema
- core/rpc
- adapter/avro
- adapter/protobuf
- grpc/client
- grpc/server
- examples/health-check
- examples/route-guide
- compendium-client

View File

@ -1,9 +1,12 @@
resolver: lts-14.13
resolver: lts-14.14
packages:
- schema
- rpc
- grpc
- core/schema
- core/rpc
- adapter/avro
- adapter/protobuf
- grpc/client
- grpc/server
- examples/health-check
- examples/route-guide
- compendium-client

View File

@ -6,19 +6,19 @@
# follow https://github.com/protocolbuffers/protobuf/tree/master/python
echo "BUILDING"
stack build
stack build mu-avro mu-protobuf
mkdir -p dist
echo "\nAVRO\n====\n"
echo "python/generate"
python3 schema/test/avro/generate.py schema/test/avro/example.avsc dist/avro-python.avro
python3 adapter/avro/test/avro/generate.py adapter/avro/test/avro/example.avsc dist/avro-python.avro
stack exec test-avro dist/avro-haskell.avro dist/avro-python.avro
echo "ptyhon/consume"
python3 schema/test/avro/consume.py schema/test/avro/example.avsc dist/avro-haskell.avro
python3 adapter/avro/test/avro/consume.py adapter/avro/test/avro/example.avsc dist/avro-haskell.avro
echo "\nPROTOBUF\n========\n"
echo "python/generate"
python schema/test/protobuf/generate.py dist/protobuf-python.pbuf
python adapter/protobuf/test/protobuf/generate.py dist/protobuf-python.pbuf
stack exec test-protobuf dist/protobuf-haskell.pbuf dist/protobuf-python.pbuf
echo "python/consume"
python schema/test/protobuf/consume.py dist/protobuf-haskell.pbuf
python adapter/protobuf/test/protobuf/consume.py dist/protobuf-haskell.pbuf