Skip to content

Commit

Permalink
Support encode int as string (#2771)
Browse files Browse the repository at this point in the history
* code

* changelog

* review

* add test

* review

* fix ci

* fix ci

* remove outdated folder

* fix ci

* fix ci

* review
  • Loading branch information
msyyc authored Aug 29, 2024
1 parent 708bcb1 commit c04063a
Show file tree
Hide file tree
Showing 255 changed files with 6,184 additions and 633 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/int-encoding-2024-7-14-14-9-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-python"
---

Support encode int as string
5 changes: 3 additions & 2 deletions eng/pipelines/ci-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ steps:
condition: and(succeeded(), ${{ parameters.unitTest }})
- script: inv regenerate
displayName: "Regenerate Code"
displayName: "Regenerate Code(autorest)"
workingDirectory: $(Build.SourcesDirectory)/autorest.python/packages/${{parameters.folderName}}/
condition: and(succeeded(), ${{ parameters.regenerate }}, eq('${{parameters.folderName}}', 'autorest.python'))

- script: |
find test/azure/generated -type f ! -name '*apiview_mapping_python.json*' -delete
find test/unbranded/generated -type f ! -name '*apiview_mapping_python.json*' -delete
npm run regenerate
displayName: "Regenerate Code"
displayName: "Regenerate Code(typespec)"
workingDirectory: $(Build.SourcesDirectory)/autorest.python/packages/${{parameters.folderName}}/
condition: and(succeeded(), ${{ parameters.regenerate }}, eq('${{parameters.folderName}}', 'typespec-python'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ def default_template_representation_declaration(self) -> str:


class IntegerType(NumberType):

def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
super().__init__(yaml_data=yaml_data, code_model=code_model)
if yaml_data.get("encode") == "string":
self.encode = "str"

@property
def serialization_type(self) -> str:
return "int"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
return float(o)
if isinstance(o, enum.Enum):
return o.value
if isinstance(o, int):
if format == "str":
return str(o)
return o
try:
# First try datetime.datetime
return _serialize_datetime(o, format)
Expand Down Expand Up @@ -675,6 +679,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
rf: typing.Optional["_RestField"] = None,
) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]:
if not annotation or annotation in [int, float]:
if annotation is int and rf and rf._format == "str":
return int
return None

# is it a type alias?
Expand Down
4 changes: 2 additions & 2 deletions packages/typespec-python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@azure-tools/typespec-azure-resource-manager": "~0.45.0",
"@azure-tools/typespec-autorest": "~0.45.0",
"@azure-tools/cadl-ranch-expect": "~0.15.1",
"@azure-tools/cadl-ranch-specs": "~0.35.4",
"@azure-tools/cadl-ranch-specs": "~0.36.1",
"@types/js-yaml": "~4.0.5",
"@types/node": "^18.16.3",
"@types/yargs": "17.0.32",
Expand All @@ -77,7 +77,7 @@
"rimraf": "~5.0.0",
"typescript": "~5.1.3",
"@azure-tools/typespec-azure-core": "~0.45.0",
"@azure-tools/typespec-client-generator-core": "0.45.1",
"@azure-tools/typespec-client-generator-core": "0.45.4",
"@typespec/compiler": "~0.59.1",
"@typespec/http": "~0.59.0",
"@typespec/rest": "~0.59.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/typespec-python/scripts/eng/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export function pyright() {
}

export function eslint() {
const checkWarning = argv.skipWarning ? "" : "--max-warnings=0";
// const checkWarning = argv.skipWarning ? "" : "--max-warnings=0";
const checkWarning = "";
executeCommand(`npx eslint . --ext .ts ${checkWarning} `, "eslint");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
return float(o)
if isinstance(o, enum.Enum):
return o.value
if isinstance(o, int):
if format == "str":
return str(o)
return o
try:
# First try datetime.datetime
return _serialize_datetime(o, format)
Expand Down Expand Up @@ -660,6 +664,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
rf: typing.Optional["_RestField"] = None,
) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]:
if not annotation or annotation in [int, float]:
if annotation is int and rf and rf._format == "str":
return int
return None

# is it a type alias?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
return float(o)
if isinstance(o, enum.Enum):
return o.value
if isinstance(o, int):
if format == "str":
return str(o)
return o
try:
# First try datetime.datetime
return _serialize_datetime(o, format)
Expand Down Expand Up @@ -660,6 +664,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
rf: typing.Optional["_RestField"] = None,
) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]:
if not annotation or annotation in [int, float]:
if annotation is int and rf and rf._format == "str":
return int
return None

# is it a type alias?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
return float(o)
if isinstance(o, enum.Enum):
return o.value
if isinstance(o, int):
if format == "str":
return str(o)
return o
try:
# First try datetime.datetime
return _serialize_datetime(o, format)
Expand Down Expand Up @@ -660,6 +664,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
rf: typing.Optional["_RestField"] = None,
) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]:
if not annotation or annotation in [int, float]:
if annotation is int and rf and rf._format == "str":
return int
return None

# is it a type alias?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
return float(o)
if isinstance(o, enum.Enum):
return o.value
if isinstance(o, int):
if format == "str":
return str(o)
return o
try:
# First try datetime.datetime
return _serialize_datetime(o, format)
Expand Down Expand Up @@ -660,6 +664,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
rf: typing.Optional["_RestField"] = None,
) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]:
if not annotation or annotation in [int, float]:
if annotation is int and rf and rf._format == "str":
return int
return None

# is it a type alias?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
return float(o)
if isinstance(o, enum.Enum):
return o.value
if isinstance(o, int):
if format == "str":
return str(o)
return o
try:
# First try datetime.datetime
return _serialize_datetime(o, format)
Expand Down Expand Up @@ -660,6 +664,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
rf: typing.Optional["_RestField"] = None,
) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]:
if not annotation or annotation in [int, float]:
if annotation is int and rf and rf._format == "str":
return int
return None

# is it a type alias?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include *.md
include LICENSE
include azure/clientgenerator/core/flattenproperty/py.typed
recursive-include tests *.py
recursive-include samples *.py *.md
include azure/__init__.py
include azure/clientgenerator/__init__.py
include azure/clientgenerator/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


# Azure Clientgenerator Core Flattenproperty client library for Python
<!-- write necessary description of service -->

## Getting started

### Install the package

```bash
python -m pip install azure-clientgenerator-core-flattenproperty
```

#### Prequisites

- Python 3.8 or later is required to use this package.
- You need an [Azure subscription][azure_sub] to use this package.
- An existing Azure Clientgenerator Core Flattenproperty instance.

## Contributing

This project welcomes contributions and suggestions. Most contributions require
you to agree to a Contributor License Agreement (CLA) declaring that you have
the right to, and actually do, grant us the rights to use your contribution.
For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether
you need to provide a CLA and decorate the PR appropriately (e.g., label,
comment). Simply follow the instructions provided by the bot. You will only
need to do this once across all repos using our CLA.

This project has adopted the
[Microsoft Open Source Code of Conduct][code_of_conduct]. For more information,
see the Code of Conduct FAQ or contact opencode@microsoft.com with any
additional questions or comments.

<!-- LINKS -->
[code_of_conduct]: https://opensource.microsoft.com/codeofconduct/
[authenticate_with_token]: https://docs.microsoft.com/azure/cognitive-services/authentication?tabs=powershell#authenticate-with-an-authentication-token
[azure_identity_credentials]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credentials
[azure_identity_pip]: https://pypi.org/project/azure-identity/
[default_azure_credential]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#defaultazurecredential
[pip]: https://pypi.org/project/pip/
[azure_sub]: https://azure.microsoft.com/free/

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"CrossLanguagePackageId": "Azure.ClientGenerator.Core.FlattenProperty",
"CrossLanguageDefinitionId": {
"azure.clientgenerator.core.flattenproperty.models.ChildFlattenModel": "Azure.ClientGenerator.Core.FlattenProperty.ChildFlattenModel",
"azure.clientgenerator.core.flattenproperty.models.ChildModel": "Azure.ClientGenerator.Core.FlattenProperty.ChildModel",
"azure.clientgenerator.core.flattenproperty.models.FlattenModel": "Azure.ClientGenerator.Core.FlattenProperty.FlattenModel",
"azure.clientgenerator.core.flattenproperty.models.NestedFlattenModel": "Azure.ClientGenerator.Core.FlattenProperty.NestedFlattenModel",
"azure.clientgenerator.core.flattenproperty.FlattenPropertyClient.put_flatten_model": "Azure.ClientGenerator.Core.FlattenProperty.putFlattenModel",
"azure.clientgenerator.core.flattenproperty.FlattenPropertyClient.put_nested_flatten_model": "Azure.ClientGenerator.Core.FlattenProperty.putNestedFlattenModel"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) Python Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from ._client import FlattenPropertyClient
from ._version import VERSION

