Skip to content

Commit

Permalink
feat(base): update resource ID generation to handle Unicode characters (
Browse files Browse the repository at this point in the history
#313)

The resource ID generation logic has been updated in both Python and TypeScript files to handle Unicode characters. This ensures that Unicode characters are properly encoded in the resource ID string, preventing any issues with special characters. The `encode_unicode` function has been added to both files to handle the encoding. This improvement enhances the reliability and compatibility of the resource ID generation process.
  • Loading branch information
jianzs authored Aug 5, 2024
1 parent 26ee4ff commit dbbfde4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/forty-spoons-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@plutolang/base": patch
---

feat: update resource ID generation to handle Unicode characters

The resource ID generation logic has been updated in both Python and TypeScript files to handle Unicode characters. This ensures that Unicode characters are properly encoded in the resource ID string, preventing any issues with special characters. The `encode_unicode` function has been added to both files to handle the encoding. This improvement enhances the reliability and compatibility of the resource ID generation process.
6 changes: 6 additions & 0 deletions packages/base-py/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# pluto-base

## 0.0.7

feat: update resource ID generation to handle Unicode characters

The resource ID generation logic has been updated in both Python and TypeScript files to handle Unicode characters. This ensures that Unicode characters are properly encoded in the resource ID string, preventing any issues with special characters. The `encode_unicode` function has been added to both files to handle the encoding. This improvement enhances the reliability and compatibility of the resource ID generation process.

## 0.0.6

feat: add no-op infrastructure API to client SDK
Expand Down
6 changes: 5 additions & 1 deletion packages/base-py/pluto_base/utils/resource_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def gen_resource_id(
resource_type,
provided_name,
)
resource_full_id = re.sub(r"[^_0-9a-zA-Z]+", "_", "_".join(args))
resource_full_id = re.sub(r"[^_0-9a-zA-Z]+", "_", encode_unicode("_".join(args)))

if len(resource_full_id) <= RESOURCE_ID_MAX_LENGTH:
return resource_full_id
Expand All @@ -41,3 +41,7 @@ def gen_resource_id(
start = len(resource_full_id) - (RESOURCE_ID_MAX_LENGTH - len(hash_digest))
end = len(resource_full_id)
return resource_full_id[start:end] + hash_digest


def encode_unicode(s: str) -> str:
return "".join(f"\\u{ord(c):04x}" if ord(c) > 255 else c for c in s)
2 changes: 1 addition & 1 deletion packages/base-py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "pluto-base"
version = "0.0.6"
version = "0.0.7"
description = "The Base Library for Pluto Programming Language."
authors = ["Jade Zheng <zheng.shoujian@outlook.com>"]
license = "Apache-2.0"
Expand Down
9 changes: 8 additions & 1 deletion packages/base/utils/resource-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function genResourceId(...args: readonly string[]): string {
args = [currentProjectName(), currentStackName()].concat(args);
}

const resourceFullId = args.join("_").replace(/[^_0-9a-zA-Z]+/g, "_");
const resourceFullId = encodeUnicode(args.join("_")).replace(/[^_0-9a-zA-Z]+/g, "_");
if (resourceFullId.length <= RESOURCE_ID_MAX_LENGTH) {
return resourceFullId;
} else {
Expand All @@ -47,3 +47,10 @@ export function genResourceId(...args: readonly string[]): string {
return resourceFullId.substring(start, end) + hash;
}
}

function encodeUnicode(str: string): string {
// eslint-disable-next-line no-control-regex
return str.replace(/[^\x00-\xff]/g, (c) => {
return "\\u" + c.charCodeAt(0).toString(16);
});
}

0 comments on commit dbbfde4

Please sign in to comment.