|
| 1 | +defmodule Protobuf.Presence do |
| 2 | + @moduledoc """ |
| 3 | + Helpers for determining Protobuf field presence. |
| 4 | + """ |
| 5 | + |
| 6 | + alias Protobuf.FieldProps |
| 7 | + |
| 8 | + @doc """ |
| 9 | + Returns whether a field or oneof is present, not present, or maybe present |
| 10 | +
|
| 11 | + `:present` and `:not present` mean that a field is **explicitly** present or not, |
| 12 | + respectively. |
| 13 | +
|
| 14 | + Some values may be implicitly present. For example, lists in `repeated` fields |
| 15 | + always have implicit presence. In these cases, if the presence is ambiguous, |
| 16 | + returns `:maybe`. |
| 17 | +
|
| 18 | + For more information about field presence tracking rules, refer to the official |
| 19 | + [Field Presence docs](https://protobuf.dev/programming-guides/field_presence/). |
| 20 | +
|
| 21 | +
|
| 22 | + ## Examples |
| 23 | +
|
| 24 | + # Non-optional proto3 field: |
| 25 | + Protobuf.Presence(%MyMessage{foo: 42}, :foo) |
| 26 | + #=> :present |
| 27 | +
|
| 28 | + Protobuf.Presence(%MyMessage{foo: 0}, :foo) |
| 29 | + #=> :maybe |
| 30 | +
|
| 31 | + Protobuf.Presence(%MyMessage{}, :foo) |
| 32 | + #=> :maybe |
| 33 | +
|
| 34 | + # Optional proto3 field: |
| 35 | + Protobuf.Presence(%MyMessage{bar: 42}, :bar) |
| 36 | + #=> :present |
| 37 | +
|
| 38 | + Protobuf.Presence(%MyMessage{bar: 0}, :bar) |
| 39 | + #=> :present |
| 40 | +
|
| 41 | + Protobuf.Presence(%MyMessage{}, :bar) |
| 42 | + #=> :not_present |
| 43 | +
|
| 44 | + """ |
| 45 | + @spec field_presence(message :: struct(), field :: atom()) :: :present | :not_present | :maybe |
| 46 | + def field_presence(%mod{} = message, field) do |
| 47 | + message_props = mod.__message_props__() |
| 48 | + transformed_message = transform_module(message, mod) |
| 49 | + fnum = Map.fetch!(message_props.field_tags, field) |
| 50 | + field_prop = Map.fetch!(message_props.field_props, fnum) |
| 51 | + value = get_oneof_value(transformed_message, message_props, field, field_prop) |
| 52 | + |
| 53 | + transformed_value = |
| 54 | + case field_prop do |
| 55 | + %{embedded: true, type: mod} -> transform_module(value, mod) |
| 56 | + _ -> value |
| 57 | + end |
| 58 | + |
| 59 | + get_field_presence(message_props.syntax, transformed_value, field_prop) |
| 60 | + end |
| 61 | + |
| 62 | + defp get_oneof_value(message, message_props, field, field_prop) do |
| 63 | + case field_prop.oneof do |
| 64 | + nil -> |
| 65 | + Map.fetch!(message, field) |
| 66 | + |
| 67 | + oneof_num -> |
| 68 | + {oneof_field, _} = Enum.find(message_props.oneof, fn {_name, tag} -> tag == oneof_num end) |
| 69 | + |
| 70 | + case Map.fetch!(message, oneof_field) do |
| 71 | + {^field, value} -> value |
| 72 | + _ -> nil |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + defp transform_module(message, module) do |
| 78 | + if transform_module = module.transform_module() do |
| 79 | + transform_module.encode(message, module) |
| 80 | + else |
| 81 | + message |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + # We probably want to make this public eventually, but it makes sense to hold |
| 86 | + # it until we add editions support, since we definitely don't want to add |
| 87 | + # `syntax` in a public API |
| 88 | + @doc false |
| 89 | + @spec get_field_presence(:proto2 | :proto3, term(), FieldProps.t()) :: :present | :not_present | :maybe |
| 90 | + def get_field_presence(syntax, value, field_prop) |
| 91 | + |
| 92 | + # Repeated and maps are always implicit. |
| 93 | + def get_field_presence(_syntax, [], _prop) do |
| 94 | + :maybe |
| 95 | + end |
| 96 | + |
| 97 | + def get_field_presence(_syntax, val, _prop) when is_map(val) do |
| 98 | + if map_size(val) == 0 do |
| 99 | + :maybe |
| 100 | + else |
| 101 | + :present |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + # For proto2 singular cardinality fields: |
| 106 | + # |
| 107 | + # - Non-one_of fields with default values have implicit presence |
| 108 | + # - Others have explicit presence |
| 109 | + def get_field_presence(:proto2, nil, _prop) do |
| 110 | + :not_present |
| 111 | + end |
| 112 | + |
| 113 | + def get_field_presence(:proto2, value, %FieldProps{default: value, oneof: nil}) do |
| 114 | + :maybe |
| 115 | + end |
| 116 | + |
| 117 | + def get_field_presence(:proto2, _value, _props) do |
| 118 | + :present |
| 119 | + end |
| 120 | + |
| 121 | + # For proto3 singular cardinality fields: |
| 122 | + # |
| 123 | + # - Optional and Oneof fields have explicit presence tracking |
| 124 | + # - Other fields have implicit presence tracking |
| 125 | + def get_field_presence(:proto3, nil, %FieldProps{proto3_optional?: true}) do |
| 126 | + :not_present |
| 127 | + end |
| 128 | + |
| 129 | + def get_field_presence(:proto3, _, %FieldProps{proto3_optional?: true}) do |
| 130 | + :present |
| 131 | + end |
| 132 | + |
| 133 | + def get_field_presence(_syntax, value, %FieldProps{oneof: oneof}) when not is_nil(oneof) do |
| 134 | + if is_nil(value) do |
| 135 | + :not_present |
| 136 | + else |
| 137 | + :present |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + # Messages have explicit presence tracking in proto3 |
| 142 | + def get_field_presence(:proto3, nil, _prop) do |
| 143 | + :not_present |
| 144 | + end |
| 145 | + |
| 146 | + # Defaults for different field types: implicit presence means they are maybe set |
| 147 | + def get_field_presence(:proto3, 0, _prop) do |
| 148 | + :maybe |
| 149 | + end |
| 150 | + |
| 151 | + def get_field_presence(:proto3, +0.0, _prop) do |
| 152 | + :maybe |
| 153 | + end |
| 154 | + |
| 155 | + def get_field_presence(:proto3, "", _prop) do |
| 156 | + :maybe |
| 157 | + end |
| 158 | + |
| 159 | + def get_field_presence(:proto3, false, _prop) do |
| 160 | + :maybe |
| 161 | + end |
| 162 | + |
| 163 | + def get_field_presence(_syntax, value, %FieldProps{type: {:enum, enum_mod}}) do |
| 164 | + if enum_default?(enum_mod, value) do |
| 165 | + :maybe |
| 166 | + else |
| 167 | + :present |
| 168 | + end |
| 169 | + end |
| 170 | + |
| 171 | + # Finally, everything else. |
| 172 | + def get_field_presence(_syntax, _val, _prop) do |
| 173 | + :present |
| 174 | + end |
| 175 | + |
| 176 | + defp enum_default?(enum_mod, val) when is_atom(val), do: enum_mod.value(val) == 0 |
| 177 | + defp enum_default?(_enum_mod, val) when is_integer(val), do: val == 0 |
| 178 | + defp enum_default?(_enum_mod, list) when is_list(list), do: false |
| 179 | +end |
0 commit comments