Skip to content

Commit 0dcd068

Browse files
author
John Tompkins
authored
Long addition (#328)
Support long types via format in jsonschema. Resource provider authors can now specify a `format` key in their integer schemas to determine if the code generated variable for the property will be of type Long (`int64`) or Integer(`int32`). Will default to Integer.
1 parent fc1725f commit 0dcd068

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,26 @@ pip3 install cloudformation-cli-java-plugin
2323

2424
Refer to the [CloudFormation CLI User Guide](https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/resource-types.html) for the [CloudFormation CLI](https://github.com/aws-cloudformation/cloudformation-cli) for usage instructions.
2525

26+
### Alternate Type Formats
27+
The `format` keyword can be specified on primitive types defined in a resource provider's schema to allow the CloudFormation CLI Java Plugin to generate more than the defaults for primitive types. Consult the table below for what formats are available and defaults for various types. The `default` value is used if omitted:
28+
29+
| JSON Schema Type | Format value | Generated variable type |
30+
| ---- | ----------- | ---------------------- |
31+
| boolean | `default` | [Boolean](https://docs.oracle.com/javase/8/docs/api/java/lang/Boolean.html)|
32+
| integer | `default`, `int32` | [Integer](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html)|
33+
| integer | `int64` | [Long](https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html)|
34+
| number | `default` | [Double](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html)|
35+
| string | `default` | [String](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html)|
36+
37+
For example, the below schema for a property would generate a variable of type `Long`.
38+
```
39+
{
40+
"type": "integer",
41+
"format": "int64"
42+
}
43+
```
44+
45+
2646
Development
2747
-----------
2848

python/rpdk/java/resolver.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
1-
from rpdk.core.jsonutils.resolver import UNDEFINED, ContainerType
1+
import logging
2+
3+
from rpdk.core.jsonutils.resolver import FORMAT_DEFAULT, UNDEFINED, ContainerType
4+
5+
LOG = logging.getLogger(__name__)
26

37
PRIMITIVE_TYPES = {
4-
"string": "String",
5-
"integer": "Integer",
6-
"boolean": "Boolean",
7-
"number": "Double",
8-
UNDEFINED: "Object",
8+
"string": {"default": "String"},
9+
"integer": {"default": "Integer", "int32": "Integer", "int64": "Long"},
10+
"boolean": {"default": "Boolean"},
11+
"number": {"default": "Double"},
12+
UNDEFINED: {"default": "Object"},
913
}
1014

1115

1216
def translate_type(resolved_type):
1317
if resolved_type.container == ContainerType.MODEL:
1418
return resolved_type.type
1519
if resolved_type.container == ContainerType.PRIMITIVE:
16-
return PRIMITIVE_TYPES[resolved_type.type]
20+
try:
21+
primitive_format = PRIMITIVE_TYPES[resolved_type.type][
22+
resolved_type.type_format
23+
]
24+
except KeyError:
25+
primitive_format = PRIMITIVE_TYPES[resolved_type.type][FORMAT_DEFAULT]
26+
LOG.error(
27+
"Could not find specified format '%s' for type '%s'. "
28+
"Defaulting to '%s'",
29+
resolved_type.type_format,
30+
resolved_type.type,
31+
primitive_format,
32+
)
33+
return primitive_format
1734

1835
if resolved_type.container == ContainerType.MULTIPLE:
1936
return "Object"
2037

2138
item_type = translate_type(resolved_type.type)
2239

2340
if resolved_type.container == ContainerType.DICT:
24-
key_type = PRIMITIVE_TYPES["string"]
41+
key_type = PRIMITIVE_TYPES["string"]["default"]
2542
return f"Map<{key_type}, {item_type}>"
2643
if resolved_type.container == ContainerType.LIST:
2744
return f"List<{item_type}>"

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def find_version(*file_paths):
3636
# package_data -> use MANIFEST.in instead
3737
include_package_data=True,
3838
zip_safe=True,
39-
install_requires=["cloudformation-cli>=0.1.4,<0.2"],
39+
install_requires=["cloudformation-cli>=0.1.14,<0.2"],
4040
python_requires=">=3.6",
4141
entry_points={"rpdk.v1.languages": ["java = rpdk.java.codegen:JavaLanguagePlugin"]},
4242
license="Apache License 2.0",

tests/test_resolver.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@
33
from rpdk.java.resolver import PRIMITIVE_TYPES, translate_type
44

55
RESOLVED_TYPES = [
6-
(ResolvedType(ContainerType.PRIMITIVE, item_type), native_type)
7-
for item_type, native_type in PRIMITIVE_TYPES.items()
6+
(ResolvedType(ContainerType.PRIMITIVE, item_type), formats["default"])
7+
for item_type, formats in PRIMITIVE_TYPES.items()
8+
]
9+
10+
RESOLVED_INTEGER_FORMATS = [
11+
(
12+
ResolvedType(ContainerType.PRIMITIVE, "integer", "int64"),
13+
PRIMITIVE_TYPES["integer"]["int64"],
14+
),
15+
(
16+
ResolvedType(ContainerType.PRIMITIVE, "integer", "int32"),
17+
PRIMITIVE_TYPES["integer"]["int32"],
18+
),
819
]
920

1021

@@ -45,3 +56,13 @@ def test_translate_type_set(resolved_type, native_type):
4556
def test_translate_type_unknown(resolved_type, _java_type):
4657
with pytest.raises(ValueError):
4758
translate_type(ResolvedType("foo", resolved_type))
59+
60+
61+
@pytest.mark.parametrize("resolved_type,java_type", RESOLVED_INTEGER_FORMATS)
62+
def test_translate_type_integer_formats(resolved_type, java_type):
63+
assert translate_type(resolved_type) == java_type
64+
65+
66+
def test_translate_type_unavailable_format():
67+
resolved_type = ResolvedType(ContainerType.PRIMITIVE, "integer", "int128")
68+
assert translate_type(resolved_type) == PRIMITIVE_TYPES["integer"]["default"]

0 commit comments

Comments
 (0)