Skip to content

Implement Client target #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,19 @@ compileTypeDeclaration src (ServiceDeclaration name (Service methods) _ _) = do
methodMetadata <- mapM compileMethodMetadata methods'
let methodMetadata' = commaNl methodMetadata
dummyMethods <- mapM compileMethod methods'
clientMethods <- mapM compileClientMethod methods'
let dummyMethods' = T.intercalate "\n\n" dummyMethods
withThirdPartyImports [ ("nirum.constructs", ["name_dict_type"])
, ("nirum.rpc", ["service_type"])
] $
return [qq|
clientMethods' = T.intercalate "\n\n" clientMethods
withStandardImport "urllib.request" $
withStandardImport "json" $
withThirdPartyImports [ ("nirum.constructs", ["name_dict_type"])
, ("nirum.deserialize", ["deserialize_meta"])
, ("nirum.serialize", ["serialize_meta"])
, ("nirum.rpc", [ "service_type"
, "client_type"
])
] $
return [qq|
class $className(service_type):

__nirum_service_methods__ = \{
Expand All @@ -517,6 +525,13 @@ class $className(service_type):
])

{dummyMethods'}


# FIXME client MUST be generated & saved on diffrent module
# where service isn't included.
class {className}_Client(client_type, $className):
{clientMethods'}
pass
|]
where
className :: T.Text
Expand Down Expand Up @@ -561,6 +576,30 @@ class $className(service_type):
paramNameMap :: [Parameter] -> T.Text
paramNameMap params = toIndentedCodes
toNamePair [pName | Parameter pName _ _ <- params] ",\n "
compileClientPayload :: Parameter -> CodeGen Code
compileClientPayload (Parameter pName _ _) = do
let pName' = toAttributeName' pName
return [qq|meta['_names']['{pName'}']: serialize_meta({pName'})|]
compileClientMethod :: Method -> CodeGen Code
compileClientMethod (Method mName params rtype _ _ _) = do
let clientMethodName' = toAttributeName' mName
params' <- mapM compileParameter $ toList params
rtypeExpr <- compileTypeExpression src rtype
payloadArguments <- mapM compileClientPayload $ toList params
return [qq|
def {clientMethodName'}(self, {commaNl params'}) -> $rtypeExpr:
meta = self.__nirum_service_methods__['{clientMethodName'}']
return deserialize_meta(
meta['_return'],
json.loads(
self.remote_call(
self.__nirum_method_names__['{clientMethodName'}'],
payload=\{{commaNl payloadArguments}\}
)
)
)
|]

compileTypeDeclaration _ (Import _ _) =
return "" -- Nothing to compile

Expand Down
4 changes: 4 additions & 0 deletions test/Nirum/Targets/PythonSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,10 @@ spec = parallel $ do
dirName = takeDirectory filePath'
createDirectoryIfMissing True dirName
TI.writeFile filePath' code'
{-- <- Remove '{' to print debug log
TI.putStrLn $ T.pack filePath'
TI.putStrLn code'
-- --}
testRunner (Just dir) defCode testCode
where
files :: M.Map FilePath (Either CompileError Code)
Expand Down