-
Notifications
You must be signed in to change notification settings - Fork 122
Add support for byte translation #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MOmarMiraj
merged 28 commits into
1Password:main
from
MOmarMiraj:omar/add-typeshare-byte-translation
Feb 21, 2025
Merged
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
dc6a467
Add support for byte translation
MOmarMiraj 1b0693d
fmt fix
MOmarMiraj 5bc8645
instead of hardcoding type, use the value inside the typeshare.toml
MOmarMiraj d24601d
Reduce code dupe and throw it in a function
MOmarMiraj 3ff5cec
add todo and comments
MOmarMiraj 8c07d74
update python and typescript replacer/reviver code as well as update …
MOmarMiraj 99dcebe
fix logic in reviver/replacer as welll as improve logic to include fu…
MOmarMiraj ca2933c
remove unneeded comment
MOmarMiraj 5282554
turn is_bytes false after its turned on
MOmarMiraj a101237
update python serialization to a cleaner format and on a field level …
MOmarMiraj 1a14865
fix is_bytes logic
MOmarMiraj ebee1ed
remove unneeded function
MOmarMiraj ea56003
change to Vec<u8> for .get() for better readability
MOmarMiraj f7bd889
fix typeshare printing for all values
MOmarMiraj 5619a59
fix spacing and logic for python and js
MOmarMiraj 784fb62
fix typeshare names add spaces between function and make python custo…
MOmarMiraj 396b58a
change to doc comment
MOmarMiraj 7165acd
add spaces between functions
MOmarMiraj 1b7f297
refactor python parsing logic and update test cases
MOmarMiraj 3739f44
remove todo link in go where its not neeeded
MOmarMiraj 71958f6
update test as its suppose to be isInteger not IsInteger
MOmarMiraj 8b64a32
remove nested id function and impl display traits for special rust ty…
MOmarMiraj f6c1901
make code more cleaner and fix some edge cases
MOmarMiraj f79fcaa
remove unnecessary diff
MOmarMiraj 5c45105
remove unnecessary diff
MOmarMiraj dd318a0
update python to be more generic when acquiring custom translation types
MOmarMiraj b92746e
update python to have JSON in custom translation logic, remove unnces…
MOmarMiraj caca8ce
update ts comment
MOmarMiraj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #[typeshare] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct Foo { | ||
| pub bytes: Vec<u8>, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package proto | ||
|
|
||
| import "encoding/json" | ||
|
|
||
| type Foo struct { | ||
| Bytes []byte `json:"bytes"` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pydantic import BaseModel, field_serializer, field_validator | ||
|
|
||
|
|
||
| class Foo(BaseModel): | ||
| bytes: bytes | ||
|
|
||
|
|
||
| @field_serializer("content") | ||
MOmarMiraj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def serialize_data(self, value: bytes) -> list[int]: | ||
| return list(value) | ||
|
|
||
| @field_validator("content", mode="before") | ||
MOmarMiraj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def deserialize_data(cls, value): | ||
| if isinstance(value, list): | ||
MOmarMiraj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return bytes(value) | ||
| return value | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| export interface Foo { | ||
| bytes: Uint8Array; | ||
| } | ||
|
|
||
| // Reviver code - required for JSON deserialization | ||
MOmarMiraj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| export function TypeshareReviver(key: string, value: unknown): unknown { | ||
| return Array.isArray(value) && value.every(Number.isFinite) | ||
MOmarMiraj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ? new Uint8Array(value) | ||
| : value; | ||
| } | ||
|
|
||
| // Replacer code - required for JSON serialization | ||
| export function TypeshareReplacer(key: string, value: unknown): unknown { | ||
| if (value instanceof Uint8Array) { | ||
| return Array.from(value); | ||
| } | ||
| return value; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime | ||
| from pydantic import BaseModel | ||
|
|
||
|
|
||
| class Foo(BaseModel): | ||
| time: datetime | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.