Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ webrpc-gen -schema=service.ridl -target=github.com/webrpc/gen-python@VERSION -cl
| `-client` | off | generate the `httpx` client |
| `-server` | off | generate the WSGI server + service `Protocol` |
| `-schemaHash=false` | on | omit the schema hash + version constants from the header |
| `-webrpcHeader=false` | on | omit the `Webrpc` version header from client requests |

## Output

Expand Down
7 changes: 5 additions & 2 deletions _examples/example.gen.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# example v1.0.0 eff11df6be0966e5f8ddb7005bae50bf68a5059c
# example v1.0.0 8ea6c2298bb7597dce55f9ec922fba0a5257fc54
# --
# Code generated by webrpc-gen@unknown with .. generator. DO NOT EDIT.
#
Expand All @@ -16,7 +16,7 @@

WEBRPC_VERSION = "v1"
WEBRPC_SCHEMA_VERSION = "v1.0.0"
WEBRPC_SCHEMA_HASH = "eff11df6be0966e5f8ddb7005bae50bf68a5059c"
WEBRPC_SCHEMA_HASH = "8ea6c2298bb7597dce55f9ec922fba0a5257fc54"



Expand All @@ -38,6 +38,7 @@ class Item:
name: ItemName
tier: ItemTier
count: int
quantity: Quantity
balance: int
tags: list[str]
attributes: dict[str, str]
Expand All @@ -54,6 +55,7 @@ def from_dict(cls, data: dict[str, Any]) -> "Item":
name=data.get("name"),
tier=ItemTier.from_dict(data.get("tier")),
count=data.get("count"),
quantity=data.get("quantity"),
balance=_parse_bigint(data.get("balance")),
tags=[_e0 for _e0 in (data.get("tags") or [])],
attributes={ _k0:_v0 for _k0, _v0 in (data.get("attributes") or {}).items() },
Expand All @@ -70,6 +72,7 @@ def to_dict(self) -> dict[str, Any]:
"name": _to_value(self.name),
"tier": _to_value(self.tier),
"count": _to_value(self.count),
"quantity": _to_value(self.quantity),
"balance": _bigint_to_value(self.balance),
"tags": [_to_value(_e0) for _e0 in (self.tags or [])],
"attributes": { _to_value(_k0): _to_value(_v0) for _k0, _v0 in (self.attributes or {}).items() },
Expand Down
1 change: 1 addition & 0 deletions _examples/example.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct Item
- name: ItemName
- tier: ItemTier
- count: uint32
- quantity: Quantity
- balance: bigint
- tags: []string
- attributes: map<string,string>
Expand Down
3 changes: 3 additions & 0 deletions _examples/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def _make_item():
name="widget", # ItemName alias -> str
tier=api.ItemTier.PREMIUM,
count=5,
quantity=12, # Quantity alias -> int
balance=9007199254740993, # > 2**53, must survive as a decimal string on the wire
tags=["a", "b"],
attributes={"k": "v"},
Expand Down Expand Up @@ -66,6 +67,7 @@ def count_by_tier(self):

def test_wire_shape():
d = _make_item().to_dict()
assert d["quantity"] == 12 # Quantity alias (uint32) -> int on the wire
assert d["balance"] == "9007199254740993", d["balance"] # bigint -> decimal string
assert d["createdAt"] == "2020-01-01T00:00:00+00:00", d["createdAt"] # tz-normalized
assert d["class"] == "reserved-name" # keyword field uses wire key
Expand All @@ -77,6 +79,7 @@ def test_wire_shape():
def test_type_serde_roundtrip():
decoded = api.Item.from_dict(_make_item().to_dict())
assert decoded.tier is api.ItemTier.PREMIUM
assert decoded.quantity == 12 and isinstance(decoded.quantity, int) # Quantity alias round-trips as int
assert decoded.balance == 9007199254740993 and isinstance(decoded.balance, int)
assert decoded.createdAt == datetime(2020, 1, 1, tzinfo=timezone.utc)
assert decoded.class_ == "reserved-name"
Expand Down
3 changes: 2 additions & 1 deletion client.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{{- $aliasMap := .AliasMap -}}
{{- $basepath := .BasePath -}}
{{- $services := .Services -}}
{{- $webrpcHeader := .WebrpcHeader -}}
{{- $kw := list "False" "None" "True" "and" "as" "assert" "async" "await" "break" "class" "continue" "def" "del" "elif" "else" "except" "finally" "for" "from" "global" "if" "import" "in" "is" "lambda" "nonlocal" "not" "or" "pass" "raise" "return" "try" "while" "with" "yield" -}}
{{- range $_, $service := $services}}

Expand All @@ -24,7 +25,7 @@ class {{$service.Name}}Client:
"{{$ik}}": {{if $in.Optional}}None if {{$ip}} is None else {{end}}{{template "toValue" dict "Type" $in.Type "TypeMap" $typeMap "AliasMap" $aliasMap "Var" $ip}},
{{- end}}
},
headers={"Content-Type": "application/json", "Accept": "application/json", "Webrpc": WEBRPC_HEADER_VALUE},
headers={"Content-Type": "application/json", "Accept": "application/json"{{if $webrpcHeader}}, "Webrpc": WEBRPC_HEADER_VALUE{{end}}},
)
_data = self._handle_response(_resp)
{{- if eq (len $method.Outputs) 0}}
Expand Down
3 changes: 2 additions & 1 deletion main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{{- set $opts "client" (ternary (in .Opts.client "" "true") true false) -}}
{{- set $opts "server" (ternary (in .Opts.server "" "true") true false) -}}
{{- set $opts "schemaHash" (ternary (eq (default .Opts.schemaHash "true") "false") false true) -}}
{{- set $opts "webrpcHeader" (ternary (eq (default .Opts.webrpcHeader "true") "false") false true) -}}

{{- /* Print help on -help. */ -}}
{{- if exists .Opts "help" -}}
Expand Down Expand Up @@ -87,7 +88,7 @@ WEBRPC_SCHEMA_HASH = "{{.SchemaHash}}"
{{template "errors" dict "WebrpcErrors" .WebrpcErrors "Errors" .Errors}}
{{template "helpers" dict}}
{{- if $opts.client}}
{{template "client" dict "BasePath" .BasePath "Services" .Services "TypeMap" $typeMap "AliasMap" $aliasMap}}
{{template "client" dict "BasePath" .BasePath "Services" .Services "TypeMap" $typeMap "AliasMap" $aliasMap "WebrpcHeader" $opts.webrpcHeader}}
{{- end}}
{{- if $opts.server}}
{{template "server" dict "BasePath" .BasePath "Services" .Services "TypeMap" $typeMap "AliasMap" $aliasMap}}
Expand Down
Loading