This repository was archived by the owner on Apr 15, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 82
feat(engine): json protocol #1035
Open
ProgramRipper
wants to merge
9
commits into
RobertCraigie:main
Choose a base branch
from
ProgramRipper:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e0098d4
feat(engine): json protocol
ProgramRipper 44eb8a5
chore(pre-commit.ci): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9599fe8
test: "files.insertFinalNewline": false
ProgramRipper 41179e3
revert(client): remove unnecessary None replacement
ProgramRipper 06b16a5
refactor(engine): backport to Python 3.12-
ProgramRipper b5e1d30
fix(engine): pass database test
ProgramRipper ed2a0f8
fix(engine): pass pyright check
ProgramRipper 7cc3171
fix(engine): pass database, pyright, mypy test again
ProgramRipper 96ffa49
fix(engine): what's wrong with mypy?
ProgramRipper 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .types import * | ||
from .serializer import dumps as dumps, serialize as serialize | ||
from .deserializer import deserialize as deserialize |
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,43 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from typing import Any | ||
from decimal import Decimal | ||
from datetime import datetime | ||
from typing_extensions import TypeGuard | ||
|
||
from .types import JsonOutputTaggedValue | ||
from ...fields import Base64 | ||
|
||
|
||
def deserialize(result: Any) -> Any: | ||
if not result: | ||
return result | ||
|
||
if isinstance(result, list): | ||
return list(map(deserialize, result)) | ||
|
||
if isinstance(result, dict): | ||
if is_tagged_value(result): | ||
return result['value'] # XXX: will pydantic cast this? | ||
|
||
return {k: deserialize(v) for k, v in result.items()} | ||
|
||
return result | ||
|
||
|
||
def is_tagged_value(value: dict[Any, Any]) -> TypeGuard[JsonOutputTaggedValue]: | ||
return isinstance(value.get('$type'), str) | ||
|
||
|
||
def deserialize_tagged_value(tagged: JsonOutputTaggedValue) -> Any: | ||
if tagged['$type'] == 'BigInt': | ||
return int(tagged['value']) | ||
elif tagged['$type'] == 'Bytes': | ||
return Base64.fromb64(tagged['value']) | ||
elif tagged['$type'] == 'DateTime': | ||
return datetime.fromisoformat(tagged['value']) | ||
elif tagged['$type'] == 'Decimal': | ||
return Decimal(tagged['value']) | ||
elif tagged['$type'] == 'Json': | ||
return json.loads(tagged['value']) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caution
test_field_not_found_error_selection
skippedJSON protocol does not explicitly select all fields, so it won't raise a
FieldNotFoundError
here.However we could: