Skip to content

Conversation

@JordonPhillips
Copy link
Contributor

@JordonPhillips JordonPhillips commented Jan 24, 2023

Some example output:

async def _serialize_json_blobs(input: JsonBlobsInput, config: Config) -> Request:
    [...]

    body: Any = b""
    result: dict[str, Document] = {}

    if input.data is not None:
        result["data"] = b64encode(input.data).decode("utf-8")

    body = json_dumps(result).encode("utf-8")

    return _Request(
        url=_URI(
            host="",
            path=path,
            scheme="https",
            query=query,
        ),
        method="POST",
        headers=headers,
        body=body,
    )




def _serialize_greeting_struct(
    input: GreetingStruct, config: Config
) -> dict[str, Document]:
    result: dict[str, Document] = {}

    if input.hi is not None:
        result["hi"] = input.hi

    return result


def _serialize_simple_union(input: SimpleUnion, config: Config) -> dict[str, Document]:
    match input:
        case SimpleUnionInt():
            return {"int": input.value}

        case SimpleUnionString():
            return {"string": input.value}

        case _:
            raise ServiceError(f"Unexpected union variant: {type(input)}")


def _serialize_timestamp_list(input: list[datetime], config: Config) -> list[Document]:
    return [serialize_epoch_seconds(ensure_utc(e)) for e in input]


def _serialize_dense_set_map(
    input: dict[str, list[str]], config: Config
) -> dict[str, Document]:
    return {k: _serialize_string_set(v, config) for k, v in input.items()}

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@JordonPhillips JordonPhillips force-pushed the serialize-json-bodies branch 2 times, most recently from f4f777b to 0f0f158 Compare January 24, 2023 17:46
@JordonPhillips
Copy link
Contributor Author

I updated this to not generate noop serialization functions for lists and maps, so we won't get stuff like:

def _serialize_string_list(input: list[str], config: Config) -> list[Document]:
    return [e for e in input]

This updates the generator to not generate serialization functions in
JSON protocols when they would be a noop. This should save some
space, and more importantly some clock time.
The dict and list types are invariant, so they're not quite suitable
for use in the Document type where we want to accept something
explicitly typed as, say, `list[str]`.
jonemo
jonemo previously approved these changes Feb 1, 2023
Copy link
Contributor

@jonemo jonemo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Co-authored-by: Jonas Neubert <jonasneu@amazon.com>
@JordonPhillips JordonPhillips requested a review from jonemo February 1, 2023 11:13
@JordonPhillips JordonPhillips merged commit 5af6aeb into develop Feb 1, 2023
@JordonPhillips JordonPhillips deleted the serialize-json-bodies branch February 1, 2023 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants