Skip to content

Commit 5df3e85

Browse files
committed
refactor(grpc): tighten codec enum specs, drop dead helpers
- instance_type/1 and arch/1: per-clause @specs over precise enum-atom types (instance_enum/arch_enum) instead of atom(); drop the {:error, :invalid_*} catch-all clauses and the now-orphaned rpc_error clauses that fed them. - Remove blank_to_nil/1: proto3 presence already distinguishes unset (nil) from explicit empty string, so boot_args passes through unchanged.
1 parent 649f5e8 commit 5df3e85

1 file changed

Lines changed: 23 additions & 31 deletions

File tree

lib/hyper/grpc/codec.ex

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ defmodule Hyper.Grpc.Codec do
66
* `from_grpc/1` — an inbound request message → a domain value.
77
* `to_grpc/1` — a domain result (or error) → an outbound response message,
88
or a `GRPC.RPCError` for the server to raise.
9-
10-
Keeping all wire knowledge here lets `Hyper.Grpc.Server` stay a thin dispatch
11-
layer.
129
"""
1310

1411
alias Hyper.Grpc.V0.{
@@ -41,13 +38,22 @@ defmodule Hyper.Grpc.Codec do
4138
ARCHITECTURE_AARCH64: :aarch64
4239
}
4340

44-
@doc """
45-
Convert an inbound request message to a domain value.
46-
47-
A `CreateMachineRequest` becomes a `Hyper.Vm.Spec`. Unset optional fields
48-
(`nil`) take Hyper's defaults: instance type → `:base`, architecture →
49-
resolved at create time, boot args → the standard cmdline.
50-
"""
41+
@typep instance_enum ::
42+
:INSTANCE_TYPE_MICRO
43+
| :INSTANCE_TYPE_MILLI
44+
| :INSTANCE_TYPE_CENTI
45+
| :INSTANCE_TYPE_DECI
46+
| :INSTANCE_TYPE_BASE
47+
| :INSTANCE_TYPE_DECA
48+
| :INSTANCE_TYPE_HECTO
49+
| :INSTANCE_TYPE_KILO
50+
| :INSTANCE_TYPE_MEGA
51+
| :INSTANCE_TYPE_GIGA
52+
| :INSTANCE_TYPE_TERA
53+
54+
@typep arch_enum :: :ARCHITECTURE_X86_64 | :ARCHITECTURE_AARCH64
55+
56+
@doc "Convert an inbound request message to a domain value."
5157
@spec from_grpc(CreateMachineRequest.t()) :: {:ok, Spec.t()} | {:error, term()}
5258
def from_grpc(%CreateMachineRequest{img_id: img_id}) when img_id in [nil, ""],
5359
do: {:error, :missing_img_id}
@@ -60,16 +66,12 @@ defmodule Hyper.Grpc.Codec do
6066
img_id: req.img_id,
6167
type: type,
6268
arch: arch,
63-
boot_args: blank_to_nil(req.boot_args)
69+
boot_args: req.boot_args
6470
}}
6571
end
6672
end
6773

68-
@doc """
69-
Convert a domain result to an outbound response message, or an error to a
70-
`GRPC.RPCError` (which the server raises). Dispatches on the result's shape;
71-
each shape carries its own precise return type below.
72-
"""
74+
@doc "Convert a domain result to an outbound response message, or an error to `GRPC.RPCError`."
7375
@spec to_grpc({:created, Hyper.Vm.id(), node()}) :: CreateMachineResponse.t()
7476
def to_grpc({:created, vm_id, node}) when is_binary(vm_id),
7577
do: %CreateMachineResponse{vm_id: vm_id, node: to_string(node)}
@@ -101,33 +103,23 @@ defmodule Hyper.Grpc.Codec do
101103
@spec machine({Hyper.Vm.id(), node()}) :: Machine.t()
102104
defp machine({vm_id, node}), do: %Machine{vm_id: vm_id, node: to_string(node)}
103105

104-
@spec instance_type(atom() | nil) :: {:ok, Hyper.Vm.Instance.t()} | {:error, term()}
106+
@spec instance_type(nil) :: {:ok, :base}
105107
defp instance_type(nil), do: {:ok, :base}
106108

109+
@spec instance_type(instance_enum()) :: {:ok, Hyper.Vm.Instance.t()}
107110
defp instance_type(enum) when is_map_key(@instance_types, enum),
108111
do: {:ok, @instance_types[enum]}
109112

110-
defp instance_type(enum), do: {:error, {:invalid_instance_type, enum}}
111-
112-
@spec arch(atom() | nil) :: {:ok, Hyper.Vm.Instance.arch() | nil} | {:error, term()}
113+
@spec arch(nil) :: {:ok, nil}
113114
defp arch(nil), do: {:ok, nil}
114-
defp arch(enum) when is_map_key(@arches, enum), do: {:ok, @arches[enum]}
115-
defp arch(enum), do: {:error, {:invalid_arch, enum}}
116115

117-
@spec blank_to_nil(String.t() | nil) :: String.t() | nil
118-
defp blank_to_nil(s) when s in [nil, ""], do: nil
119-
defp blank_to_nil(s), do: s
116+
@spec arch(arch_enum()) :: {:ok, Hyper.Vm.Instance.arch()}
117+
defp arch(enum) when is_map_key(@arches, enum), do: {:ok, @arches[enum]}
120118

121119
@spec rpc_error(term()) :: GRPC.RPCError.t()
122120
defp rpc_error(:missing_img_id),
123121
do: GRPC.RPCError.exception(:invalid_argument, "img_id is required")
124122

125-
defp rpc_error({:invalid_instance_type, _}),
126-
do: GRPC.RPCError.exception(:invalid_argument, "unknown instance_type")
127-
128-
defp rpc_error({:invalid_arch, _}),
129-
do: GRPC.RPCError.exception(:invalid_argument, "unknown arch")
130-
131123
defp rpc_error(:not_found),
132124
do: GRPC.RPCError.exception(:not_found, "no such machine")
133125

0 commit comments

Comments
 (0)