Skip to content

Commit 248a8de

Browse files
committed
Move the field_presence function to the Protobuf module
Also fix examples, and add an extra example about repeated
1 parent 1cb4cb0 commit 248a8de

File tree

2 files changed

+51
-40
lines changed

2 files changed

+51
-40
lines changed

lib/protobuf.ex

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,56 @@ defmodule Protobuf do
285285
"version that introduced implicit struct generation"
286286
end
287287

288+
@doc """
289+
Returns whether a field or oneof is present, not present, or maybe present
290+
291+
`:present` and `:not present` mean that a field is **explicitly** present or not,
292+
respectively.
293+
294+
Some values may be implicitly present. For example, lists in `repeated` fields
295+
always have implicit presence. In these cases, if the presence is ambiguous,
296+
returns `:maybe`.
297+
298+
For more information about field presence tracking rules, refer to the official
299+
[Field Presence docs](https://protobuf.dev/programming-guides/field_presence/).
300+
301+
302+
## Examples
303+
304+
# Non-optional proto3 field:
305+
Protobuf.field_presence(%MyMessage{foo: 42}, :foo)
306+
#=> :present
307+
308+
Protobuf.field_presence(%MyMessage{foo: 0}, :foo)
309+
#=> :maybe
310+
311+
Protobuf.field_presence(%MyMessage{}, :foo)
312+
#=> :maybe
313+
314+
# Optional proto3 field:
315+
Protobuf.field_presence(%MyMessage{bar: 42}, :bar)
316+
#=> :present
317+
318+
Protobuf.field_presence(%MyMessage{bar: 0}, :bar)
319+
#=> :present
320+
321+
Protobuf.field_presence(%MyMessage{}, :bar)
322+
#=> :not_present
323+
324+
# Lists
325+
Protobuf.field_presence(%MyMessage{repeated_field: []}, :repeated_field)
326+
#=> :maybe
327+
328+
Protobuf.field_presence(%MyMessage{repeated_field: [1}, :repeated_field)
329+
#=> :present
330+
331+
"""
332+
@doc since: "0.15.0"
333+
@spec field_presence(message :: struct(), field :: atom()) :: :present | :not_present | :maybe
334+
def field_presence(message, field) do
335+
Protobuf.Presence.field_presence(message, field)
336+
end
337+
288338
@doc """
289339
Loads extensions modules.
290340

lib/protobuf/presence.ex

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,8 @@
11
defmodule Protobuf.Presence do
2-
@moduledoc """
3-
Helpers for determining Protobuf field presence.
4-
"""
2+
@moduledoc false
53

64
alias Protobuf.FieldProps
75

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-
"""
456
@spec field_presence(message :: struct(), field :: atom()) :: :present | :not_present | :maybe
467
def field_presence(%mod{} = message, field) do
478
message_props = mod.__message_props__()

0 commit comments

Comments
 (0)