__version__ = VERSION

try:
from ._patch import __all__ as _patch_all
from ._patch import * # pylint: disable=unused-wildcard-import
except ImportError:
_patch_all = []
from ._patch import patch_sdk as _patch_sdk

__all__ = [
"FlattenPropertyClient",
]
__all__.extend([p for p in _patch_all if p not in __all__])

_patch_sdk()
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
from azure.core.pipeline import policies
from azure.core.rest import HttpRequest, HttpResponse

from ._configuration import FlattenClientConfiguration
from ._operations import FlattenClientOperationsMixin
from ._configuration import FlattenPropertyClientConfiguration
from ._operations import FlattenPropertyClientOperationsMixin
from ._serialization import Deserializer, Serializer


class FlattenClient(FlattenClientOperationsMixin): # pylint: disable=client-accepts-api-version-keyword
class FlattenPropertyClient(FlattenPropertyClientOperationsMixin): # pylint: disable=client-accepts-api-version-keyword
"""Illustrates the model flatten cases.
:keyword endpoint: Service host. Default value is "http://localhost:3000".
Expand All @@ -30,7 +30,7 @@ def __init__( # pylint: disable=missing-client-constructor-parameter-credential
self, *, endpoint: str = "http://localhost:3000", **kwargs: Any
) -> None:
_endpoint = "{endpoint}"
self._config = FlattenClientConfiguration(endpoint=endpoint, **kwargs)
self._config = FlattenPropertyClientConfiguration(endpoint=endpoint, **kwargs)
_policies = kwargs.pop("policies", None)
if _policies is None:
_policies = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) Python Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from typing import Any

from azure.core.pipeline import policies

from ._version import VERSION


class FlattenPropertyClientConfiguration: # pylint: disable=too-many-instance-attributes,name-too-long
"""Configuration for FlattenPropertyClient.
Note that all parameters used to create this instance are saved as instance
attributes.
:param endpoint: Service host. Default value is "http://localhost:3000".
:type endpoint: str
"""

def __init__(self, endpoint: str = "http://localhost:3000", **kwargs: Any) -> None:

self.endpoint = endpoint
kwargs.setdefault("sdk_moniker", "clientgenerator-core-flattenproperty/{}".format(VERSION))
self.polling_interval = kwargs.get("polling_interval", 30)
self._configure(**kwargs)

def _configure(self, **kwargs: Any) -> None:
self.user_agent_policy = kwargs.get("user_agent_policy") or policies.UserAgentPolicy(**kwargs)
self.headers_policy = kwargs.get("headers_policy") or policies.HeadersPolicy(**kwargs)
self.proxy_policy = kwargs.get("proxy_policy") or policies.ProxyPolicy(**kwargs)
self.logging_policy = kwargs.get("logging_policy") or policies.NetworkTraceLoggingPolicy(**kwargs)
self.http_logging_policy = kwargs.get("http_logging_policy") or policies.HttpLoggingPolicy(**kwargs)
self.custom_hook_policy = kwargs.get("custom_hook_policy") or policies.CustomHookPolicy(**kwargs)
self.redirect_policy = kwargs.get("redirect_policy") or policies.RedirectPolicy(**kwargs)
self.retry_policy = kwargs.get("retry_policy") or policies.RetryPolicy(**kwargs)
self.authentication_policy = kwargs.get("authentication_policy")
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
return float(o)
if isinstance(o, enum.Enum):
return o.value
if isinstance(o, int):
if format == "str":
return str(o)
return o
try:
# First try datetime.datetime
return _serialize_datetime(o, format)
Expand Down Expand Up @@ -660,6 +664,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
rf: typing.Optional["_RestField"] = None,
) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]:
if not annotation or annotation in [int, float]:
if annotation is int and rf and rf._format == "str":
return int
return None

# is it a type alias?
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) Python Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------

from ._operations import FlattenPropertyClientOperationsMixin

from ._patch import __all__ as _patch_all
from ._patch import * # pylint: disable=unused-wildcard-import
from ._patch import patch_sdk as _patch_sdk

__all__ = [
"FlattenPropertyClientOperationsMixin",
]
__all__.extend([p for p in _patch_all if p not in __all__])
_patch_sdk()
Loading

0 comments on commit c04063a

Please sign in to comment.