Skip to content

Commit

Permalink
Merge pull request #280 from dahlia/omittable-optional-params
Browse files Browse the repository at this point in the history
Make optional parameters omittable
  • Loading branch information
dahlia authored May 24, 2018
2 parents 454e4b6 + 435b82f commit fa13a7f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ mpcBehindName :: MethodParameterCode -> T.Text
mpcBehindName MethodParameterCode { mpcParam = Parameter pName _ _ } =
toBehindSnakeCaseText pName

mpcOptional :: MethodParameterCode -> Bool
mpcOptional mpc = case mpcType mpc of
OptionModifier _ -> True
_ -> False

mpcType :: MethodParameterCode -> TypeExpression
mpcType MethodParameterCode { mpcParam = Parameter _ typeExpr _ } = typeExpr

Expand Down Expand Up @@ -1313,7 +1318,11 @@ class #{className}(service_type):
try:
field_value = value['#{mpcBehindName mpc}']
except KeyError:
%{ if mpcOptional mpc }
result['#{mpcAttributeName mpc}'] = None
%{ else }
on_error('.#{mpcBehindName mpc}', 'Expected to exist.')
%{ endif }
else:
result['#{mpcAttributeName mpc}'] = \
table['#{mpcBehindName mpc}'](
Expand Down
1 change: 1 addition & 0 deletions test/nirum_fixture/fixture/foo.nrm
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ union animal = cat
service sample-service (
sample-method (animal a, product b/bb, gender c, way d/dd,
uuid e, binary f/ff, bigint g, text h/hh),
sample-method-with-optional-param(uuid a, animal? b),
animal sample-method-that-returns(),
);

Expand Down
22 changes: 22 additions & 0 deletions test/python/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,28 @@ def test_service_deserialize_arguments(fx_method_args):
assert errors == {('', 'Expected an object.')}


def test_service_deserialize_arguments_with_omitted_keys(fx_dog):
m = SampleService.sample_method_with_optional_param
f = m.__nirum_deserialize_arguments__
payload = {
'a': 'f7db93e3-731e-48ef-80a2-cac81e02f1ae',
'b': fx_dog[1],
}
expected = {
'a': uuid.UUID('F7DB93E3-731E-48EF-80A2-CAC81E02F1AE'),
'b': fx_dog[0],
}
assert f(payload) == expected
assert f(dict(payload, b=None)) == dict(expected, b=None)
assert f({'a': payload['a']}) == dict(expected, b=None)
with raises(ValueError) as e:
f({'b': fx_dog[1]})
assert str(e.value) == '.a: Expected to exist.'
with raises(ValueError) as e:
f(dict(payload, b=dict(fx_dog[1], name=None)))
assert str(e.value) == '.b.name: Expected a string.'


def test_service_argument_serializers(fx_method_args):
args, expected = fx_method_args
table = SampleService.sample_method.__nirum_argument_serializers__
Expand Down

0 comments on commit fa13a7f

Please sign in to comment.