diff --git a/README.md b/README.md index bd6366e..a09a2cb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/_examples/example.gen.py b/_examples/example.gen.py index 830c304..d86eea7 100644 --- a/_examples/example.gen.py +++ b/_examples/example.gen.py @@ -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. # @@ -16,7 +16,7 @@ WEBRPC_VERSION = "v1" WEBRPC_SCHEMA_VERSION = "v1.0.0" -WEBRPC_SCHEMA_HASH = "eff11df6be0966e5f8ddb7005bae50bf68a5059c" +WEBRPC_SCHEMA_HASH = "8ea6c2298bb7597dce55f9ec922fba0a5257fc54" @@ -38,6 +38,7 @@ class Item: name: ItemName tier: ItemTier count: int + quantity: Quantity balance: int tags: list[str] attributes: dict[str, str] @@ -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() }, @@ -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() }, diff --git a/_examples/example.ridl b/_examples/example.ridl index 229991d..30eb88a 100644 --- a/_examples/example.ridl +++ b/_examples/example.ridl @@ -25,6 +25,7 @@ struct Item - name: ItemName - tier: ItemTier - count: uint32 + - quantity: Quantity - balance: bigint - tags: []string - attributes: map diff --git a/_examples/test_example.py b/_examples/test_example.py index e4f5080..36eb458 100644 --- a/_examples/test_example.py +++ b/_examples/test_example.py @@ -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"}, @@ -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 @@ -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" diff --git a/client.go.tmpl b/client.go.tmpl index 3da2d48..4f587f9 100644 --- a/client.go.tmpl +++ b/client.go.tmpl @@ -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}} @@ -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}} diff --git a/main.go.tmpl b/main.go.tmpl index 388d475..fbd5283 100644 --- a/main.go.tmpl +++ b/main.go.tmpl @@ -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" -}} @@ -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}}