Skip to content

Add support for DateTime field for Go, JS, Python #236

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
merged 24 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6d62e79
Add support for DateTime field for Go, JS, Python and keep the origin…
MOmarMiraj Mar 12, 2025
66bfad1
fmt fix
MOmarMiraj Mar 12, 2025
916ef7b
remove diff for kotlin
MOmarMiraj Mar 12, 2025
05ea27e
remove unused error and add TODOs for the languages that we don't add…
MOmarMiraj Mar 12, 2025
def0131
add todo
MOmarMiraj Mar 12, 2025
4e9246f
change to OffSetDateTime
MOmarMiraj Mar 12, 2025
f9f3ca4
add test cases for datetime and add errors for Datetime for unsupport…
MOmarMiraj Mar 12, 2025
eddbf9a
fix python serialization logic
MOmarMiraj Mar 12, 2025
4ee40f5
Merge branch 'main' into omar/34/add-date-time-support
MOmarMiraj Mar 12, 2025
a7accd9
update typescript update
MOmarMiraj Mar 12, 2025
5b474d6
add key as a match for TS
MOmarMiraj Mar 12, 2025
fc673a8
clean up code a bit
MOmarMiraj Mar 12, 2025
529be9c
add capability to add imports in go as well as address nits in python…
MOmarMiraj Mar 14, 2025
b4d5d3f
fix go imports and typescript spacing
MOmarMiraj Mar 14, 2025
e1a2b92
update imports to be tabs and sort them
MOmarMiraj Mar 14, 2025
ad45688
update expected tests
MOmarMiraj Mar 14, 2025
8371b66
ensure the custom translations are determnistic by sorting them
MOmarMiraj Mar 14, 2025
7a74614
fmt fix
MOmarMiraj Mar 14, 2025
9e39854
update typescript regex to include optional seconds as well as fix go…
MOmarMiraj Mar 14, 2025
82b5ece
add new line after single imports
MOmarMiraj Mar 14, 2025
2ef4d4a
change write import to add import
MOmarMiraj Mar 14, 2025
646f8f2
fix chaining and check if value is string instead of type casting it
MOmarMiraj Mar 14, 2025
2c7717e
ensure custom serialization/deserialzation is determinstic
MOmarMiraj Mar 14, 2025
5056f25
make go imports deterministic and add all numbers for python serializ…
MOmarMiraj Mar 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion core/data/tests/anonymous_struct_with_rename/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// Generated type representing the anonymous struct variant `List` of the `AnonymousStructWithRename` Rust enum
type AnonymousStructWithRenameListInner struct {
List []string `json:"list"`
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_apply_prefix_correctly/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type ItemDetailsFieldValue struct {
Hello string `json:"hello"`
}
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_generate_algebraic_enum/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// Struct comment
type ItemDetailsFieldValue struct {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type SomeEnumTypes string
const (
SomeEnumTypeVariantA SomeEnumTypes = "A"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// Generated type representing the anonymous struct variant `Us` of the `AutofilledBy` Rust enum
type AutofilledByUsInner struct {
// The UUID for the fill
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_generate_bare_string_enum/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// This is a comment.
type Colors string
const (
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_generate_const/output.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package proto

import "encoding/json"

const MyVar uint32 = 12
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type AddressDetails struct {
}
type AddressTypes string
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_generate_simple_enum/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// This is a comment.
// Continued lovingly here
type Colors string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type Location struct {
}
// This is a comment.
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_generate_slice_of_user_type/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type Video struct {
Tags []Tag `json:"tags"`
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type MyStruct struct {
A int `json:"a"`
C int `json:"c"`
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_generate_unit_structs/output.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package proto

import "encoding/json"

type UnitStruct struct {
}
1 change: 0 additions & 1 deletion core/data/tests/can_handle_anonymous_struct/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// Generated type representing the anonymous struct variant `Us` of the `AutofilledBy` Rust enum
type AutofilledByUsInner struct {
// The UUID for the fill
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_handle_quote_in_serde_rename/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// This is a comment.
type Colors string
const (
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_handle_serde_rename/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type OtherType struct {
}
// This is a comment.
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_handle_serde_rename_all/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// This is a Person struct with camelCase rename
type Person struct {
FirstName string `json:"firstName"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type OtherType struct {
}
// This is a comment.
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_handle_unit_type/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// This struct has a unit field
type StructHasVoidType struct {
ThisIsAUnit struct{} `json:"thisIsAUnit"`
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/can_override_types/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type OverrideStruct struct {
FieldToOverride uint `json:"fieldToOverride"`
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type A struct {
Field uint32 `json:"field"`
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// This is a comment.
// Continued lovingly here
type Colors string
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/excluded_by_target_os/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// A struct with no target_os. Should be generated when
// we use --target-os.
type AlwaysAccept struct {
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/generate_types/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type CustomType struct {
}
type Types struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package proto

import "encoding/json"

type MyEmptyStruct struct {
}
1 change: 0 additions & 1 deletion core/data/tests/kebab_case_rename/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// This is a comment.
type Things struct {
Bla string `json:"bla"`
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/orders_types/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type A struct {
Field uint32 `json:"field"`
}
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/recursive_enum_decorator/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// Generated type representing the anonymous struct variant `Exactly` of the `MoreOptions` Rust enum
type MoreOptionsExactlyInner struct {
Config string `json:"config"`
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/resolves_qualified_type/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type QualifiedTypes struct {
Unqualified string `json:"unqualified"`
Qualified string `json:"qualified"`
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/serde_rename_references/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type AliasTest []SomethingFoo

type Test struct {
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/serialize_anonymous_field_as/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type SomeEnumTypes string
const (
// The associated String contains some opaque context
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/serialize_field_as/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type EditItemViewModelSaveRequest struct {
Context string `json:"context"`
Values []EditItemSaveValue `json:"values"`
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/serialize_type_alias/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type Uuid string

// Unique identifier for an Account
Expand Down
1 change: 0 additions & 1 deletion core/data/tests/smart_pointers/output.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

// This is a comment.
type ArcyColors struct {
Red int `json:"red"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package proto

import "encoding/json"

type ItemDetailsFieldValue struct {
}
type AdvancedColorsTypes string
Expand Down
6 changes: 0 additions & 6 deletions core/data/tests/test_byte_translation/input.rs

This file was deleted.

8 changes: 0 additions & 8 deletions core/data/tests/test_byte_translation/output.go

This file was deleted.

24 changes: 0 additions & 24 deletions core/data/tests/test_byte_translation/output.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[typeshare]
#[serde(rename_all = "camelCase")]
pub struct Foo {
pub time: time::OffsetDateTime,
pub time2: time::OffsetDateTime,
pub time3: time::OffsetDateTime,
pub bytes: Vec<u8>,
pub bytes2: Vec<u8>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package proto

import (
"encoding/json"
"time"
)

type Foo struct {
Time time.Time `json:"time"`
Time2 time.Time `json:"time2"`
Time3 time.Time `json:"time3"`
Bytes []byte `json:"bytes"`
Bytes2 []byte `json:"bytes2"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

from datetime import datetime
from pydantic import BaseModel, BeforeValidator, ConfigDict, Field, PlainSerializer
from typing import Annotated


def serialize_binary_data(value: bytes) -> list[int]:
return list(value)

def deserialize_binary_data(value):
if isinstance(value, list):
if all(isinstance(x, int) and 0 <= x <= 255 for x in value):
return bytes(value)
raise ValueError("All elements must be integers in the range 0-255 (u8).")
elif isinstance(value, bytes):
return value
raise TypeError("Content must be a list of integers (0-255) or bytes.")

def serialize_datetime_data(utc_time: datetime) -> str:
return utc_time.strftime("%Y-%m-%dT%H:%M:%SZ")

def parse_rfc3339(date_str: str) -> datetime:
date_formats = [
"%Y-%m-%dT%H:%M:%SZ",
"%Y-%m-%dT%H:%M:%S.%fZ"
]

for fmt in date_formats:
try:
return datetime.strptime(date_str, fmt)
except ValueError:
continue

raise ValueError(f"Invalid RFC 3339 date format: {date_str}")

class Foo(BaseModel):
model_config = ConfigDict(populate_by_name=True)

time: Annotated[datetime, BeforeValidator(parse_rfc3339), PlainSerializer(serialize_datetime_data)]
time_2: Annotated[datetime, BeforeValidator(parse_rfc3339), PlainSerializer(serialize_datetime_data)] = Field(alias="time2")
time_3: Annotated[datetime, BeforeValidator(parse_rfc3339), PlainSerializer(serialize_datetime_data)] = Field(alias="time3")
bytes: Annotated[bytes, BeforeValidator(deserialize_binary_data), PlainSerializer(serialize_binary_data)]
bytes_2: Annotated[bytes, BeforeValidator(deserialize_binary_data), PlainSerializer(serialize_binary_data)] = Field(alias="bytes2")

Loading
Loading