Skip to content

Commit 6805be1

Browse files
authored
Merge pull request #52 from admire93/client
Implement Client target
2 parents ae4ab18 + 6a027a0 commit 6805be1

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/Nirum/Targets/Python.hs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,11 +502,19 @@ compileTypeDeclaration src (ServiceDeclaration name (Service methods) _ _) = do
502502
methodMetadata <- mapM compileMethodMetadata methods'
503503
let methodMetadata' = commaNl methodMetadata
504504
dummyMethods <- mapM compileMethod methods'
505+
clientMethods <- mapM compileClientMethod methods'
505506
let dummyMethods' = T.intercalate "\n\n" dummyMethods
506-
withThirdPartyImports [ ("nirum.constructs", ["name_dict_type"])
507-
, ("nirum.rpc", ["service_type"])
508-
] $
509-
return [qq|
507+
clientMethods' = T.intercalate "\n\n" clientMethods
508+
withStandardImport "urllib.request" $
509+
withStandardImport "json" $
510+
withThirdPartyImports [ ("nirum.constructs", ["name_dict_type"])
511+
, ("nirum.deserialize", ["deserialize_meta"])
512+
, ("nirum.serialize", ["serialize_meta"])
513+
, ("nirum.rpc", [ "service_type"
514+
, "client_type"
515+
])
516+
] $
517+
return [qq|
510518
class $className(service_type):
511519

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

519527
{dummyMethods'}
528+
529+
530+
# FIXME client MUST be generated & saved on diffrent module
531+
# where service isn't included.
532+
class {className}_Client(client_type, $className):
533+
{clientMethods'}
534+
pass
520535
|]
521536
where
522537
className :: T.Text
@@ -561,6 +576,30 @@ class $className(service_type):
561576
paramNameMap :: [Parameter] -> T.Text
562577
paramNameMap params = toIndentedCodes
563578
toNamePair [pName | Parameter pName _ _ <- params] ",\n "
579+
compileClientPayload :: Parameter -> CodeGen Code
580+
compileClientPayload (Parameter pName _ _) = do
581+
let pName' = toAttributeName' pName
582+
return [qq|meta['_names']['{pName'}']: serialize_meta({pName'})|]
583+
compileClientMethod :: Method -> CodeGen Code
584+
compileClientMethod (Method mName params rtype _ _ _) = do
585+
let clientMethodName' = toAttributeName' mName
586+
params' <- mapM compileParameter $ toList params
587+
rtypeExpr <- compileTypeExpression src rtype
588+
payloadArguments <- mapM compileClientPayload $ toList params
589+
return [qq|
590+
def {clientMethodName'}(self, {commaNl params'}) -> $rtypeExpr:
591+
meta = self.__nirum_service_methods__['{clientMethodName'}']
592+
return deserialize_meta(
593+
meta['_return'],
594+
json.loads(
595+
self.remote_call(
596+
self.__nirum_method_names__['{clientMethodName'}'],
597+
payload=\{{commaNl payloadArguments}\}
598+
)
599+
)
600+
)
601+
|]
602+
564603
compileTypeDeclaration _ (Import _ _) =
565604
return "" -- Nothing to compile
566605

test/Nirum/Targets/PythonSpec.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ spec = parallel $ do
465465
dirName = takeDirectory filePath'
466466
createDirectoryIfMissing True dirName
467467
TI.writeFile filePath' code'
468+
{-- <- Remove '{' to print debug log
469+
TI.putStrLn $ T.pack filePath'
470+
TI.putStrLn code'
471+
-- --}
468472
testRunner (Just dir) defCode testCode
469473
where
470474
files :: M.Map FilePath (Either CompileError Code)

0 commit comments

Comments
 (0)